本文以CentOS7为例,演示查看、设置系统时间,并同步多台Slave机时间。
1. 查看系统时间:
date
2. 手动修改系统时间:
date -s "20240907 20:19:34"
date -s "年月日 时:分:秒"
3. 通过NTP服务同步网络时间:
安装 NTP 服务端/守护进程
yum -y install ntp
4. 运行如下步骤同步网络时间:
ntpdate 0.asia.pool.ntp.org
若该时间服务器不可用可尝试以下命令
ntpdate ntp.aliyun.com
ntpdate cn.pool.ntp.org
某些环境中,网络防火墙可能会阻止对低端口号的访问,则使用:
ntpdate -u 0.asia.pool.ntp.org
5. 将时间同步到硬件,防止重启后还原:
hwclock --systohc
6. 在Master机中修改ntp.conf文件:
首先进入配置文件后需要把restrict开头的行注释掉
写入生效的配置
restrict 192.168.90.0 mask 255.255.255.0 nomodify notrap
restrict [IP] mask [掩码] nomodify notrap
再把server开头的行注释掉,减少外部同步干扰,仅用本地时间作为源。
写入生效的配置
server 127.127.1.0
fudge 127.127.1.0 stratum 10
7. 在Master机中开启ntp服务,并设置开机自启动:
service ntpd restart
chkconfig ntpd on
8. 在Slave机中运行命令,同步Master时间:
ntpdate Master
以下是让集群自行同步Master时间的方法(长期运行时使用):
在Slave中运行crontab -e命令,在末行加入以下语句,实现每分钟同步一次,并将记录写入日志。
*/1 * * * * /usr/sbin/ntpdate Master >> /root/ntpdate.log 2>&1
频率 /usr/sbin/ntpdate Master机IP >> 日志路径 2>&1
最后通过cat /root/ntpdate.log查看同步状态。