chrony时间服务器

Chrony 适合用来搭建局域网内的时间同步服务。服务端对外提供 NTP,同一内网中的服务器再统一从它同步时间,能减少外部网络不稳定对时间同步的影响。

安装

apt install chrony

配置

服务端配置放在 chrony.conf 中,重点关注允许访问的网段、上游 NTP 源和本机是否允许作为时间源。

chrony.conf
# Welcome to the chrony configuration file. See chrony.conf(5) for more
# information about usable directives.

# Include configuration files found in /etc/chrony/conf.d.
confdir /etc/chrony/conf.d

# Use Debian vendor zone.
pool ntp.ntsc.ac.cn iburst
pool cn.ntp.org.cn iburst

# Use time sources from DHCP.
sourcedir /run/chrony-dhcp

# Use NTP sources found in /etc/chrony/sources.d.
sourcedir /etc/chrony/sources.d

# 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

# Save NTS keys and cookies.
ntsdumpdir /var/lib/chrony

# 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

# Get TAI-UTC offset and leap seconds from the system tz database.
# This directive must be commented out when using time sources serving
# leap-smeared time.
leapsectz right/UTC

# 允许做为服务端
allow 0.0.0.0/0
# 监听所有地址
bindaddress 0.0.0.0
bindcmdaddress 0.0.0.0

客户端配置

客户端可以继续使用系统自带的 systemd-timesyncd,只需要把 NTP 地址改为内网 Chrony 服务端地址。

sed -i 's/^#NTP=.*/NTP=192.168.1.1/' /etc/systemd/timesyncd.conf
systemctl restart systemd-timesyncd
timedatectl set-timezone Asia/Shanghai
timedatectl set-local-rtc 0
timedatectl
Tip

修改完成后可以通过 timedatectl timesync-status 查看当前同步源和同步状态。生产环境建议至少准备两个可用时间源,避免单点故障。