在上篇看了第一种使用Python 同步Linux 系统时间的方法, 参考:
Python 同步Linux 系统时间方法一 ---- 利用其他网站时间进行同步
http://www.cndba.cn/account/article/details/157http://www.cndba.cn/dave/article/158
这里看第二种方法,直接使用ntpdate命令命令来修改,使用该命令的好处就是如果是内网环境,可以参考其他服务器进行时间同步,或者公网的NTP IP地址进行同步。
1 Ntpdate使用说明
注意在使用ntpdate的时候,需要加上-u 选项,可以使用man ntpdate查看说明:http://www.cndba.cn/dave/article/158
-u Direct ntpdate to use an unprivileged port for outgoing packets. This is most useful when behind a firewall that blocks incoming traffic to privileged ports, and you want to synchronize with hosts beyond the firewall. Note that the -d option always uses unprivileged ports.
注意需要选择正确的NTP服务器,否则会出现如下错误:
[root@cndba.cn ~]# ntpdate -u 210.72.145.44
18 Feb 23:43:35 ntpdate[86548]: no server suitable for synchronization found[root@cndba.cn ~]# ntpdate 210.72.145.44
18 Feb 23:43:52 ntpdate[86615]: no server suitable for synchronization found
关于具体有哪些可用的NTP IP地址,可以直接从如下网址查看:
http://www.ntp.org.cn/
如:http://www.cndba.cn/dave/article/158
[root@cndba.cn ~]# ntpdate 202.108.6.95
18 Feb 16:11:07 ntpdate[90393]: adjust time server 202.108.6.95 offset -0.021017 sec
[root@cndba.cn ~]#
如果是本地机房的其他机器做NTP服务器,还需要配置并启动NTP Server,否则无法正常使用。
编辑NTP配置文件/etc/ntp.conf, 添加如下内容:
[root@cndba.cn ~]# cat /etc/ntp.conf
server 202.108.6.95
server 120.25.108.11
server 192.168.4.181 #本地时间
restrict 192.168.4.0 mask 255.255.255.0 nomodify (允许192.168.4.* 的IP 使用该时间服务器)
#中国国家受时中心地址
server 210.72.145.44 perfer
http://www.cndba.cn/dave/article/158http://www.cndba.cn/dave/article/158
#外部的时间服务器不可用时,以本地时间作为时间服务
server 127.127.1.0
http://www.cndba.cn/dave/article/158
fudge 127.127.1.0 stratum 10
当然这里也可以不配置,直接使用默认值启动。
启动ntpd 服务:http://www.cndba.cn/dave/article/158
[root@cndba.cn ~]# chkconfig ntpd on[root@cndba.cn ~]# chkconfig ntpd --list
ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@cndba.cn ~]#
[root@cndba.cn ~]# service ntpd start
Starting ntpd: [ OK ]
http://www.cndba.cn/dave/article/158
[root@cndba.cn ~]#
[root@web8 ~]# ntpstat
synchronised to local net at stratum 11
time correct to within 948 ms
polling server every 64 s
[root@web8 ~]#
[root@web8 ~]# ntpq -phttp://www.cndba.cn/dave/article/158
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 10 l 8 64 77 0.000 0.000 0.000
192.168.16.81 .INIT. 16 u - 64 0 0.000 0.000 0.000
192.168.16.80 .INIT. 16 u - 64 0 0.000 0.000 0.000
然后在其他机器上进行ntpdate 同步:
[root@cndba.cn ~]# ntpdate 192.168.4.181
18 Feb 16:21:43 ntpdate[134013]: step time server 192.168.4.181 offset -28723.065957 sec
注意:
如果重启ntp守护进程后,不能同步,并报如下错误:
[root@cndba.cn ~]# ntpdate -d 192.168.16.21
18 Feb 19:46:31 ntpdate[20517]: ntpdate 4.2.2p1@1.1570-o Tue Oct 25 12:54:51 UTC 2011 (1)
Looking for host 192.168.16.21 and service ntp
host found : 192.168.16.21
transmit(192.168.16.21)
receive(192.168.16.21)
transmit(192.168.16.21)
receive(192.168.16.21)
transmit(192.168.16.21)
receive(192.168.16.21)
transmit(192.168.16.21)
receive(192.168.16.21)
transmit(192.168.16.21)
192.168.16.21: Server dropped: strata too high
server 192.168.16.21, port 123
stratum 16, precision -20, leap 11, trust 000
refid [192.168.16.21], delay 0.02576, dispersion 0.00000
transmitted 4, in filter 4
reference time: 00000000.00000000 Thu, Feb 7 2036 14:28:16.000
originate timestamp: da702d96.33834125 Thu, Feb 18 2016 19:46:30.201
transmit timestamp: da702d97.a1899bf5 Thu, Feb 18 2016 19:46:31.631
filter delay: 0.02576 0.02579 0.02580 0.02582
0.00000 0.00000 0.00000 0.00000
filter offset: -1.42992 -1.42992 -1.42991 -1.42989
0.000000 0.000000 0.000000 0.000000
delay 0.02576, dispersion 0.00000
offset -1.429923
18 Feb 19:46:31 ntpdate[20517]: no server suitable for synchronization found[root@cndba.cn ~]#
这是因为每次重启NTP服务器之后大约要3-5分钟客户端才能与服务器建立正常的通讯连接,等待一会儿再更新就可以了。
如果还报错,可能就是防火墙的因素,ntpd 使用的udp的123 端口。 需要在防火墙里添加如下内容:
-A RH-Firewall-1-INPUT -m state --state NEW -m udp -p udp --dport 123 -j ACCEPT
2 Python 同步脚本
上节中讲了使用公网的NTP服务器和本地NTP 服务器的配置方法,有了IP地址之后,就可以用Python 进行同步了。
脚本很简短,如下:http://www.cndba.cn/dave/article/158
[root@cndba.cn ~]# cat updatetime.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Created on 2016-2-17
@author: dave, cndba.cn
"""
import os
def updatetime(host):
cmd='/usr/sbin/ntpdate -s ' + host
#print cmd
hw="/sbin/clock -w"
tm="/bin/date"
os.system(cmd)
# os.system(hw)
os.system(tm)
print("update successfully!")
updatetime('192.168.4.181')
对脚本赋权:
[root@cndba.cn ~]# chmod a+x updatetime.py
然后添加到crontab 就可以了,每天同步一次:
[root@cndba.cn ~]# crontab -l
0 1 * * * /root/updatetime.py >updatetime.log 2>&1 &[root@cndba.cn ~]#
版权声明:本文为博主原创文章,未经博主允许不得转载。
python