1. 安装 ntp
# 确认是否已安装 ntp, 若只有 ntpdate,则需删除 ntpdate
[root@localhost ~]# rpm -qa|grep ntp
# 删除已安装 ntpdate
[root@localhost ~]# yum remove ntpdate –y
# 安装ntp
[root@localhost ~]# yum install ntp -y
2. 配置服务端
[root@localhost ~]# vi /etc/ntp.conf
# 如果集群是在一个封闭的局域网内,可以屏蔽掉默认的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
# 授权192.168.89.0网段上的所有机器可以从这台机器上查询和同步时间
restrict 192.168.89.0 mask 255.255.255.0 nomodify notrap
# 将本地时钟作为时间供给源,即便它失去网络连接,仍可以继续为网络提供服务;
server 127.127.1.0 #local clock
fudge 127.127.1.0 stratum 3
# 防火墙放开 123 端口
[root@localhost ~]# iptables -A INPUT -s 192.168.89.0/24 -p udp --dport 123 -j ACCEPT
[root@localhost ~]# iptables -A INPUT -p udp --dport 123 -j DROP
# 重启服务
[root@localhost ~]# systemctl stop ntpd.service
[root@localhost ~]# systemctl start ntpd.service
3. 配置客户端
# 修改/etc/ntp.conf,增加时间同步主节点
[root@localhost ~]# vi /etc/ntp.conf
server 192.168.89.121 prefer
# 重启服务,客户端从服务端同步时间
[root@localhost ~]# systemctl stop ntpd.service
[root@localhost ~]# systemctl start ntpd.service