在Ubuntu下用Chrony做主从机时间同步
主机
下载chrony
sudo apt install chrony
修改配置文件:
sudo gedit /etc/chrony/chrony.conf
# Welcome to the chrony configuration file. See chrony.conf(5) for more
# information about usuable directives.# This will use (up to):
# - 4 sources from ntp.ubuntu.com which some are ipv6 enabled
# - 2 sources from 2.ubuntu.pool.ntp.org which is ipv6 enabled as well
# - 1 source from [01].ubuntu.pool.ntp.org each (ipv4 only atm)
# This means by default, up to 6 dual-stack and up to 2 additional IPv4-only
# sources will be used.
# At the same time it retains some protection against one of the entries being
# down (compare to just using one of the lines). See (LP: #1754358) for the
# discussion.
#
# About using servers from the NTP Pool Project in general see (LP: #104525).
# Approved by Ubuntu Technical Board on 2011-02-08.
# See http://www.pool.ntp.org/join.html for more information.server 192.168.1.1 minpoll 4 maxpoll 10 iburst #将本地作为服务器allow all # 这里允许所有网段的客户端计算机发送的请求local stratum 10 #无法同步时,将本地的时间返回给客户端# This directive specify the location of the file containing ID/key pairs for
# NTP authentication.
keyfile /etc/chrony/chrony.keys# This directive specify the file into which chronyd will store the rate
# information.
driftfile /var/lib/chrony/chrony.drift# Uncomment the following line to turn logging on.
#log tracking measurements statistics# Log files location.
logdir /var/log/chrony# Stop bad estimates upsetting machine clock.
maxupdateskew 100.0# This directive enables kernel synchronisation (every 11 minutes) of the
# real-time clock. Note that it can’t be used along with the 'rtcfile' directive.
rtcsync# Step the system clock instead of slewing it if the adjustment is larger than
# one second, but only in the first three clock updates.
makestep 1 3
这里直接将主机的时间返回给从机,好处是主机和从机的时间同步效果更好,但是没有和外部时间同步服务器进行同步。如果需要和外部服务器同步的话,将server 192.168.1.1 minpoll 4 maxpoll 10 iburst删掉,并添加以下内容:
server ntp.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp1.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp1.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp10.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp11.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp12.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp2.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp2.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp3.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp3.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp4.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp4.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp5.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp5.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp6.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp6.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp7.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp8.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp9.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
修改完配置文件后,重启chrony服务
sudo service chrony restart
从机
配置文件:
# Welcome to the chrony configuration file. See chrony.conf(5) for more
# information about usuable directives.# This will use (up to):
# - 4 sources from ntp.ubuntu.com which some are ipv6 enabled
# - 2 sources from 2.ubuntu.pool.ntp.org which is ipv6 enabled as well
# - 1 source from [01].ubuntu.pool.ntp.org each (ipv4 only atm)
# This means by default, up to 6 dual-stack and up to 2 additional IPv4-only
# sources will be used.
# At the same time it retains some protection against one of the entries being
# down (compare to just using one of the lines). See (LP: #1754358) for the
# discussion.
#
# About using servers from the NTP Pool Project in general see (LP: #104525).
# Approved by Ubuntu Technical Board on 2011-02-08.
# See http://www.pool.ntp.org/join.html for more information.
server 192.168.31.20 minpoll 4 maxpoll 10 iburst # 这里192.168.31.20是主机的ip,要确保能ping通# 这里记得注释,否则从机会从外部服务器拿时间
#pool ntp.ubuntu.com iburst maxsources 4
#pool 0.ubuntu.pool.ntp.org iburst maxsources 1
#pool 1.ubuntu.pool.ntp.org iburst maxsources 1
#pool 2.ubuntu.pool.ntp.org iburst maxsources 2# This directive specify the location of the file containing ID/key pairs for
# NTP authentication.
keyfile /etc/chrony/chrony.keys# This directive specify the file into which chronyd will store the rate
# information.
driftfile /var/lib/chrony/chrony.drift# Uncomment the following line to turn logging on.
#log tracking measurements statistics# Log files location.
logdir /var/log/chrony# Stop bad estimates upsetting machine clock.
maxupdateskew 100.0# This directive enables kernel synchronisation (every 11 minutes) of the
# real-time clock. Note that it can’t be used along with the 'rtcfile' directive.
rtcsync# Step the system clock instead of slewing it if the adjustment is larger than
# one second, but only in the first three clock updates.
makestep 1 3 #时间偏差超过 1 秒,Chrony 将立即调整时间,最多连续调整 3 次
验证
在从机输入:
chronyc tracking
System time 表示的是与主机之间的时间差
Last offset 表示的是上一次时间调整的偏移量
Root delay 从根时间源到本地系统的延迟时间
Root dispersion 从根时间源到本地系统的最大时间偏差
Update interval Chrony 每隔多少秒会更新一次时间
^*表示正在用的服务器
LastRx是最后一次测量的偏移量 (+20us)
Last是上一次测量的偏移量 (+30us)
sample表示 Chrony 对时间源的测量精度范围。反映了 Chrony 在同步时间时的不确定性。