How to Sync Linux Time With NTP Server

Sync Linux Time Ntp 00 Featured Image

Computer clocks are not perfect. Given a few days, weeks or months, they drift and stop showing the real time. After they drift, they may show it’s “10:30” when in fact it’s “10:33.” It was common practice on older computers to periodically readjust the computer clock manually. But after Internet connections became ubiquitous, modern operating systems started to adjust the clock automatically with the help of NTP servers.

Also read: How to Generate SSL Certificates on Linux Using OpenSSL

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:

timedatectl
Sync Linux Time Ntp 01 Timedatectl Sample

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.

sudo timedatectl set-ntp true

That’s all you have to do on these distributions.

Tip: read our full timedatectl tutorial for more options.

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.

sudo cat /var/log/syslog | grep ntp

On MX Linux you will get a result similar to what you see in the following picture.

Sync Linux Time Ntp 02 Mx Linux Grep Time

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.

sudo timedatectl set-ntp false

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:

sudo apt install ntpdate

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:

sudo apt install ntp

These distros will start ntpd immediately, then automatically launch it at every boot; no further action is required from you.

On Fedora, install with:

sudo dnf install ntp

For Arch Linux, use:

sudo pacman -S ntp

On openSUSE, enter:

sudo zypper install ntp

Afterward, enable the service to launch at boot then start it:

sudo systemctl enable ntpd
sudo systemctl start ntpd

On other distros that run without systemd, you may have to run these commands instead:

sudo update-rc.d ntpd enable
sudo service ntpd start

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.

Also read: The Best Time Zone Converters for Easy Scheduling

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:

ntpstat
Sync Linux Time Ntp 03 Ntpstat Running

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:

ntpq -p
Sync Linux Time Ntp 04 Ntpq List Timeservers

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.

Sync Linux Time Ntp 05 Ntpq Interactive Mode

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:

ntpversion 4
Sync Linux Time Ntp 06 Ntpq Change Ntp Version

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:

sudo apt install chrony
Sync Linux Time Ntp 07 Apt Install Chrony

With Arch Linux, use pacman to install the program:

sudo pacman -S chrony

Use dnf to install Chrony on Fedora:

sudo dnf install chrony

Once Chrony is in your system, make sure it is polling a remote timeserver by running the following command:

chronyc sources
Sync Linux Time Ntp 08 Chrony List Timeservers

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.

Also read: How to Fix High CPU Usage in Linux

Frequently Asked Questions

Is it possible to install both Chrony and ntpd in the same machine?

No, as both Chrony and ntpd are daemons that provide the same set of services for your machine. Both programs aim to sync the local Linux time to a timeserver. However, Chrony and ntpd differ on its target application.

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.

Does mixing timeservers make my clock more accurate?

Yes! Some Linux desktops use “pool timeservers” to maintain its system time. These are services that collect time data from other timeservers. A “pool timeserver” uses this data to create an estimate of the current time. For example, Ubuntu uses “ubuntu.pool.ntp.org” as its main timeserver for both its desktops and servers.

I am using SystemD. Will Chrony and ntpd cause conflicts with timesyncd?

No! By default, SystemD’s timesyncd is only an optional module to the init system. It is possible to use alternative NTP daemons even with a SystemD distribution. For example, installing Chrony on Ubuntu and Debian will automatically remove timesyncd in the system.

Image credit: Unsplash. All alterations and screenshots by Ramces Red.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Ramces Red
Ramces Red - Staff Writer

Ramces is a technology writer that lived with computers all his life. A prolific reader and a student of Anthropology, he is an eccentric character that writes articles about Linux and anything *nix.