分布式系统集群的时间同步_李孟_新浪博客

将node1配置成一个time server,修改/etc/ntp.conf,
[root@node1 ~]vi /etc/ntp.conf
其他的配置不怎么需要改,只需要关注restrict的配置:

1. 注释掉原来的restrict default ignore这一行,这一行本身是不响应任何的ntp更新请求,其实也就是禁用了本机的ntp server的功能,所以需要注释掉。

2. 加入下面3行: 

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap(注释:用于让192.168.1.0/24网段上的机器能和本机做时间同步)
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10

后两行是让本机的ntpd和本地硬件时间同步。

当然,我们也可以添加server xxx.xxx.xxx.xxx,让他和其他的time server时间同步。

4. /etc/init.d/ntpd restart或者 service ntpd restart

5. chkconfig ntpd on 设置开机自启动

6. 修改iptables配置,将tcp和udp 123端口开放,这是ntp需要的端口,在/etc/services中可以查到这个端口。


第三步,
这样node1就成为一台time server了,现在我们配置node2这台机器(这里我们用定时任务来定时同步时间)

首先关掉这台机器上的ntpd服务:

service ntpd stop(本次关掉) 

chkconfig ntpd off(再关掉开机自启动);

centos7:
systemctl start  ntpd.service
systemctl enable ntpd.service

否则会报错:

[root@sv1 logs]# ntpdate node1
 1 Sep 20:09:19 ntpdate[2614]: the NTP socket is in use, exiting
好,开始写定时任务:

[root@sv1 logs]# crontab -e
#synchronize time with node1
*/5 * * * * /usr/sbin/ntpdate nimbus >/dev/null 2>&1

意思是每5分钟同步一次时间;

systemctl enable ntpd
systemctl status 
如是使用systemctl is-enabled chronyd来查看一下,往往得到的结果是chrony已经被设置为enabled。 ntpd

systemctl disable chronyd
posted @ 2017-04-13 16:34  Dlimeng  阅读(11)  评论(0)    收藏  举报  来源