本文操作系统环境是
Cent OS 7.6
Chrony
在 Cent OS 7.x
系统中是内置的开源软件。分布式系统中的所有机器必须保持时间一致,至于为什么要一致,有空再单独一篇文章来讲讲原因,这篇文章主要阐述如何使用 Chrony
Chrony 程序组成
有两部分组成:
chronyd
是守护进程在系统中一直保持运行,并保持和时间时间服务器进行交互同步时间chronyc
主要提供的是用户端命令交互功能,相当于一个轻量的客户端
设置
如果系统中没有 chrony
执行一下命令可以快速安装
yum install chrony -y
设置开机自动启动
systemctl enable chronyd.service
systemctl restart chronyd.service
systemctl status chronyd.service
防火墙
为了避免防火墙的影响,放行 chrony
的通信端口 123/UDP
,允许防火墙访问 NTP
服务
# 放行
firewall-cmd --add-service=ntp --permanent
# 重新加载防火墙规则
firewall-cmd --reload
查看当前时区
查看当前系统时区
╭─root at react in ~
╰─○ timedatectl
...
Time zone: Asia/Shanghai (CST, +0800)
...
如果时区不对,按照如下步骤进行重新设置
# 查看所有可用时区
╭─root at react in ~
╰─○ timedatectl list-timezones | grep -E "Asia/S.*"
Asia/Sakhalin
Asia/Samarkand
Asia/Seoul
Asia/Shanghai
Asia/Singapore
Asia/Srednekolymsk
# 设置亚洲/上海时区
timedatectl set-timezone Asia/Shanghai
同步本地时间
# 设置完成后强制同步时钟
chronyc -a makestep
# 查看时间同步源
╭─root at react in ~
╰─○ chronyc sources -v
210 Number of sources = 4
.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
|| .- xxxx [ yyyy ] +/- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | | \
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* dns2.synet.edu.cn 1 10 375 666 -1991us[-4165us] +/- 21ms
^- 119.28.206.193 2 10 367 951 +46ms[ +44ms] +/- 79ms
^- tock.ntp.infomaniak.ch 1 10 377 93 -14ms[ -14ms] +/- 85ms
^- ntp.wdc2.us.leaseweb.net 2 10 235 573 -19ms[ -19ms] +/- 262ms
# 查看时间同步源状态
╭─root at react in ~
╰─○ chronyc sourcestats -v
210 Number of sources = 4
.- Number of sample points in measurement set.
/ .- Number of residual runs with same sign.
| / .- Length of measurement set (time).
| | / .- Est. clock freq error (ppm).
| | | / .- Est. error in freq.
| | | | / .- Est. offset.
| | | | | | On the -.
| | | | | | samples. \
| | | | | | |
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
==============================================================================
dns2.synet.edu.cn 6 3 111m -0.632 2.823 -459us 1332us
119.28.206.193 44 20 540m -0.073 0.081 +43ms 1680us
tock.ntp.infomaniak.ch 45 22 554m -0.087 0.144 -7985us 3233us
ntp.wdc2.us.leaseweb.net 6 3 398m -0.024 1.616 -25ms 4467us
# 启用 NTP 时间同步,启用过后会有一定的时间才会生效,取决于本地网络!
timedatectl set-ntp yes
# 时间校准,同样需要一定的时间,时间开销在本地和时间服务器之间的通信上!
chronyc tracking
参考资料: