251118-NTP-Ubuntu时间同步(chrony)

Ubuntu 20.04 客户端指向内网 NTP 服务器

执行脚本

1
2
3
4
5
6
7
8
sudo vim /etc/chrony/chrony.conf
server 192.168.0.100 iburst
sudo systemctl restart chrony
# 强制一步到位
sudo chronyc -a makestep

# 查看是否已指向 192.168.0.100 且出现 * 号
chronyc sources -v

在 Ubuntu 20.04 上,若要让系统只通过内网 IP 地址与内部 NTP 服务器同步时间,最简、最稳的做法是:
① 停用/禁用自带 timesyncd → ② 安装 chrony → ③ 在配置文件里只写内网 server 地址 → ④ 重启服务并验证。
下面给出完整命令,复制即可用(假设内网 NTP-Server IP 为 192.168.0.100)。


  1. 停用 timesyncd(防止冲突)

1
2
sudo systemctl stop systemd-timesyncd
sudo systemctl disable systemd-timesyncd

  1. 安装并启用 chrony(推荐,轻量、抗网络抖动)

1
2
3
sudo apt update
sudo apt install -y chrony
sudo systemctl enable --now chrony

  1. 指定内网服务器

1
sudo vim /etc/chrony/chrony.conf

清空原有 server 行,只保留(或新增):

1
server 192.168.0.100 iburst

保存后执行:

1
sudo systemctl restart chrony

  1. 立即强制同步 & 验证

1
2
3
4
5
# 强制一步到位
sudo chronyc -a makestep

# 查看是否已指向 192.168.0.100 且出现 * 号
chronyc sources -v

输出示例:

1
^* 192.168.0.100   3   6   377   45   +2us[  +2us] +/-  3ms

看到 ^*只通过内网


  1. 防火墙(可选)

如果本机启用了 UFW,需放行 UDP 123:

1
sudo ufw allow out 123/udp

  1. 客户端批量脚本(任意 Ubuntu 20.04)

1
2
3
4
5
6
7
8
#!/bin/bash
srv=192.168.0.100
sudo systemctl stop systemd-timesyncd && sudo systemctl disable systemd-timesyncd
sudo apt update && sudo apt install -y chrony
echo "server $srv iburst" | sudo tee /etc/chrony/chrony.conf
sudo systemctl enable --now chrony
sudo chronyc -a makestep
chronyc sources -v

保存为 set-ntp.shchmod +x set-ntp.sh && ./set-ntp.sh 即可。

至此,Ubuntu 20.04 将通过内网地址 192.168.0.100 完成时间同步,不再访问外部 NTP 源