Network Time Protocol
This document explains how to configure an Alpine Linux system to synchronize its clock with Internet time servers.
Time Synchronization
Computers often use two separate components for determining the time of day:
- Time is kept by the operating system kernel by measuring elapsed time on the Central Processing Unit (CPU), and
- Most systems have a hardware module called a Real Time Clock (RTC) that uses a small battery to keep the date and time stored separately.
On systems that lack an RTC module, or when the RTC battery is dead, the time and date normally reset to the earliest allowable value the platform firmware allows whenever power to the computer is lost. The exact date and time vary by hardware manufacturer. Desktop and laptop systems can use either a primary lithium cell battery (mostly commonly a CR2032-sized coin cell) or a rechargeable battery for the RTC. In either case, the battery will eventually wear out and require replacement.
When running, Linux systems keep time by maintaining a UNIX timestamp internally. UNIX time simply counts the number of seconds (and fractions of seconds) that have elapsed since the epoch. The epoch is an arbitrary date and time that was chosen in the early years of commercial UNIX systems, and it is always 00:00 (midnight) in Coordinated Universal Time (UTC) on January 1, 1970. Keeping time this way avoids ambiguities with date formats and local time zones, while making time easy to store as an integer or floating-point value.
Both the RTC and the UNIX time stored in the kernel suffer from accuracy problems, as neither the CPU nor the RTC chip is exceptionally precise or always totally consistent. For this reason, a computer’s clock will drift relative to the actual wall clock time, since the internal clock will tend to run a little faster or a little slow than real time. Precise official time is normally kept using highly accurate atomic clocks, which would be cost prohibitive to put into every computer system.
Accurate time is still needed for many processes, however, including proper operation of encryption technologies used to secure Internet websites. For this reason, it is important to keep the computer’s clock synchronized with the official atomic clocks. Fortunately, Internet services are available for this purpose, and these services use the Network Time Protocol (NTP), which is standardized in RFC 5905.
It is possible for us to run an NTP server on Alpine Linux, which would allow other systems to synchronize their clocks with our clock. However, it’s usually more beneficial to let each system run an NTP client that synchronizes with servers that are part of the NTP pool and are themselves synchronized to government atomic clocks. By configuring an NTP client on our system, we can keep our local clocks synchronized to within a fraction of a second of these official clocks.
Installing an NTP Client
Before can set up chrony as our NTP client, we have to create a minimal /etc/network/interfaces file if we aren’t using wired networking. If you are connecting to the Internet with an Ethernet cable, then you probably have already done this step. If using WiFi, create and edit the required file by running:
vi /etc/network/interfaces
At a minimum, this file should contain the following two lines, which you may need to add:
auto lo
iface lo inet loopback
If you have any other lines in that file, such as references to eth0, do NOT change them! Save your file and exit vi.
On Alpine Linux, we can use the setup-ntp script to configure an NTP client. In general, chrony is a good choice, and it can be installed, configured, and enabled in one easy step (as root):
setup-ntp chrony
The chrony service takes a few moments to start, and you may find that you need to press the Enter key to get a new command prompt after this step.