Time, gentlemen, please – NTP

The Date & Time pane in System Preferences provides a friendly front-end to the NTP service.

Any minute now, you should be able to buy an Apple Watch, but ever since the original Apple I, its computers have been keeping time too.

Time is central information for many functions on your Mac. Every time that a file is created, changed, or opened, OS X dutifully records the event with its time and date. Every message posted to your logs bears a timestamp, each step which occurred the last time that you started your Mac up too.

Disconnect your Mac’s clock from the Internet, and it will roam free, a bit like country villages did before the arrival of railways. No-one worried too much if the church clock was a few minutes out, so long as everyone was in sync with it. In fact most villages used to listen for the church clock (or a small cannon) in the nearest town once a day and set their clock by that: a process similar to that used by OS X to keep your Mac on time too. The big difference is that in the country, areas ran on local solar time, which was more important to them. When railway services came, though, their timetables used national standard time, so everyone had to switch to that instead.

NTP

OS X uses Network Time Protocol (NTP) version 4, which provides the essential ntpd service. You may recall that just before Christmas 2014, Apple propagated a patch for a security vulnerability in NTP, the first to be applied automatically. Although you can still configure and access NTP through its raw Unix interface, OS X provides the Date & Time pane in System Preferences as an easier access point. There you can set the network time server against which to sync your Mac’s clock, which should be as much as you need to do to it.

When shut down, your Mac’s internal clock keeps track of time. Older Macs had clocks and PRAM backup which required significant power, so had batteries, which would go flat over time, and needed to be replaced periodically. The modern Mac’s clock gets by on almost no power at all, so flat batteries are no longer an issue unless your Mac is disconnected from mains power for a long time.

Startup

When you start your Mac up, system time is initially set to that of the internal clock. The NTP service then tries to connect to the designated time server, and asks it for the time. If the result differs substantially from that of the internal clock, NTP should correct the clock. This may occur by means a series of small corrections over a period, or one larger change: NTP uses a sophisticated algorithm to determine that, and to allow for delays that occur with transmission over the Internet. Periodically thereafter ntpd will check the network time server, correcting the clock as it needs, to achieve an overall accuracy of around 0.1 second.

The Date & Time pane in System Preferences provides a friendly front-end to the NTP service.
The Date & Time pane in System Preferences provides a friendly front-end to the NTP service.

If you want to force your Mac to resynchronise with the time server, simply open the Date & Time pane, and uncheck then check the Set date & time automatically box. You could also stop and restart the ntpd service in Terminal instead. Accurate time is particularly important when using some network services, such as Kerberos authentication. It may be simpler then to make one of the network servers a local time server, and for all users to point their own systems at that time server rather than using an external one. The latter is simple in the Date & Time pane.

Time servers

NTPscape's list of available time servers can highlight discrepancies among them.
NTPscape’s list of available time servers can highlight discrepancies among them.

You can view the hierarchy of network time servers using free NTPScape. The top-level time servers are designated Stratum One, numbering a few hundred, synchronised to very precise sources such as atomic clocks. They are used to keep Stratum Two servers accurate; there are over 100,000 of them worldwide, including Apple’s European and US servers which are normally used by OS X.

NTPscape can also show time servers as a graph, which reveals their dependencies.
NTPscape can also show time servers as a graph, which reveals their dependencies.

These servers are listed here. Be very careful which you choose, if you opt to use servers other than Apple’s. Many are small capacity and only intended for limited groups of users. A few years ago one modem manufacturer caused chaos by pointing many of their products at an unsuspecting time server which simply could not cope.

You can specify multiple time servers in the Date & Time pane, by entering them into the Set date & time automatically box and separating their names with a space.

Internals

NTP’s background service, ntpd, is started up automatically by launchd, using the script at /usr/libexec/ntpd-wrapper. Its standard list of time servers is given in /etc/ntp.conf, and other devices can use that Mac as a time server if permitted by the restrictions in /etc/ntp-restrict.conf

If you want to read the Unix documentation for ntpd, type
man ntpd
at the command line in Terminal. Some useful commands include:

  • ntpq -p which lists servers currently accessed and gives summaries of their state,
  • ntpq -crv which lists all internal variables and settings for NTP locally.

If your Mac is often disconnected from the Internet for long periods, Apple has suggested some workarounds which can help it keep accurate time here.