| 1. 安装NTP |
安装NTP服务软件包 |
yum install -y ntp
|
| 2. 初次同步 |
在启动ntpd服务前,先手动与时间服务器同步一次,避免初始时间差过大 |
ntpdate ntp1.aliyun.com
|
| 3. 配置NTP |
编辑配置文件,指定可靠的时间服务器
关键修改:替换NTP服务器列表
|
编辑配置文件:
vi /etc/ntp.conf
修改NTP服务器配置:
# 注释掉原有默认配置:
#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
# 使用阿里云NTP服务器:
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
|
| 4. 启动服务 |
启动ntpd服务并设置为开机自启 |
# 启动NTP服务
service ntpd start
# 设置开机自启
chkconfig ntpd on
|
| 5. 防火墙 |
如果启用了iptables防火墙,需放行NTP流量 |
# 添加防火墙规则允许NTP流量
iptables -I INPUT -p udp --dport 123 -j ACCEPT
# 保存防火墙规则
service iptables save
命令解释:添加一条iptables规则,允许UDP协议、目标端口123的入站流量
|
| 6. 验证状态 |
等待几分钟后,检查同步状态 |
# 查看同步状态
ntpstat
# 查看时间源详情
ntpq -p
|