Computer clocks are not perfect. Given a few days, weeks or months, they drift and stop showing the real time. Simply put, 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.
What Is NTP?
NTP is an acronym for Network Time Protocol. It’s an algorithm designed to synchronize computer clocks through network connections and keep 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 noticed your computer clock drift 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, 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. Sometimes 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.
On Linux OS without systemd
With MX Linux becoming so popular lately, an example regarding this OS seems fitting to start with. It’s also a special case. It “kind of” has systemd, but it “kind of” doesn’t. timedatectl reports that network time is “on,” but systemd-timesyncd is not used.
So how does it sync? You can check with the next command which 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.
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, syncs, then exits.
This means 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
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 connected to the Internet.
On Debian, Ubuntu, Linux Mint, Zorin and other distros based on these, run:
sudo apt install ntpdate
On all 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 Fedora, ntpdate will be deprecated, so it’s recommended you use the ntpd daemon instead (see next section). 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 and then automatically launch it at every boot; no further action is required from you.
On Fedora, install with:
sudo dnf install ntp
On CentOS, run:
sudo yum install ntp
For Arch Linux, use:
sudo pacman -S ntp
And on openSUSE, enter:
sudo zypper install ntp
Afterward, enable the service to launch at boot and 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.
It’s rare that you have to follow any of these steps on a Linux OS with a graphical user interface at least. Time sync should already be configured for you. But these steps can at least help you sometimes debug a problem. Many desktop environment control panels let you enable and disable time synchronization. In the background they use similar commands to configure this for you, so you can do it yourself when the graphical user interface tools fail.
Our latest tutorials delivered straight to your inbox