1)查看ntp服务是否开启
systemctl is-enabled ntpd
2)启动ntp服务
systemctl start ntpd
3)配置开机启动
chkconfig ntpd on
4)设置当前master本地时间(例如)
date -s "2022-01-01 09:00:00"
或者同步网络时间(如果可以访问外网最好)
ntpdate -u 120.25.108.11
5) master 节点配置
vi /etc/ntp.conf
添加或修改以下内容
#设置 192.168.100.0-192.168.100.255 该网段上的所有服务器可以从这台服务器上同步时间
restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap
#集群在局域网中,不使用其他互联网上的时间
#注释以下内容:
#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
#当该节点丢失网络连接,依然可以采用本地时间作为时间服务器为其他节点提供时间同步
server 127.127.1.0
fudge 127.127.1.0 stratum 10
6)让硬件时间和系统时间一起同步
vi /etc/sysconfig/ntpd
SYNC_HWCLOCK=yes
7)所有子节点配置
systemctl stop ntpd
systemctl disable ntpd
crontab -e
编写定时命令,每十分钟同步更新一次
*/10 * * * * /usr/sbin/ntpdate master
其他补充:
重启服务
systemctl restart ntpd.service
查看硬件时间
hwclock --show
系统时间写入到硬件时间
hwclock --hctosys
参数说明:
–hctosys 将系统时钟调整为与目前的硬件时钟一致
–systohc 将硬件时钟调整为与目前的系统时钟一致
同步网络时间
中国
ntpdate -u cn.ntp.org.cn
中国教育网
ntpdate -u edu.ntp.org.cn
阿里云
ntpdate -u 120.25.108.11
ntpdate -u 182.92.12.11