安装 chrony 服务
联网安装
sudo apt update
sudo apt -y install chrony
离线安装
- 从互联网下载安装包,不同的操作系统对应的安装包不同
- 安装
以 20.04 为例
sudo dpkg -i chrony_3.5-6ubuntu6.2.deb
在离线环境中,通常首选使用 dpkg 安装本地 .deb 软件包文件,因为您可能无法使用 apt 自动下载依赖项。但请注意,dpkg 不会自动解决依赖关系,因此您可能需要手动安装所有相关依赖项。
配置
然后编辑 /etc/chrony/chrony.conf
- 对于联网的服务器,指明上游时间源,例如
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
pool time.pool.aliyun.com iburst maxsources 4
pool cn.pool.ntp.org iburst maxsources 5
pool asia.pool.ntp.org iburst maxsources 5
pool ntp.neu.edu.cn iburst maxsources 2
pool time.edu.cn iburst maxsources 4
- 允许在初始化时,大幅修改系统时间。这可以加速本机时间与上游时间源的同步过程。
# Step the system clock instead of slewing it if the adjustment is larger than
# 1000 second, but only in the first 10 clock updates.
makestep 1000 10
- 允许客户端连接
allow 0.0.0.0/0
- 声称本机时间可信
local stratum 8
此时,一个完整的配置样例如下
# 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.
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
pool time.pool.aliyun.com iburst maxsources 4
pool cn.pool.ntp.org iburst maxsources 5
pool asia.pool.ntp.org iburst maxsources 5
pool ntp.neu.edu.cn iburst maxsources 2
pool time.edu.cn iburst maxsources 4
# 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
makestep 1000 10
allow 0.0.0.0/0
local stratum 8
- 重启chrony服务
sudo systemctl restart chrony
客户端
对应的chrony客户端安装方法及配置
sudo apt update
sudo apt -y install chrony
然后编辑 /etc/chrony/chrony.conf, 在默认配置尾部,追加server即可。它们可以是安装了ntp或是chrony,并监听udp端口123的服务器。
例如
server 192.168.30.201 iburst
server 192.168.1.102 iburst
server 192.168.43.253 iburst
重启chrony服务
sudo systemctl restart chrony