chrony 已经成了事实标准,替代了ntp。

但是,有几个细节,需要非常注意。

给出我们的配置,/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 172.10.1.1 iburst prefer minpoll 6 maxpoll 10
server 172.10.1.2 iburst

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# Allow NTP client access from local network.
#allow 192.168.0.0/16

# Serve time even if not synchronized to a time source.
#local stratum 10

# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# Specify directory for log files.
logdir /var/log/chrony
logchange 0.5
# Select which information is logged.
#log measurements statistics tracking rtc

里面有好几个细节,下面逐一解释一下:

一、server

这里可以添加很多时间服务器,172.10.1.1 和 172.10.1.2 是两台自建的时间服务器。

ibust 会在 chrony 启动的2秒内,去快速poll服务器4次来快速矫正当前系统时间

prefer 优先使用指定的服务器

minpoll 6,缺省是6,意思是2的6次方,也就是64秒,最小轮询时间服务器的时间间隔是64秒

maxpoll 10,缺省是10,同上,2的10次方,也就是1024秒,最大轮询时间间隔是1024秒

通常情况下一过minpoll的时间周期,就会触发一次时间同步询问。

二、makestep

正常情况下如果系统时钟跟时间服务器不一致,chrony调整的方式是慢慢增加,或慢慢减少,不会一步到位,直接去跟时间服务器对齐。

makestep 1.0 3,意思就是如果时间服务器跟系统时间相差1秒,那么就在下3个时钟更新中追上时间服务器。

这样就会立刻快速追平了,这样会带来时间跳跃。

三、rtcsync

这个是把系统时钟同步到主板的硬件时钟去。

缺省情况下是11分钟同步一次

四、logchange

logchange 0.5,意思是如果chrony调整的系统时间,超过了0.5秒的时长,就会发一条消息到syslog,这样我们就能在/var/log/messages里看到这条消息了。

五、验证调试

开发人员会问,什么时候同步的服务器啊,多长时间同步一次,时间到底准不准啊,有没有发生跳跃啊

我们就用chronyc sources来验证,配置中

server 172.10.1.1 iburst prefer minpoll 4 maxpoll 5
server 172.10.1.2 iburst

Float left

解释一下,minpoll 4 maxpoll 5 ,那么最小轮询时间16秒,最大32秒。

我们可以看到上图 LastRx 就是上次询问时间服务器的间隔时间,14秒、15秒、16秒,然后就变1了,最小间隔16秒后,立即就询问时间服务器,同步时间。

同样可以看到第二台时间服务器就不受这个限制,缺省minpoll 6,就是64秒。所以上图第二台,63秒、64秒、65秒,变0,才去询问时间服务器。

总结一下,chrony 调整时间偏差是匀速的,缓慢的。它询问时间服务器的间隔由minpoll来控制。

我们用logchange来记录大的时间调整,以备追溯和查询。