0

The ntpdate program was deprecated years ago. I am trying to transition old code from ntpdate to standalone ntpd. The goal is to rely on the ntp service (run with -g) to discipline the clock, but to allow a scripted forced time sync if the clock is significantly wrong (due, perhaps, to prolonged but temporary inability of the ntp daemon to access the time servers) and the daemon is unable to fix it.

service ntp stop
ntpd -gq
service ntp start

We are currently using ntpdate for that purpose, with no issues. We expect "ntpd -gq" to synch the time regardless of the size of time discrepancy -- but it does not work in ub14.04.4 LTS. It reports a time slew of 0 seconds, and exits without error.

The exact same approach works as expected in ub10.04 LTS.

The us of sntp is not an option in the scope of our application. I don't know what to do, other than go back to ntpdate.

Helpful advice will be appreciated IMMENSELY. Thank you.

What follows is information about my ub14 and ub10 systems, and logs of my little test, which can be summarized in two simple steps:

  1. Push the time back an hour or so.
  2. Try to fix it with ntpd -gq.

(Sorry if TMI. Better too much than not enough, right?)

`#======================`  
`# 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 4.2.6p5@1.2349-o 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 4.2.4p8@1.1612-o 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`  
0

Do you have firewalls or anything blocking UDP traffic? ntp requires UDP port 123 open bi-directionally. Do a tcpdump matching on UDP port 123:

sudo tcpdump -i wlan0 udp port 123

Replace wlan0 above with your main Internet-facing interface.

Then restart the ntp service. You should see something similar to the following (addresses may vary though):

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
  • Hello, TLin, thanks for the quick response. Each computer does run ufw. I don't think that's a problem. All appear to be sending and receiving ntp messages though port 123. I will post tcpdump in separate "answer" because the comment box is too small. :-) – Beatrix Goncharova Feb 28 '17 at 21:38
  • On second thought I won't "answer". Condensed output of tcpdump looks like (see next comment). goncharova10 is the ub10 box which behaves as expected. 10.160.44.41 is ub12 box which does not. 10.160.44.140 is ub14 box which does not. – Beatrix Goncharova Feb 28 '17 at 21:56
  • 15:47:46.695850 IP goncharova10.ntp > alphyn.canonical.com.ntp: NTPv4, Client, length 48 __ 15:47:46.718775 IP alphyn.canonical.com.ntp > goncharova10.ntp: NTPv4, Server, length 48 __ 15:48:01.560220 IP 10.160.44.41.ntp > juniperberry.canonical.com.ntp: NTPv4, Client, length 48 __ 15:48:01.640623 IP juniperberry.canonical.com.ntp > 10.160.44.41.ntp: NTPv4, Server, length 48 __ 15:48:30.716965 IP 10.160.44.140.ntp > lithium.constant.com.ntp: NTPv4, Client, length 48 __ 15:48:30.775966 IP lithium.constant.com.ntp > 10.160.44.140.ntp: NTPv4, Server, length 48 – Beatrix Goncharova Feb 28 '17 at 21:56
  • Sorry, that's horrible. I don't know how to format it any better. There seems to be no way to force newline in comment. – Beatrix Goncharova Feb 28 '17 at 22:02
  • Also of note: ntpq reports peer(s) as reachable. – Beatrix Goncharova Feb 28 '17 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.