What Is NTP?
NTP is an acronym for Network Time Protocol. It’s an algorithm that synchronizes computer clocks through network connections and keeps them accurate.
How to Enable Time Synchronization on Linux
Luckily, most distributions implement one form or another of time synchronization out of the box. If you never observed your computer clock drifting away from your phone clock, you almost certainly have an NTP client/daemon running already.
On Linux OS With systemd
The majority of Linux distributions have adopted systemd, and with it comes the “systemd-timesyncd” daemon. That means you have an NTP client already preinstalled on Ubuntu, Debian, Fedora, Arch Linux, openSUSE, Manjaro and others. On these distros and others that are based on them (e.g., elementary OS and Zorin OS), run this to check whether NTP sync is enabled: If you see Network time on: yes in the output, then your computer clock is automatically periodically adjusted through NTP. You may also notice NTP synchronized: no, which may mean the clock is synced through some other tool besides “systemd-timesyncd.” It can also mean that systemd-timesyncd didn’t sync yet but will do so later. If you see Network time on: no, run the following command to enable NTP time synchronization. That’s all you have to do on these distributions.
On Linux OS Without systemd
With MX Linux being one of the most popular Linux distros, it seems fitting to start with an example regarding this OS. It’s also a special case, as it “kind of” has systemd but also “kind of” doesn’t. timedatectl reports that network time is “on,” but systemd-timesyncd is not used. How does it sync? You can check with the next command, one that you can use on any other Linux operating system. On MX Linux you will get a result similar to what you see in the following picture. From this filtered log message, you can see that the “ntpdate” client ran once. This is a popular client, installed by default on many Linux distributions. It’s lightweight and only queries NTP servers sporadically. It doesn’t run in the background and is triggered by scripts and syncs, then exits. That time is not synced with extremely high accuracy (think milliseconds or microseconds), but it’s accurate enough for most purposes. It’s normally accurate, at least to the second. On other operating systems you may see “ntpd,” which is a daemon constantly running in the background and extremely accurate. The more it runs, the more precise it becomes. If, for some reason, you want to install ntp or ntpdate on operating systems with systemd, first disable systemd’s ntp client. Of course, the above command is not necessary on non-systemd distros.
Install ntpdate Client
This is suitable for home computers, laptops and other devices that are not constantly on the Internet. On Debian, Ubuntu, Linux Mint, Zorin and other distros based on these, run: On all of the above Debian-based distros, it is configured out of the box to run after it sees a new network connection. You can force a time sync now if you disconnect from your network and reconnect. On both Fedora and Ubuntu, ntpdate is a deprecated software, so it’s recommended you use the ntpd daemon instead (as shown below). The same is recommended for all other distros for two reasons:
They sometimes don’t include ntpdate in a separate package but bundle it with ntpd from the next section.They don’t automate ntpdate to run automatically on every network connection. You have to insert ntpdate yourself in a script that automatically runs after the network connects.
In this case, it makes more sense to install ntpd as instructed in the next section.
Install ntpd Daemon
On Debian, Ubuntu, Zorin, Mint and other distros based on Debian or Ubuntu, run: These distros will start ntpd immediately, then automatically launch it at every boot; no further action is required from you. On Fedora, install with: For Arch Linux, use: On openSUSE, enter: Afterward, enable the service to launch at boot then start it: On other distros that run without systemd, you may have to run these commands instead: or whatever is equivalent for your OS. If you get an error that ntpd doesn’t exist, replace “ntpd” with “ntp” in the above commands. Some distributions name ntpd daemon service files differently.
Monitoring ntpd
With ntpd running, you can now use its built-in tools to check the status of your timeservers. For example, you can run the following command to check whether your machine is currently talking to a timeserver: You can also poll the active timeservers in your current ntpd instance. This can be useful if you are trying to sync the time in your Linux machine and want to see the most reliable hosts that you can use. To do a timeserver poll, run the following command: Lastly, ntpq is also a prompt that you can use to directly interact with the NTP daemon. To use this prompt, run ntpq in your terminal window. Once inside, enter ? to list all the available commands for the NTP daemon. For example, running the following command will force the daemon to sync your Linux machine’s time to NTP4:
Install the Chrony Daemon
Aside from running ntpd, it is also possible to use a modern timekeeping protocol and front-end for NTP. Chrony is a simple and modern implementation of the NTP daemon. One of the biggest advantages of using Chrony is that it automatically calculates the amount of seconds your local clock drifts away from its reference timeserver. From there, the daemon then offsets the local Linux time to be always in sync with this reference. This approach can be useful if you are using your machine for time-sensitive tasks, such as monitoring weather forecasts. To install Chrony in Debian and Ubuntu, run the following command: With Arch Linux, use pacman to install the program: Use dnf to install Chrony on Fedora: Once Chrony is in your system, make sure it is polling a remote timeserver by running the following command: After that, either log out or restart your machine to make sure that the time in your Linux system will sync with your remote timeserver. Image credit: Unsplash. All alterations and screenshots by Ramces Red. The developers of Chrony designed the program to work with modern Linux hardware, so it includes features that take a lot of resources from your system. ntpd is simpler and can work with both legacy and smaller hardware.