CentOS 8 系统有了不更改变,例如 dnf 代替 yum 成为默认包管理工具。这不,许多人发现 CentOS 7熟悉的 ntpdate 命令没有了,也不能用yum安装上,同步时间顿时成了一个难题。本文介绍CentOS 8 使用chron y同步时间。
chrony介绍
Chrony 是一个开源软件,能用来于时钟服务器(NTP)同步,从而保持系统时间精确。chrony 由两部分组成:后台运行的 chronyd,前端使用的 chronyc。chronyd 用于调整内核中系统时钟和时钟服务器同步,它确定计算机增减时间的比率,并对此进行补偿;chronyc 提供查询和配置接口,可以在 chronyd 实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。
chrony同步时间
dnf install -y chrony # 也可以用yum安装 yum install -y chrony
chrony配置文件是 /etc/chrony.conf
# Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). pool ntp5.aliyun.com iburst # Record the rate at which the system clock gains/losses time. driftfile /var/lib/chrony/drift # Allow the system clock to be stepped in the first three updates # if its offset is larger than 1 second. makestep 1.0 3 # Enable kernel synchronization of the real-time clock (RTC). rtcsync # Enable hardware timestamping on all interfaces that support it. #hwtimestamp * # Increase the minimum number of selectable sources required to adjust # the system clock. #minsources 2 # Allow NTP client access from local network. #allow 192.168.0.0/16 # Serve time even if not synchronized to a time source. #local stratum 10 # Specify file containing keys for NTP authentication. keyfile /etc/chrony.keys # Get TAI-UTC offset and leap seconds from the system tz database. leapsectz right/UTC # Specify directory for log files. logdir /var/log/chrony # Select which information is logged. #log measurements statistics tracking
一般来说,配置文件无需改动就能正常使用。当然为了同步速度快,可以使用国内的ntp服务器,例如将第一行改成: ntp1.aliyun.com iburst(公共ntp服务器请参考:公共 NTP 服务器地址大全)。
接下来可以手动同步时间:
chronyd -q 'pool ntp1.aliyun.com iburst' # output 2021-01-20T13:43:30Z chronyd version 3.5 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG) 2021-01-20T13:43:34Z System clock wrong by -0.001392 seconds (step) 2021-01-20T13:43:34Z chronyd exiting
更好的方式是让chronyd后台运行,自动同步时间:
systemctl enable chronyd systemctl start chronyd
注意:chronyd服务启动后,不能再用chronyd方式手动同步时间,会报“Fatal error : Another chronyd may already be running”的错误。
chronyc tracking命令可以查看同步状态(需要chronyd服务在运行):
[root@linux ~] chronyc tracking Reference ID : 00000000 () Stratum : 0 Ref time (UTC) : Thu Jan 01 00:00:00 1970 System time : 0.000000000 seconds fast of NTP time Last offset : +0.000000000 seconds RMS offset : 0.000000000 seconds Frequency : 20.852 ppm fast Residual freq : +0.000 ppm Skew : 0.000 ppm Root delay : 1.000000000 seconds Root dispersion : 1.000000000 seconds Update interval : 0.0 seconds Leap status : Not synchronised
chronyc sources命令可以查看同步的时间服务器(需要chronyd服务在运行):
[root@linux ~] chronyc tracking Reference ID : 00000000 () Stratum : 0 Ref time (UTC) : Thu Jan 01 00:00:00 1970 System time : 0.000000000 seconds fast of NTP time Last offset : +0.000000000 seconds RMS offset : 0.000000000 seconds Frequency : 20.852 ppm fast Residual freq : +0.000 ppm Skew : 0.000 ppm Root delay : 1.000000000 seconds Root dispersion : 1.000000000 seconds Update interval : 0.0 seconds Leap status : Not synchronised [root@imac ~]# chronyc sources 210 Number of sources = 4 MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 119.28.183.184 2 6 73 11 -2557ns[ -876us] +/- 57ms ^? ntp.wdc1.us.leaseweb.net 2 7 2 72 -487us[-1360us] +/- 248ms ^- time.cloudflare.com 3 6 123 70 +376us[ -497us] +/- 86ms ^- undefined.hostname.local> 2 6 37 10 -3071us[-3071us] +/- 118ms
CONFIGURING THE DATE AND TIME
现代操作系统区分了以下两种类型的时钟:
- 实时时钟(RTC),通常被称为硬件时钟(通常是系统板上的集成电路),它完全独立于操作系统的当前状态,即使在计算机关闭时也能运行。
- 一种系统时钟,也称为软件时钟,由内核维护,其初始值基于实时时钟。一旦系统启动并初始化系统时钟,系统时钟就完全独立于实时时钟。
系统时间总是保持协调世界时(UTC),并根据需要在应用程序中转换为本地时间。本地时间是当前时区的实际时间,考虑了夏令时(DST)。实时时钟可以使用UTC或本地时间。建议UTC。
Red Hat Enterprise Linux 8 提供了三个命令行工具,可以用来配置和显示关于系统日期和时间的信息:
- timedatectl实用程序是Red Hat Enterprise Linux 7中的新工具,是systemd的一部分。更改当前日期和时间,设置时区,或启用系统时钟与远程服务器自动同步。
- 传统的 date 命令。
- 用于访问硬件时钟的hwclock实用程序。
显示当前日期和时间
显示当前日期和时间以及有关系统和硬件时钟配置的详细信息
~]$ timedatectl # 显示本地时间和通用时间、当前使用的时区、NTP (Network time Protocol)配置状态以及与夏令时相关的附加信息。 Local time: Wed 2021-01-20 22:00:22 CST Universal time: Wed 2021-01-20 14:00:22 UTC RTC time: Wed 2021-01-20 22:00:22 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes NTP service: active RTC in local TZ: yes
缺省情况下,系统配置为UTC。要配置您的系统以本地时间维护时钟,以root用户身份运行带有set-local-rtc选项的timedatectl命令,如果要配置您的系统维护本地时间的时钟,将布尔值替换为yes(或者,或者,y, true, t,或1)。如果要配置系统使用UTC,将布尔值替换为no(或者,或者,n, false, f,或0)。
~] timedatectl set-local-rtc false
修改时区
~] timedatectl list-timezones # 列出所有可用的时区 ~] timedatectl set-timezone time_zone # 将time_zone替换为timedatectl列表-timezones中列出的任何值 ~] timedatectl list-timezones | grep Asia # 列出与当前位置最近的时区,使用timedatectl命令和list-timezones命令行选项。例如,要列出亚洲所有可用的时区 ~] timedatectl set-timezone Asia/Shanghai # 将时区改为:亚洲/Shanghai
与远程服务器同步系统时钟
要使系统与远程NTP服务器同步系统时钟,将boolean替换为yes(默认选项)。要禁用此特性,请将布尔值替换为no。
timedatectl set-ntp yes
设置硬件时钟为本地时区
~] timedatectl set-local-rtc 1 # 设置硬件时钟为本地时区 ~] timedatectl set-local-rtc 0 # 设置硬件时钟为协调世界时(UTC) ~] timedatectl set-ntp true # 启用与远程NTP服务器的自动时间同步 ~] timedatectl set-ntp false # 禁用与远程NTP服务器的自动时间同步
同步日期和时间
同步硬件时钟和系统时间时,还可以通过添加–utc或–localtime选项来指定将硬件时钟保持在本地时间还是UTC 。与使用相似–set,UTC或LOCAL记录在/etc/adjtime文件中。
该hwclock –systohc –utc 命令在功能上与相似,timedatectl set-local-rtc false 并且 hwclock –systohc –local 是的替代方法 timedatectl set-local-rtc true
有两个方向可以同步硬件时钟和当前系统时间
hwclock --systohc # 设置硬件时钟为当前系统时间 hwclock --hctosys # 设置硬件时钟为系统时间
将硬件时钟与系统时间同步
要将硬件时钟设置为当前系统时间并将硬件时钟保持在本地时间
~] hwclock --systohc --localtime
为避免时区和DST切换问题,建议将硬件时钟保持为UTC。所示的示例3.11“将硬件时钟与系统时间同步”很有用,例如,在Windows系统的多重启动的情况下,假定默认情况下硬件时钟在本地时间运行,并且所有其他系统都需要适应也可以使用当地时间 虚拟机可能也需要它;如果主机提供的虚拟硬件时钟以本地时间运行,则也需要将来宾系统配置为使用本地时间。