Linux 部署 chrony 时间同步服务器

⏰安装 chrony

1 、检查 chrony 安装情况

1
dpkg -l | grep chrony

2 、安装 chrony

1
apt update && apt install chrony -y

⏰服务端 chrony 配置文件示例

1 、编辑配置文件

1
vim /etc/chrony/chrony.conf

2 、配置文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
cp /etc/chrony/chrony.conf /etc/chrony/chrony.conf-bak && cat > /etc/chrony/chrony.conf <<'EOF'
# 使用多个时间源进行同步
pool 10.200.0.2 iburst

# 记录时钟漂移文件
driftfile /var/lib/chrony/drift

# 设置最大偏差为1秒,允许最多三次步进修正
makestep 1.0 3

# 启用系统时钟与硬件实时时钟同步
rtcsync

# 启用硬件时间戳功能
hwtimestamp *

# 最少需要两个同步源进行时间校正
minsources 2

# 如果没有可用的时间源,使用本地时钟作为备用
local stratum 10

# 允许所有本地网络访问Chrony服务
allow 0.0.0.0/0

# 配置日志存储路径并启用日志记录
logdir /var/log/chrony
log measurements statistics tracking
EOF

# 设置 chrony 立即启动并开机自启
systemctl enable --now chrony && systemctl daemon-reload && systemctl restart chronyd

3 、重启 chrony

1
systemctl restart chronyd

⏰Chrony客户端安装与配置

1 、查看当前系统时间

1
date

2 、编辑 chrony 配置文件

1
vim /etc/chrony/chrony.conf

3 、配置文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
cp /etc/chrony/chrony.conf /etc/chrony/chrony.conf-bak && cat > /etc/chrony/chrony.conf <<'EOF'
# 欢迎使用 chrony 配置文件。有关可用指令的更多信息,请参阅 chrony.conf(5)。

# 包含在 /etc/chrony/conf.d 目录中找到的配置文件。
confdir /etc/chrony/conf.d

# 使用的 NTP 服务器
pool 2.pool.ntp.org iburst
pool ntp.ntsc.ac.cn iburst
pool ntp.sjtu.edu.cn iburst
pool s2g.time.edu.cn iburst
pool ntp.tencent.com iburst
pool ntp.aliyun.com iburst

# 使用来自 DHCP 的时间源。
sourcedir /run/chrony-dhcp

# 使用在 /etc/chrony/sources.d 中找到的 NTP 源。
sourcedir /etc/chrony/sources.d

# 此指令指定包含 NTP 身份验证的 ID/密钥对的文件位置。
# keyfile /etc/chrony/chrony.keys

# 此指令指定 chronyd 将存储速率信息的文件。
driftfile /var/lib/chrony/chrony.drift

# 保存 NTS 密钥和 cookie。
# ntsdumpdir /var/lib/chrony

# 取消注释以下行以打开日志记录。
log tracking measurements statistics

# 日志文件位置。
logdir /var/log/chrony

# 阻止错误的估计扰乱机器时钟。
maxupdateskew 100.0

# 此指令启用内核同步(每 11 分钟一次)实时时钟。注意,它不能与 'rtcfile' 指令一起使用。
rtcsync

# 如果调整超过一秒,则在前三个时钟更新中步进系统时钟,而不是缓慢调整。
makestep 1 3

# 从系统的 tz 数据库中获取 TAI-UTC 偏移量和闰秒。
# 使用提供闰秒平滑时间的时间源时,必须注释掉此指令。
leapsectz right/UTC
EOF

# 设置 chrony 立即启动并开机自启
systemctl enable --now chrony && systemctl daemon-reload && systemctl restart chronyd

4 、重启 chrony

1
systemctl restart chronyd

⏰可用的时间服务器地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# CentOS NTP Servers
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

# Apple NTP Servers
server time1.apple.com iburst
server time2.apple.com iburst
server time3.apple.com iburst
server time4.apple.com iburst
server time5.apple.com iburst

# Google NTP Servers
server time1.google.com iburst
server time2.google.com iburst
server time3.google.com iburst
server time4.google.com iburst

# Pool.ntp.org NTP Servers (Global)
server pool.ntp.org iburst

# China NTP Servers (Local)
server cn.pool.ntp.org iburst

# Aliyun NTP Servers
pool ntp1.aliyun.com iburst
pool ntp2.aliyun.com iburst
pool time1.aliyun.com iburst
pool time2.aliyun.com iburst

⏰chrony 常用命令

查看当前系统使用的NTP时间源的状态,包括延迟、同步质量等。

1
chronyc sources -v
  • 指示源的状态,*当前同步的源,+其他可接受的源

启动 chrony

1
systemctl start chronyd

重启 chrony

1
systemctl restart chronyd

重新加载 chrony 配置文件

1
systemctl daemon-reload

查看 chrony 时间同步状态

1
timedatectl status

查看 chrony 服务器状态

1
chronyc tracking

System time:表示系统时间与NTP时间的偏差。
RMS offset:表示时间同步的精度。
Update interval:表示时间同步的间隔。

设置 chrony 立即启动并开机自启

1
systemctl enable --now chrony

查看 chrony 占用端口

1
ss -ulwn
1
lsof -i :323

检查 chrony 服务运行状态

1
systemctl status chronyd

查看NTP源的详细统计信息

1
chronyc sourcestats

查看时间同步源的状态

1
chronyc activity

强制校准时间

1
chronyc -a makestep

开启网络时间同步

1
timedatectl set-ntp true

⏰卸载 chrony

1 、首先,使用以下命令停止chrony服务:

1
sudo systemctl stop chronyd

2 、卸载chrony并删除配置文件

1
sudo apt remove chrony && sudo apt purge chrony

3 、清理残留文件(可选)

1
sudo rm -rf /etc/chrony && sudo rm -rf /var/lib/chrony && sudo rm -rf /var/log/chrony

完成以上步骤后,chrony就从Debian系统中被完全卸载了。