Rootop 服务器运维与web架构

linux ntp时间同步的两种方法

| 暂无评论

服务端IP 192.168.1.101      客户端IP 192.168.1.88

1、首先配置ntpd服务器:

vi /etc/ntp.conf

 配置文件中有restrict default语句

 #restrict default nomodify notrap noquery     #  默认允许所有可连接客户端ntpdate到本机   

restrict default ignore          #  默认所有客户端禁止ntpdate到本机

 #与上级服务端连续性同步时间,prefer表示优先,如无可不设置

#server 上级ntp服务器IP或者域名 [prefer] 

server 210.72.145.44 perfer

 #当之前设置了restrict default ignore的情况下,可以设置哪些客户可以ntpdate到本机

restrict 192.168.1.88 mask 255.255.255.255 nomodify notrap

#其余为可选设置,以默认值即可

开启ntpd服务

service ntpd start && chkconfig –level 345 ntpd on 

2、客户端配置:

此时有2种办法使客户端与服务器同步,一种是用ntpdate + crontab,另一种是客户端也开启ntpd服务

 ntpdate + crontab

#使用ntpdate同步一次时间,查看是否有正确回显

ntpdate 192.168.1.101

 #如为以下报错,一般是因为服务端才开启,需要等5分钟左右就好了

[root@test ~]# ntpdate 192.168.1.101
 5 Nov 05:25:51 ntpdate[3892]: no server suitable for synchronization found

#如显示以下信息表示成功

[root@test ~]# ntpdate 192.168.1.101
 7 Nov 22:34:59 ntpdate[4546]: step time server 192.168.1.101 offset 2.946644 sec

#这样就可以将ntpdate命令放入crontab中,定期执行

crontab -e

添加一条,如

* 2 * * * * ntpdate 192.168.1.101    #每天2点执行

保存即可

如有开启iptables,配置允许ntpd服务的udp协议端口123被访问。 

第二种同步方法

#使用ntpdate同步一次时间,查看是否有正确回显

ntpdate 192.168.1.101

#设置ntpd服务

vi /etc/ntp.conf

方法同上,但注意将设置的上级ntp server 设置为服务器的地址。

server 192.168.1.101 prefer

#设置开启服务后自动同步上级ntp server时间

vi /etc/ntp/step-tickers   加入一条

192.168.1.101

重启ntpd服务。

[root@test ~]# service ntpd restart
关闭 ntpd:                                                [确定]
ntpd: 同步时间服务器:                                     [确定]
同步硬件时钟到系统时钟                                     [确定]
启动 ntpd:                                                [确定]
You have new mail in /var/spool/mail/root
[root@localhost ~]#

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/1104.html

作者:Venus

服务器运维与性能优化

发表回复