Linux虚拟机配置NTP服务
一 选择方案与准备
timedatectl status、timedatectl set-timezone Asia/Shanghaihwclock -w(在系统时间正确后执行)二 方案一 使用 chrony(RHEL/CentOS 8+/Rocky 8+ 推荐)
yum install -y chrony(或 dnf install -y chrony)systemctl enable --now chronyd/etc/chrony.conf:
server 行(若本机不向上级同步)allow 192.168.1.0/24(按实际网段修改)local stratum 10(上级不可达时作为备用源)/etc/chrony.conf:将 server 指向服务器,例如:server 192.168.1.10 iburstfirewall-cmd --add-service=ntp --permanent && firewall-cmd --reloadchronyc tracking、chronyc sources -vtimedatectl(应看到 NTP enabled: yes、NTP synchronized: yes)三 方案二 使用 NTP(ntpd,RHEL/CentOS 7 常见)
yum install -y ntpsystemctl enable --now ntpd/etc/ntp.conf(示例)
restrict default kod nomodify notrap nopeer noqueryrestrict -6 default kod nomodify notrap nopeer noqueryrestrict 127.0.0.1、restrict -6 ::1restrict 192.168.1.0 mask 255.255.255.0 nomodify notrapserver 0.centos.pool.ntp.org iburstserver 1.centos.pool.ntp.org iburstserver 2.centos.pool.ntp.org iburstserver 3.centos.pool.ntp.org iburstserver 127.127.1.0、fudge 127.127.1.0 stratum 10/etc/ntp.conf
server 192.168.1.10 iburstfirewall-cmd --add-service=ntp --permanent && firewall-cmd --reloadss -lunpt | grep 123(应看到 ntpd 监听 UDP 123)ntpq -p(关注 */+ 标记的同步源)timedatectl四 常见问题与排障
local stratum 10 或 fudge 127.127.1.0 stratum 10),避免客户端失步。restrict 规则是否阻止了客户端访问(如缺少 nomodify notrap 的网段允许项)。