CentOS6.8搭建NTP服务器完整教程
准备工作
确保系统已连接网络并具备root权限,使用yum update
更新系统软件包。
安装NTP服务
yum install ntp -y
安装完成后验证版本:ntpd --version
配置NTP服务器
编辑配置文件:vi /etc/ntp.conf
# 添加国内公共NTP服务器 server ntp.ntsc.ac.cn iburst server cn.ntp.org.cn iburst # 允许内网客户端访问 restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # 启用本地时钟 server 127.127.1.0 fudge 127.127.1.0 stratum 10
防火墙设置
iptables -I INPUT -p udp --dport 123 -j ACCEPT
service iptables save
service iptables restart
启动NTP服务
service ntpd start
chkconfig ntpd on
ntpstat
客户端配置
Linux客户端执行:ntpdate 服务器IP
设置定时任务:crontab -e
添加:
*/30 * * * * /usr/sbin/ntpdate 服务器IP >/dev/null 2>&1
验证同步状态
ntpq -p
ntpstat
显示synchronised to NTP server
表示同步成功。
常见问题排查
- 服务启动失败:检查
/var/log/messages
日志 - 时间不同步:确认防火墙是否开放UDP 123端口
- 同步延迟大:尝试更换NTP服务器源