How to keep your Linux system clock accurate

A guide to system time

Keeping time, and ensuring that it's kept accurately, is a common function on computer systems. It occurs under the hood of your system and is a given when establishing servers. It's the system administrator's responsibility to ensure that the foundational infrastructure is in place to support time on computers.

In this guide we'll explore the configuration options to allow the network time protocol (NTP) to manage time. We'll also introduce a tool to examine statics from time sources and cover a few troubleshooting exercises using network tools.

Using the block diagram below, we'll start our discussion with the hardware box, moving to sources of time and finish with more in-depth details on management – but let's begin with the BIOS and hardware clocks.

How time is set, managed and synchronised on a Linux system.

If you've ever set up the BIOS you will have been presented with menu selections for establishing date and time. This establishes the clock time on the PC's motherboard.

This clock answers to many names: a complementary metal oxide semiconductor (or CMOS) clock, a BIOS clock, a motherboard clock or as in our drawing a hardware clock.

The CMOS battery keeps the clock ticking when there's no AC power to the board. In times of old the clock and battery may have been contained in a CMOS module that plugged into a 16-pin chip socket.

The hardware clock is, in turn, the source of time for the system clock that's maintained by the operating systems. When an OS starts, the system clock seed time is taken from the hardware clock and the OS then takes over.

These two clocks operate independently and are not synchronised, which means it's possible to reboot and discover the system time isn't accurate because the hardware clock is a poor timekeeper. How far the hardware clock time drifts can result in problems, which we'll discuss later.

Managing time

The system clock can call for assistance to get the time right using a network time protocol daemon (ntpd). This daemon sends a request to more accurate external time source for time checks, and using the data provided the daemon feeds the system clock a drift factor to keep the time in sync with the external sources.

One of the limitations of ntpd is how far out of whack the system clock and the external sources are allowed to be. If the hardware clock and system clock differ by approximately 1K seconds or more, the daemon assumes one of the two time keepers is misconfigured and the daemon will fail.

Since the hardware clock is the system clock's seed time, it becomes important for the hardware clock to be within the 1K limits. One mechanism to compensate for the hardware clock is ntpdate. The program ntpdate uses an external time source to provide a one-time adjustment to the system's clock for accuracy.

From the command line you can provide ntpdate with a time source. You must make sure no time service is running, then type the following:

ntpdate 0.pool.ntp.org

On some OSes, before the ntpd starts, ntpdate uses the external time source to provide a one-time kick to the system's clock for accuracy. For the list of time source URLs or IPs for ntpdate, look in the /etc/ntp/step-tickers file.

When ntpd takes over and provides drift compensation, it isn't faced with a system clock that's so far out it can't pull it back on time. The hardware clock can continue to run amok or get some help, but we'll discuss that a little later.

Time sources

The world of external time sources is divided into levels or strata numbered according to accuracy. The closer to the originating source of time, the lower the stratum number. Getting time from a source that's directly connected is considered stratum 0.

If that time has been provided to other distribution points, which in turn pass the time on to other points, then the stratum number is incremented, to a maximum of 15.

The source of most accurate times are atomic clocks, which can be carried in satellites or be ground-based. Satellites have multiple atomic clocks on board to ensure they are keeping accurate time. The global positioning system (GPS) requires the time accuracy to put a plane on a runway and not off in the bush.

The GPS time-source receiver and Cesium clock are in the module (to the left of middle) with the black heat sink.

Ground-based atomic clocks have been a source of time in the telecommunication industry for decades. Accurate time is important to ensure that information transmitted as bits can be decoded as acceptable bits when received at the other end.