[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)