chrony实现时间同步 - 实践手册
架构图
前期准备
安装包 chrony
yum -y install chrony
chronyd:后台运行的守护进程,用于调整内核运行的系统时钟和时钟服务器同步。确定计算机增减时间的比率,并对此进行补偿
chronyc:命令行用户工具,用于监控性能并进行多样化的配置。可在chrony 实例控制的计算机上工作,也可在一台不同的远程计算机上工作
服务unit 文件:/usr/lib/systemd/system/chronyd.service
配置文件:/etc/chrony.conf
监听端口:323/udp,123/udp
NTP Server1
IP:10.0.0.8
NTP Server2
IP:10.0.0.18
客户端(2)
IP:10.0.0.7
IP:10.0.0.6
操作步骤
1 配置NTP Server1 和NTP Server2
# 配置NTP Server1
[root@centos8 ~]#yum -y install chrony
# 删除原有的pool
server ntp.aliyun.com iburst
server time1.cloud.tencent.com iburst
...省略部分信息...
# Allow NTP client access from local network.
#allow 192.168.0.0/16 设置允许同步的客户端
allow 10.0.0.0/24
# Serve time even if not synchronized to a time source.
# 外网断网的话,允许客户端同步本机的时间
local stratum 10
...省略部分信息...
[root@centos8 ~]#date -s '-1 year'
Mon Sep 9 21:53:35 CST 2019
[root@centos8 ~]#systemctl restart chronyd
[root@centos8 ~]#date
Wed Sep 9 21:53:51 CST 2020
[root@centos8 ~]#chronyc -n sources -v
210 Number of sources = 2
.-- 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
===============================================================================
^- 203.107.6.88 2 6 17 35 +594us[ +594us] +/- 21ms
^* 139.199.215.251 2 6 17 35 -1134us[ -799us] +/- 45ms
[root@centos8 ~]#scp /etc/chrony.conf 10.0.0.18:/etc/
root@10.0.0.18's password:
chrony.conf 100% 1169 1.5MB/s 00:00
# 配置NTP Server2
[root@centos8 ~]#yum -y install chrony
[root@CentOS8 ~]#systemctl restart chronyd.service
[root@CentOS8 ~]#ss -ntlu
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 0.0.0.0:123 0.0.0.0:*
udp UNCONN 0 0 127.0.0.1:323 0.0.0.0:*
udp UNCONN 0 0 [::1]:323 [::]:*
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
tcp LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
tcp LISTEN 0 128 [::]:22 [::]:*
tcp LISTEN 0 128 *:23 *:*
tcp LISTEN 0 100 [::1]:25 [::]:*
3 设置客户端1 的chrony 的配置,并进行同步
[root@centos7 ~]#vim /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 10.0.0.8 iburst
server 10.0.0.18 iburst
...省略部分...
[root@centos7 ~]#chronyc -n sources -v
210 Number of sources = 2
.-- 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
===============================================================================
^* 10.0.0.8 3 6 17 46 +1726us[+1727us] +/- 23ms
^? 10.0.0.18 0 7 0 - +0ns[ +0ns] +/- 0ns
[root@centos7 ~]#date
Wed Sep 9 22:04:20 CST 2020
4 设置客户端2 的配置,并使用ntp进行同步
[root@centos6 ~]#vim /etc/ntp.conf
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 10.0.0.8 iburst
server 10.0.0.18 iburst
...省略部分信息...
[root@centos6 ~]#service ntpd restart
Shutting down ntpd: [ OK ]
Starting ntpd: [ OK ]
# 查看同步情况
[root@centos6 ~]#ntpq -np
remote refid st t when poll reach delay offset jitter
==============================================================================
*10.0.0.8 203.107.6.88 3 u 37 64 3 0.250 0.023 0.800
+10.0.0.18 203.107.6.88 3 u 35 64 3 0.339 -1.752 0.178
# 或关闭ntpd 服务,使用ntpdate 同步
[root@centos6 ~]#service ntpd stop
Shutting down ntpd: [ OK ]
[root@centos6 ~]#date -s '-2 month'
Thu Jul 9 22:24:48 CST 2020
[root@centos6 ~]#ntpdate 10.0.0.8
9 Sep 22:24:59 ntpdate[1687]: step time server 10.0.0.8 offset 5356799.996917 sec
[root@centos6 ~]#date
Wed Sep 9 22:25:08 CST 2020
e 10.0.0.8
9 Sep 22:24:59 ntpdate[1687]: step time server 10.0.0.8 offset 5356799.996917 sec
[root@centos6 ~]#date
Wed Sep 9 22:25:08 CST 2020