[TOC]
<br >
*****
<br >
# **Chrony**
<br >
## **安裝 Chrony**
1. 安裝 chrony(用于自動同步系統時鐘):
~~~
# yum erase 'ntp*'
# yum install -y chrony
~~~
> ntpd 和 chrony 可能會導致沖突,不能同時安裝
<br >
2. 配置 ntp 服務器:
~~~
# cp /etc/chrony.conf /etc/chrony.conf.bak
# vim /etc/chrony.conf
~~~
刪除默認的 ntp 服務器,改為阿里的 ntp 服務器:
~~~
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
server ntp4.aliyun.com iburst
server ntp5.aliyun.com iburst
server ntp6.aliyun.com iburst
server ntp7.aliyun.com iburst
~~~
<br >
3. 啟動 chronyd:
~~~
# service chronyd start
Redirecting to /bin/systemctl start chronyd.service
# chkconfig chronyd on
Note: Forwarding request to 'systemctl enable chronyd.service'.
~~~
<br >
4. 驗證 chronyd:
~~~
# chronyc 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
===============================================================================
^* 120.25.115.20 2 6 17 7 -27ms[ -137ms] +/- 617ms
^+ 203.107.6.88 2 6 17 9 -32ms[ -32ms] +/- 613ms
~~~
在返回的輸出中,^\* 表示首選的時間源。
~~~
# chronyc tracking
Reference ID : 78197314 (120.25.115.20)
Stratum : 3
Ref time (UTC) : Sat Jan 11 12:39:01 2020
System time : 0.000672388 seconds fast of NTP time
Last offset : -0.109685801 seconds
RMS offset : 0.109685801 seconds
Frequency : 0.000 ppm slow
Residual freq : -36916.840 ppm
Skew : 1000000.000 ppm
Root delay : 1.225557446 seconds
Root dispersion : 40.070934296 seconds
Update interval : 2.9 seconds
Leap status : Normal
~~~
<br >
## **常用命令**
### **檢查時鐘是否同步**
chronyc tracking 用于顯示系統時間信息。
```
$ chronyc tracking
Reference ID : 78197314 (120.25.115.20)
Stratum : 3
Ref time (UTC) : Wed Jan 15 01:03:05 2020
System time : 0.000222231 seconds fast of NTP time
Last offset : +0.000310285 seconds
RMS offset : 4079.027587891 seconds
Frequency : 6.416 ppm slow
Residual freq : +0.049 ppm
Skew : 4.128 ppm
Root delay : 0.036260299 seconds
Root dispersion : 0.002073948 seconds
Update interval : 129.9 seconds
Leap status : Normal
```
> Ref time (UTC) :來自參考源的 UTC 時間。
> System time:在正常的操作中,chronyd 絕不會步進系統時鐘,因為時鐘的跳躍可能會對某些應用程序產生不利的影響。取而代之的是,通過稍微加快或降低系統時鐘的速度來糾正系統時鐘中的錯誤,直到錯誤被消除,然后恢復系統時鐘的正常速度。在開發環境使用 VMWare 虛擬機的暫停和恢復功能時將很容易重現這個問題,如果你要立即步進同步時鐘,請使用 chronyc makestep 命令。
<br >
### **手動調整系統時鐘**
要立即步進同步系統時鐘,請使用以下命令:
~~~
# chronyc makestep
200 OK
~~~
> 注:如果步進同步系統時間沒有立即生效,你可以嘗試重啟服務器。
<br >
<br >
*****
# **參考資料**
* [USING CHRONY](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-using_chrony#sect-Checking_if_chrony_is_synchronized)