本文分享自天翼云开发者社区《chrony时间同步软件介绍》,作者:刘****苏
chrony是网络时间协议NTP的通用实现,它可以将系统时钟和`NTP服务器同步。它支持在各种条件下包括间歇性的网络连接、严重阻塞的网络、不断变化的温度以及支持不连续的运行并且可以运行于虚机上。
本文介绍chrony工具的安装和使用,包括两个节点:一个作为时间服务器,另一个作为客户端。
执行如下命令来安装chrony:
1[root@kvm ~]# rpm -ql chrony 2/etc/NetworkManager/dispatcher.d/20-chrony-dhcp 3/etc/NetworkManager/dispatcher.d/20-chrony-onoffline 4/etc/chrony.conf 5/etc/chrony.keys 6/etc/dhcp/dhclient.d/chrony.sh 7/etc/logrotate.d/chrony 8/etc/sysconfig/chronyd 9/usr/bin/chronyc 10/usr/lib/.build-id 11/usr/lib/.build-id/38 12/usr/lib/.build-id/38/1d3a324ce66865ba879dfa43d49e0e0c1e2c41 13/usr/lib/.build-id/43 14/usr/lib/.build-id/43/444c816fcf0802c43011f282afaf9feded4c56 15/usr/lib/systemd/ntp-units.d/50-chronyd.list 16/usr/lib/systemd/system/chrony-dnssrv@.service 17/usr/lib/systemd/system/chrony-dnssrv@.timer 18/usr/lib/systemd/system/chrony-wait.service 19/usr/lib/systemd/system/chronyd.service 20/usr/libexec/chrony-helper 21/usr/sbin/chronyd 22/usr/share/doc/chrony 23/usr/share/doc/chrony/FAQ 24/usr/share/doc/chrony/NEWS 25/usr/share/doc/chrony/README 26/usr/share/doc/chrony/ntp2chrony.py 27/usr/share/licenses/chrony 28/usr/share/licenses/chrony/COPYING 29/usr/share/man/man1/chronyc.1.gz 30/usr/share/man/man5/chrony.conf.5.gz 31/usr/share/man/man8/chronyd.8.gz 32/var/lib/chrony 33/var/lib/chrony/drift 34/var/lib/chrony/rtc 35/var/log/chrony
如下所示为配置文件:
1[root@kvm ~]# cat /etc/chrony.conf 2pool 2.centos.pool.ntp.org iburst 3 4# Record the rate at which the system clock gains/losses time. 5driftfile /var/lib/chrony/drift 6 7# Allow the system clock to be stepped in the first three updates 8# if its offset is larger than 1 second. 9makestep 1.0 3 10 11# Enable kernel synchronization of the real-time clock (RTC). 12rtcsync 13 14# Enable hardware timestamping on all interfaces that support it. 15#hwtimestamp * 16 17# Increase the minimum number of selectable sources required to adjust 18# the system clock. 19#minsources 2 20 21# Allow NTP client access from local network. 22allow 0/0 23 24# Serve time even if not synchronized to a time source. 25local stratum 10 26 27# Specify file containing keys for NTP authentication. 28keyfile /etc/chrony.keys 29 30# Get TAI-UTC offset and leap seconds from the system tz database. 31leapsectz right/UTC 32 33# Specify directory for log files. 34logdir /var/log/chrony 35 36# Select which information is logged. 37#log measurements statistics tracking
其中: pool:为外部时钟源地址 rtcsync:开启硬件时钟的同步机制,linux内核每11分钟同步一次系统时钟到硬件时钟 local stratum 10:当外部时钟源不可用的时候,以本机时钟为准继续提供服务 allow 0/0:允许的客户端的cidr 启动服务后查看状态:
1[root@kvm ~]# systemctl start chronyd 2[root@kvm ~]# 3[root@kvm ~]# 4[root@kvm ~]# systemctl status chronyd 5● chronyd.service - NTP client/server 6 Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled) 7 Active: active (running) since Fri 2023-09-08 19:02:50 CST; 2min 43s left 8 Docs: man:chronyd(8) 9 man:chrony.conf(5) 10 Process: 11542 ExecStopPost=/usr/libexec/chrony-helper remove-daemon-state (code=exited, status=0/SUCCESS) 11 Process: 11552 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS) 12 Process: 11547 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS) 13 Main PID: 11550 (chronyd) 14 Tasks: 1 (limit: 52427) 15 Memory: 924.0K 16 CGroup: /system.slice/chronyd.service 17 └─11550 /usr/sbin/chronyd 18 19Sep 08 19:02:50 kvm chronyd[11550]: chronyd version 4.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +NTS +SECHASH +IPV6 +DEBUG) 20Sep 08 19:02:50 kvm chronyd[11550]: Frequency 1.633 +/- 0.046 ppm read from /var/lib/chrony/drift 21Sep 08 19:02:50 kvm chronyd[11550]: Using right/UTC timezone to obtain leap second data 22Sep 08 19:02:50 kvm systemd[1]: Started NTP client/server. 23Sep 08 19:02:55 kvm chronyd[11550]: Selected source 36.110.233.85 (2.centos.pool.ntp.org) 24Sep 08 19:02:55 kvm chronyd[11550]: System clock wrong by -791.549636 seconds 25Sep 08 18:49:44 kvm chronyd[11550]: System clock was stepped by -791.549636 seconds 26Sep 08 18:49:44 kvm chronyd[11550]: System clock TAI offset set to 37 seconds 27Sep 08 18:50:49 kvm chronyd[11550]: Selected source 119.28.206.193 (2.centos.pool.ntp.org) 28Sep 08 18:54:07 kvm chronyd[11550]: Source 81.16.177.123 replaced with 78.46.102.180 (2.centos.pool.ntp.org) 29[root@kvm ~]# 30[root@kvm ~]# netstat -tunlp |grep chrony 31udp 0 0 0.0.0.0:123 0.0.0.0:* 11550/chronyd 32udp 0 0 127.0.0.1:323 0.0.0.0:* 11550/chronyd 33udp6 0 0 ::1:323 :::* 11550/chronyd 34[root@kvm ~]# 35[root@kvm ~]# chronyc sources -v 36 37 .-- Source mode '^' = server, '=' = peer, '#' = local clock. 38 / .- Source state '*' = current best, '+' = combined, '-' = not combined, 39| / 'x' = may be in error, '~' = too variable, '?' = unusable. 40|| .- xxxx [ yyyy ] +/- zzzz 41|| Reachability register (octal) -. | xxxx = adjusted offset, 42|| Log2(Polling interval) --. | | yyyy = measured offset, 43|| \ | | zzzz = estimated error. 44|| | | \ 45MS Name/IP address Stratum Poll Reach LastRx Last sample 46=============================================================================== 47^- 79.133.44.136 1 6 377 56 -10ms[ -10ms] +/- 138ms 48^- electrode.felixc.at 2 6 33 55 +17ms[ +17ms] +/- 119ms 49^* 119.28.206.193 2 6 377 60 +1706us[+2327us] +/- 40ms 50^+ 36.110.233.85 2 6 377 58 -1324us[-1324us] +/- 68ms
chrony守护进程监听在udp协议的123和323端口。 客户端节点上安装好chrony后,修改配置文件如下所示:
1[root@node1 ~]# cat /etc/chrony.conf 2server 172.24.21.221 iburst 3 4# Record the rate at which the system clock gains/losses time. 5driftfile /var/lib/chrony/drift 6 7# Allow the system clock to be stepped in the first three updates 8# if its offset is larger than 1 second. 9makestep 3600 3 10 11# Enable kernel synchronization of the real-time clock (RTC). 12rtcsync 13 14# Enable hardware timestamping on all interfaces that support it. 15#hwtimestamp * 16 17# Increase the minimum number of selectable sources required to adjust 18# the system clock. 19#minsources 2 20 21# Allow NTP client access from local network. 22#allow 192.168.0.0/16 23 24# Serve time even if not synchronized to a time source. 25#local stratum 10 26 27# Specify file containing keys for NTP authentication. 28#keyfile /etc/chrony.keys 29 30# Specify directory for log files. 31logdir /var/log/chrony 32 33# Select which information is logged. 34#log measurements statistics tracking
查看状态:
1[root@node1 ~]# chronyc sources -v 2210 Number of sources = 1 3 4 .-- Source mode '^' = server, '=' = peer, '#' = local clock. 5 / .- Source state '*' = current synced, '+' = combined , '-' = not combined, 6| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable. 7|| .- xxxx [ yyyy ] +/- zzzz 8|| Reachability register (octal) -. | xxxx = adjusted offset, 9|| Log2(Polling interval) --. | | yyyy = measured offset, 10|| \ | | zzzz = estimated error. 11|| | | \ 12MS Name/IP address Stratum Poll Reach LastRx Last sample 13=============================================================================== 14^* 172.24.21.221 3 6 377 37 -7753ns[ +140us] +/- 29ms
chrony默认是采用连续同步的方式来同步时钟的,并非采用跳变的方式。
