独立 ntpd 不计时

独立 ntpd 不计时

ntpdate程序几年前就被弃用了。我正在尝试将旧代码从ntpdate独立ntpd目标是依靠NTTP 协议服务(使用 -g 运行)来规范时钟,但如果时钟出现严重错误(可能是由于长时间但暂时无法使用),则允许脚本强制时间同步NTTP 协议守护进程无法访问时间服务器)并且守护进程无法修复它。

service ntp stop
ntpd -gq
service ntp start

我们目前正在使用ntpdate出于这个目的,没有问题。我们期望“ntpd -gq”能够同步时间,无论时间差异有多大——但它在 ub14.04.4 LTS 中不起作用。它报告时间偏差为 0 秒,并且退出时没有错误。

完全相同的方法在 ub10.04 LTS 中按预期工作。

美国同步时间协议不在我们的应用范围内。我不知道该怎么做,除了回到ntpdate

非常感谢您提供有用的建议。谢谢。

接下来是我的ub14和ub10系统的信息,以及我的小测试的日志,可以总结为两个简单的步骤:

  1. 将时间往后推一个小时左右。
  2. 尝试修复它ntpd-gq 命令

(抱歉,信息太多了。不过,好过不够,对吧?)

`#======================`  
`# UB14 VERSION DETAILS`  
`#======================`  
`root@goncharova14:~# lsb_release -d ; uname -a`  
`Description:    Ubuntu 14.04.4 LTS`  
`Linux goncharova14 4.2.0-27-generic #32~14.04.1-Ubuntu SMP Fri Jan 22   15:32:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux`

`#--------------------`  
`# NTP QUERY of PEERS`  
`#--------------------`  
`root@goncharova14:~# ntpq -p`  
`    remote           refid      st t when poll reach   delay   offset    jitter`
  `==============================================================================`
` golem.canonical 193.79.237.14    2 u    2   64    7   81.242  3459840   2446479`
`*LOCAL(0)        .LOCL.          13 l    3   64    7    0.000    0.000     0.000`

`grep -v -e ^$ -e ^# /etc/ntp.conf /etc/default/ntp`  
`#---------------------------`  
`# NTP.CONF and NTP DEFAULTS`  
`#---------------------------`  
`/etc/ntp.conf:`  

`server ntp.ubuntu.com prefer`  
`server 127.127.1.0`  
`fudge 127.127.1.0 stratum 13`  
`driftfile /var/lib/ntp/ntp.drift`  
`statistics loopstats peerstats clockstats`  
`filegen loopstats file loopstats type day enable`  
`filegen peerstats file peerstats type day enable`  
`filegen clockstats file clockstats type day enable`  
`restrict -4 default kod notrap nomodify nopeer noquery`  
`restrict -6 default kod notrap nomodify nopeer noquery`  
`restrict 127.0.0.1`  
`restrict ::1`  

`/etc/default/ntp:`  

`NTPD_OPTS='-g'`  

`#---------------------------------`  
`# PUSH THE TIME BACK && TEST SYNC`  
`#---------------------------------`  

`root@goncharova14:~# date; date 02272000`  
`Mon Feb 27 20:57:39 EST 2017`  
`Mon Feb 27 20:00:00 EST 2017`  

`root@goncharova14:~# service ntp stop && (ntpd -gq ; echo "Status: $?";   date ; echo '---SYSLOG---' ; tail -n 15 /var/log/syslog; echo '---endLOG---' ; date)`  
` * Stopping NTP server ntpd     [ OK ]`  
`ntpd: time slew +0.000000s`  
`Status: 0`  
`Mon Feb 27 20:08:44 EST 2017`  
`---SYSLOG---`  
`Feb 27 20:07:51 goncharova14 ntpd[574]: peers refreshed`  
`Feb 27 20:07:51 goncharova14 ntpd[574]: Listening on routing socket on fd  #22 for interface updates`  
`Feb 27 20:07:53 goncharova14 ntpd[574]: ntpd: time slew +0.000000 s`  
`Feb 27 20:08:42 goncharova14 ntpd[1566]: ntpd [email protected] Thu Feb 11  18:30:40 UTC 2016 (1)`  
`Feb 27 20:08:42 goncharova14 ntpd[1566]: proto: precision = 0.160 usec`  
`Feb 27 20:08:42 goncharova14 ntpd[1566]: ntp_io: estimated max descriptors:   1024, initial socket boundary: 16`  
`Feb 27 20:08:42 goncharova14 ntpd[1566]: Listen and drop on 0 v4wildcard   0.0.0.0 UDP 123`  
`Feb 27 20:08:42 goncharova14 ntpd[1566]: Listen and drop on 1 v6wildcard ::   UDP 123`  
`Feb 27 20:08:42 goncharova14 ntpd[1566]: Listen normally on 2 lo 127.0.0.1   UDP 123`  
`Feb 27 20:08:42 goncharova14 ntpd[1566]: Listen normally on 3 eth0   10.160.74.140 UDP 123`  
`Feb 27 20:08:42 goncharova14 ntpd[1566]: Listen normally on 4 lo ::1 UDP   123`  
`Feb 27 20:08:42 goncharova14 ntpd[1566]: Listen normally on 5 eth0   fe80::a00:27ff:fecd:8c80 UDP 123`  
`Feb 27 20:08:42 goncharova14 ntpd[1566]: peers refreshed`  
`Feb 27 20:08:42 goncharova14 ntpd[1566]: Listening on routing socket on fd   #22 for interface updates`  
`Feb 27 20:08:44 goncharova14 ntpd[1566]: ntpd: time slew +0.000000 s`  
`---endLOG---`  
`Mon Feb 27 20:08:44 EST 2017`  

  `#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++`  
`# THE SAME APPROACH WORKS AS EXPECTED IN UB10. Commands are identical. Conf   files are identical. +`
  `#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++`  

`#======================`  
`# UB10 VERSION DETAILS`  
`#======================`  
`root@goncharova10:~# lsb_release -d ; uname -a ; echo`  
`Description:    Ubuntu 10.04 LTS`  
`Linux goncharova10 2.6.32-21-generic #32-Ubuntu SMP Fri Apr 16 08:10:02 UTC   2010 i686 GNU/Linux`

`#--------------------`  
`# NTP QUERY of PEERS`  
`#--------------------`  
`root@goncharova10:~# ntpq -p ; echo`  
`    remote           refid      st t when poll reach   delay   offset    jitter`
  `==============================================================================`
`*chilipepper.can 17.253.34.125    2 u  238  512  377   78.643   -2.038     1.020`
` LOCAL(0)        .LOCL.          13 l   22   64  377    0.000    0.000     0.001`

`#---------------------------`  
`# NTP.CONF and NTP DEFAULTS`  
`#---------------------------`  
`root@goncharova10:~# grep -v -e ^$ -e ^# /etc/ntp.conf /etc/default/ntp`  

`/etc/ntp.conf:`  

`server ntp.ubuntu.com prefer`  
`server 127.127.1.0`  
`fudge 127.127.1.0 stratum 13`  
`driftfile /var/lib/ntp/ntp.drift`  
`statistics loopstats peerstats clockstats`  
`filegen loopstats file loopstats type day enable`  
`filegen peerstats file peerstats type day enable`  
`filegen clockstats file clockstats type day enable`  
`restrict -4 default kod notrap nomodify nopeer noquery`  
`restrict -6 default kod notrap nomodify nopeer noquery`  
`restrict 127.0.0.1`  
`restrict ::1`  

`/etc/default/ntp:`  

`NTPD_OPTS='-g'`  

`#---------------------------------`  
`# PUSH THE TIME BACK && TEST SYNC`  
`#---------------------------------`  

`root@goncharova10:~# date; date 02272000`  
`Mon Feb 27 21:10:03 EST 2017`  
`Mon Feb 27 20:00:00 EST 2017`  

`root@goncharova10:~# service ntp stop && (ntpd -gq ; echo "Status: $?";   date ; echo '---SYSLOG---' ; tail -n 15 /var/log/syslog; echo '---endLOG---' ; date)`  
` * Stopping NTP server ntpd [ OK ]`  
`ntpd: time set +4203.430466s`  
`Status: 0`  
`Mon Feb 27 21:10:12 EST 2017`  
`---SYSLOG---`  
`Feb 27 20:00:00 goncharova10 ntpd[25264]: ntpd exiting on signal 15`  
`Feb 27 20:00:00 goncharova10 ntpd[31309]: ntpd [email protected] Sat Dec 20   11:53:03 UTC 2014 (1)`  
`Feb 27 20:00:00 goncharova10 ntpd[31309]: precision = 1.000 usec`  
`Feb 27 20:00:00 goncharova10 ntpd[31309]: ntp_io: estimated max   descriptors: 1024, initial socket boundary: 16`  
`Feb 27 20:00:00 goncharova10 ntpd[31309]: Listening on interface #0   wildcard, 0.0.0.0#123 Disabled`  
`Feb 27 20:00:00 goncharova10 ntpd[31309]: Listening on interface #1   wildcard, ::#123 Disabled`  
`Feb 27 20:00:00 goncharova10 ntpd[31309]: Listening on interface #2 lo,   127.0.0.1#123 Enabled`  
`Feb 27 20:00:00 goncharova10 ntpd[31309]: Listening on interface #3 eth0,   10.160.74.40#123 Enabled`  
`Feb 27 20:00:00 goncharova10 ntpd[31309]: Listening on interface #4 lo,   ::1#123 Enabled`  
`Feb 27 20:00:00 goncharova10 ntpd[31309]: Listening on interface #5 eth0,   fe80::20c:29ff:feeb:5b07#123 Enabled`  
`Feb 27 20:00:00 goncharova10 ntpd[31309]: kernel time sync status 2040`  
`Feb 27 20:00:00 goncharova10 ntpd[31309]: frequency initialized -14.511 PPM   from /var/lib/ntp/ntp.drift`  
`Feb 27 21:10:12 goncharova10 ntpd[31309]: synchronized to 91.189.89.198,   stratum 2`  
`Feb 27 21:10:12 goncharova10 ntpd[31309]: time reset +4203.430466 s`  
`---endLOG---`  
`Mon Feb 27 21:10:12 EST 2017`  

答案1

您是否有防火墙或其他阻止 UDP 流量的设备?ntp 要求双向打开 UDP 端口 123。在 UDP 端口 123 上执行 tcpdump 匹配:

sudo tcpdump -i wlan0 udp port 123

用您主要的面向互联网的接口替换上面的 wlan0。

然后重启 ntp 服务。你应该看到类似下面的内容(地址可能有所不同):

22:34:16.962180 IP 10.10.1.150.ntp > muug.ca.ntp: NTPv4, Client, length 48
22:34:16.999362 IP muug.ca.ntp > 10.10.1.150.ntp: NTPv4, Server, length 48
22:34:17.962186 IP 10.10.1.150.ntp > host.hosttechnet.com.ntp: NTPv4, Client, length 48
22:34:17.988816 IP host.hosttechnet.com.ntp > 10.10.1.150.ntp: NTPv4, Server, length 48
22:34:18.962187 IP 10.10.1.150.ntp > ntp2.torix.ca.ntp: NTPv4, Client, length 48
22:34:18.980295 IP ntp2.torix.ca.ntp > 10.10.1.150.ntp: NTPv4, Server, length 48
22:34:19.962194 IP 10.10.1.150.ntp > time.srv.ualberta.ca.ntp: NTPv4, Client, length 48
22:34:20.019589 IP time.srv.ualberta.ca.ntp > 10.10.1.150.ntp: NTPv4, Server, length 48
22:34:20.962167 IP 10.10.1.150.ntp > golem.canonical.com.ntp: NTPv4, Client, length 48
22:34:21.061641 IP golem.canonical.com.ntp > 10.10.1.150.ntp: NTPv4, Server, length 48

相关内容