【学习笔记】NTP时间同步验证
chronyd服务作为服务器:
检查chronyd服务(Ubuntu默认)sudo systemctl status chronyd
查看chronyd是否开机自启 sudo systemctl is-enabled chronyd
配置文件:sudo vim /etc/chrony/chrony.conf
添加允许客户端同步规则:
allow 192.168.0.1/24 # 替换为设备所在的网段或具体IP
查看防火墙开放端口:sudo ufw status 一般时间同步为123端口
开放端口: sudo ufw allow 123/udp sudo ufw reload
服务器上查看有哪些客户端连接NTP服务器:192.168.0.9目前正是主控systemd-timesyncd正在连接
$ sudo chronyc clients
Hostname NTP Drop Int IntL Last Cmd Drop Int Last
===============================================================================
localhost 0 0 - - - 2 0 8 529
192.168.0.9 6 0 7 - 47 0 0 - -
systemd-timesyncd作为客户端:
systemd-timesyncd是否开启:sudo systemctl status systemd-timesyncd
开启服务:sudo systemctl start systemd-timesyncd
开机自启动:sudo systemctl enable systemd-timesyncd
开启同步功能:sudo timedatectl set-ntp true
查看同步时间有没有开启:timedatectl status
时钟服务配置文件路径 /etc/systemd/timesyncd.conf:
[Time]
NTP=0.debian.pool.ntp.org #NTP时钟服务器地址,域名或IP均可
查看同步日志:sudo journalctl -u systemd-timesyncd
说明同步成功:
~# sudo journalctl -u systemd-timesyncd
-- Logs begin at Tue 2025-08-05 20:49:46 CST, end at Tue 2025-08-05 21:18:55 CST. --
Aug 05 21:02:10 novastar systemd-timesyncd[22776]: Synchronized to time server 203.107.6.88:123 (ntp.aliyun.com).
Aug 05 21:11:51 novastar systemd-timesyncd[11626]: Synchronized to time server 192.168.0.10:123 (192.168.0.10).
Aug 05 21:13:39 novastar systemd-timesyncd[15592]: Synchronized to time server 192.168.0.10:123 (192.168.0.10).