實驗環境
系統 角色 IP
Centos7.6 Server 192.168.14.217
Centos7.6 Client 192.168.14.218
一、Server端配置
1、Centos7系統已經默認安裝chrony服務,沒有再執行以下命令
[root@server ~]# yum install -y chrony
2、啓動服務
[root@server ~]# systemctl start chronyd
[root@server ~]# systemctl enable chronyd
3、修改配置文件
[root@server ~]# vi /etc/chrony.conf
4、查看時間同步源(^*表示已經同步)
[root@server ~]# 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
===============================================================================
^- sv1.ggsrv.de 2 6 327 88 +26ms[ +26ms] +/- 131ms
^- ntp8.flashdance.cx 2 6 377 18 +3759us[+3759us] +/- 152ms
^- 119.28.183.184 2 6 317 26 -653us[ -653us] +/- 49ms
^* 120.25.115.20 2 6 377 89 +50us[ +119us] +/- 4323us
二、Clinet端配置
1、修改配置文件
[root@client ~]# cat /etc/chrony.conf |grep -v "^$"|grep -v "^#"
server server iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
註釋默認的server地址,添加本地時間同步地址
2、修改主機名解析
#底部添加一條記錄
[root@client ~]# vi /etc/hosts
192.168.14.217 server
3、重啓服務
[root@client ~]# systemctl restart chronyd
4、查看時間是否同步
5、就算chronyc sources -v顯示^*,但是如果NTP顯示no也是不會同步時間的。(解決方法是重啓服務)
6、模擬時間不同步,隨意修改一個時間
[root@client ~]# date -s "2020-06-29 08:00:00"
7、查看時間同步狀態(^?沒同步,NTP狀態爲no)
8、NTP狀態爲不會自動變爲yes,需要重啓服務
9、因此可以編輯腳本,添加到自動任務
#當檢測NTP synchronized狀態爲no的時候,自動執行重啓服務
[root@client ~]# vi time.sh
#!/bin/bash
status=`timedatectl | grep "NTP synchronized" | awk -F' ' '{print $3}'`
if [ "$status" = "no" ]; then
systemctl restart chronyd
fi
#添加到自動任務,一分鐘執行一次
[root@client ~]# crontab -e
*/1 * * * * /root/time.sh
#查看日誌
[root@client ~]# tail -f /var/log/cron
10、再次查看時間同步狀態,至此完成