我有两台服务器分别为:
机器1-172.24.223.28(没有外网)
机器2-172.24.218.154(有外网)
方案一:将机器2配置为NTP服务器
1.将机器2-172.24.218.154配置为NTP服务器
# 安装chrony(如果还没安装)
yum install -y chrony
# 备份原配置
cp /etc/chrony.conf /etc/chrony.conf.bak
# 编辑配置文件
vi /etc/chrony.conf
在配置文件中添加/修改以下内容:
# 允许内网网段访问(根据您的实际网段调整)
allow 172.24.0.0/16
# 或者允许所有内网访问
allow 172.24.218.0/24
# 使用外网可靠时间源
server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
# 如果无法访问外网,可以使用本地硬件时钟作为备份
# local stratum 10
重启chronyd服务:
systemctl restart chronyd
systemctl enable chronyd
# 开放防火墙端口
firewall-cmd --add-service=ntp --permanent
firewall-cmd --reload
# 或者直接开放UDP 123端口
firewall-cmd --add-port=123/udp --permanent
firewall-cmd --reload
2.在没有外网的机器1-172.24.223.28上配置NTP客户端
# 停止chronyd服务
systemctl stop chronyd
# 备份原配置
cp /etc/chrony.conf /etc/chrony.conf.bak
# 编辑配置文件
vi /etc/chrony.conf
清空原有server行,添加:
# 使用内网NTP服务器
server 172.24.218.154 iburst
# 注释掉其他所有server行
# 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
3.测试和同步
# 重启chronyd服务
systemctl start chronyd
# 立即同步时间
chronyc makestep
# 检查同步状态
chronyc sources -v
chronyc tracking
# 查看时间
date
方案二:使用ntpdate手动同步(临时方案)
# 安装ntpdate(如果没安装)
yum install -y ntpdate
# 从172.24.218.154同步时间
ntpdate -u 172.24.218.154
# 将系统时间写入硬件时钟
hwclock -w