1.检查ntp服务是否已经安装
sudo rpm -qa | grep ntp
显示已经安装ntp程序。如果安装过,应该有两个进程:
ntpdate… 用来和某台服务器同步
ntp… 用来提供时间同步服务
如果没有,就通过yarn install -y ntp
安装下
2.修改配置文件ntp.conf
vi /etc/ntp.conf
2.1 启用restrice
取消注释 restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap
并将网段改为集群的网段
restrict 172.16.30.2 mask 255.255.255.0 nomodify notrap
2.2 修改server域名配置
把默认的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
server 127.127.1.0
fudge 127.127.1.0 stratum 10
3 修改配置文件ntpd
vi /etc/sysconfig/ntpd
添加一行:SYNC_CLOCK=yes
# Drop root to id 'ntp:ntp' by default.
SYNC_HWCLOCK=yes
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
4.启动ntpd服务
service ntpd start
5.开机启动
sudo chkconfig ntpd on
6.其他服务器同步
ntpdate hostname
也可创建一个定时任务
crontab -e
*/10 * * * * /usr/sbin/ntpdate ntpdserver
7.查看时间
date "+%Y-%m-%d %H:%M:%S"