Command to sync time with NTP server in Linux
For many people, computer clocks in your devices, network machines, and servers are generally accurate. But that’s not true! These clocks are manually maintained and backed by batteries which over time drift the clock, especially in the older machines.
So why is accurate time so important? Having exact time on your machine is quite significant because of several reasons. Many aspects of your computer activity are linked with time. Perfectly synched time is crucial for tracking security-related issues; troubleshooting can become quite difficult if the timestamps in log files are incorrect. Even for financial services, keeping accurate time is critical.
Many companies solve time-related issues by connecting their networks with NTP. So what is NTP? Let’s dig into it first:
What is NTP:
The full form of NTP is “Network Time Protocol,” which has been one of the most authentic ways to synchronize the clock over a network. If your system uses NTP, you don’t need to check and set your time manually. It automatically updates the clock every time the device reboots. It is an extremely accurate way to update the clock of your device. Since the internet is everywhere, NTP is being used by every modern computer.
How to enable NTP synchronization on Linux:
Most of the Linux distributions are using “systemd,” which comes with NTP for clock synchronization. To verify this, use the command given below:
It indicates if NTP is active. If, for some reasons, it is not active, then use the below-mentioned command to enable it:
$sudo timedatectl set-ntp true
How to enable NTP for Linux distributions without “systemd”:
But what if your distribution does not come with “systemd”? Well, in that case, you can install NTP:
For Debien based distros, use:
For Fedora:
For Arch Linux:
For Red Hot and CentOS:
If you are installing NTP on a distribution that supports “systemd,” then you need to disable the NTP service of “systemd”:
$sudo timedatectl set-ntp false
How to start NTP:
Once the installation is completed, NTP will be active by default. But if it is inactive, then use:
$sudo systemctl start ntp
To keep it enable upon rebooting use:
$sudo systemctl enable ntp
How to check the status of NTP:
To check the status of NTP use:
How to check NTP stats:
To check NTP stats, use the command:
How to monitor NTP daemon:
To monitor the NTP daemon, we will use the “ntpq” utility with the “-p” flag:
How to modify the “ntp.conf” file:
You can modify the NTP configuration file according to your preference. To open the files, use:
Or if you have vim installed, then use:
It can be seen in the image below that different default servers system is synchronized time with.
You can keep the default settings, but if you want to add another pool directive, then visit NTP pools and add the lines in the configuration file following the syntax mentioned below:
pool [pool address] iburst
For instance, you can find NTP pools of United States from here:
How to implement NTP on Linux using Chrony:
There is another modern tool to implement NTP on the latest Linux distributions. Firstly, let’s check how to install it on various distributions.
For Debian based distros, use:
For Red Hot and CentOS, use:
For Fedora:
To enable the chrony daemon, use:
$systemctl enable –now chrony
Now, verify it by checking the status:
To check information about sources and other stats, use:
For detailed output, use the “-v” option with the above command:
To check the name of the server to which our computer is currently synchronized, use :
To check and modify the configuration file, use:
$vi /etc/chrony/chrony.conf z
The above command is for Debian and Ubuntu, for Red Hot, Fedora, and CentOS, use:
Conclusion:
Synchronization of time is essential to determine some specific activity of a computer. Every modern system comes with some mechanism that automatically sets the time of the machine; NTP (Network Time Protocol) is one of them.
In this write-up, we learned how to sync time with the NTP server. If your distribution is “systemd” based, your clock is already synced with NTP. We also discussed installing and enabling NTP service for the distributions that do not base on “systemd.” There is another utility for NTP configuration called “Chrony,” which has some additional features but essentially does the same job.