2

Suppose the machine has an ntp.conf file that looks like this:

driftfile <path-to-drift-file>
server <NTP-server-1>
server <NTP-server-2>
server <NTP-server-2>

For some reason, let us say that the NTP server is not running at the first query to all servers. Can we make ntpd reiterate querying these sources (i.e. again consult server-1 to server-3 in a loop)? How do we do it?

Edit: Is there any way to quantitatively determine which server caused the actual time sync from the list of servers given in the ntp.conf in the machine?

Arpith
  • 274
  • 6
  • 17

2 Answers2

6

All defined servers in /etc/ntp.conf are used to synchronize time. There's no need to have it "loop" through the servers as the algorithm already handles multiple sources.

The ntpd program operates by exchanging messages with one or more configured servers at designated poll intervals.

From: man ntpd

You can see this by executing ntpq -p on the command-line to show your peers and their status.

You might see output like shown here:

  remote           refid      st when poll reach   delay  offset    disp
========================================================================
+128.4.2.6    132.249.16.1     2  131  256  373     9.89   16.28   23.25
*128.4.1.20   .WWVB.           1  137  256  377   280.62   21.74   20.23
-128.8.2.88   128.8.10.1       2   49  128  376   294.14    5.94   17.47
+128.4.2.17   .WWVB.           1  173  256  377   279.95   20.56   16.40

The output is explained in the man pages, too. But, over time I collected some notes:

remote: peers specified in the ntp.conf file
* = current time source
# = source selected, distance exceeds maximum value
o = source selected, Pulse Per Second (PPS) used
+ = source selected, included in final set
x = source false ticker
. = source selected from end of candidate list
- = source discarded by cluster algorithm
blank = source discarded high stratum, failed sanity

refid: remote source’s synchronization source

stratum: stratum level of the source

t: types available
l = local (such as a GPS, WWVB)
u = unicast (most common)
m = multicast
b = broadcast
- = netaddr

when: number of seconds passed since last response

poll: polling interval, in seconds, for source

reach: indicates success/failure to reach source, 377 all attempts successful

delay: indicates the round trip time, in milliseconds, to receive a reply

offset: indicates the time difference, in milliseconds, between the client server and source

disp/jitter: indicates the difference, in milliseconds, between two samples

Finally, to answer the last question;

Is there any way to quantitatively determine which server caused the actual time sync from the list of servers given in the ntp.conf in the machine?

The host indicated with the (*) is your currently selected time source. This can change during polling.

Aaron Copley
  • 12,525
  • 5
  • 47
  • 68
  • Wow! Amazingly detailed answer. Not that the other answers were any less correct. Thanks guys. :-) – Arpith Oct 04 '12 at 18:51
0

Ntpd will, at startup, query all configured server and pick the one with the lowest stratum. If that one fails, it will automatically fall over to the next server. So why would you want to change that?

Wikipedia: Network Time Protocol - Clock strata

Edit: After checking around I found some information about the iburst parameter, which is actually intended to speed up the clock-synchronization. The difference here is, that it will make the ntpd-server to exit when it can't reach any of the server. You could abuse this in a way to make sure that an instance of ntpd is running at all times, for instance by using a watchdog or a simple script ran by cron every now and then.

Unfortunately I couldn't figure out what the default behaviour of ntpd is when no server is reachable; although I found a lot of references about how it will behave when the servers don't resolve in DNS (e.g. this bug) or when the interface goes down.

Alexander Janssen
  • 2,607
  • 16
  • 21
  • I do not want to change that. I want to recursively query the same sources if querying all sources fails. – Arpith Oct 03 '12 at 19:25
  • Ah, you mean if ntpd can't reach any of those it should continue querying until it reaches one host? – Alexander Janssen Oct 03 '12 at 19:27
  • Was that not apparent in the question itself? – Arpith Oct 03 '12 at 19:28
  • I'm sorry, no, it's wasn't apparent. But let me check the documentation how this is being done. – Alexander Janssen Oct 03 '12 at 19:31
  • 2
    If the server isn't reachable when ntpd starts, it will just keep trying. The frequency depends on your polling settings. There is no loop. ntpd uses ALL the servers all the time. – Zoredache Oct 03 '12 at 19:33
  • "There is no loop. ntpd uses ALL the servers all the time": how is this possible? You mean to say ntpd sets up as many sockets as the server addresses given in the ntp.conf file? – Arpith Oct 03 '12 at 19:37
  • 1
    Yes ntpd will check the time of all the servers you list. If you run the command `ntpq -p` you will see a list of all the servers. The `poll` column lists how frequently the time is checked the `when` column is the time until the next check, both values are in seconds. Keep in mind that NTP is mostly **UDP** based. It doesn't need to do any kind of connection setup. – Zoredache Oct 03 '12 at 20:33
  • @Zoredache: So you mean to say that all the servers are queried in parallel and simultaneously? What if the server addresses mentioned are hostnames and not IP addresses? – Arpith Oct 04 '12 at 10:59
  • @Zoredache is correct. From the `ntpd` [man page](http://linux.die.net/man/8/ntpd): `The ntpd program operates by exchanging messages with one or more configured servers at designated poll intervals.` – Aaron Copley Oct 04 '12 at 11:16
  • @Arpith Queries are not parallel and simultaneous. From the `ntpd` [man page](http://linux.die.net/man/8/ntpd): `In order to protect the network from bursts, the initial poll interval for each server is delayed an interval randomized over a few second.` – Aaron Copley Oct 04 '12 at 11:19
  • @Zoredache: Can this random initial poll be dropped to incorporate sequential querying of the server sources mentioned in the ntp.conf file (the initial poll I mean)? – Arpith Oct 04 '12 at 11:30
  • Why do you think you would need something like that? What purpose would it serve? – Zoredache Oct 04 '12 at 16:14
  • By giving a sequential initial poll I can override the behaviour of ntpd by which it connects to the lowest stratum. I could give a server that is much higher in terms of the stratum and it would be queried first rather than the present approach. – Arpith Oct 04 '12 at 19:28
  • You can set a server as "preferred." That's about it.. – Aaron Copley Oct 05 '12 at 11:39