Command to sync time with NTP server in Linux

9

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

command to sync time with ntp server in linux Command to sync time with NTP server in Linux 1623202412 889 Command to sync time with NTP server in Linux

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:

command to sync time with ntp server in linux Command to sync time with NTP server in Linux 1623202412 181 Command to sync time with NTP server in Linux

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

command to sync time with ntp server in linux Command to sync time with NTP server in Linux 1623202413 490 Command to sync time with NTP server in Linux

How to check the status of NTP:

To check the status of NTP use:

command to sync time with ntp server in linux Command to sync time with NTP server in Linux 1623202413 596 Command to sync time with NTP server in Linux

How to check NTP stats:

To check NTP stats, use the command:

command to sync time with ntp server in linux Command to sync time with NTP server in Linux 1623202414 355 Command to sync time with NTP server in Linux

How to monitor NTP daemon:

To monitor the NTP daemon, we will use the “ntpq” utility with the “-p” flag:

command to sync time with ntp server in linux Command to sync time with NTP server in Linux 1623202414 867 Command to sync time with NTP server in Linux

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:

command to sync time with ntp server in linux Command to sync time with NTP server in Linux 1623202415 982 Command to sync time with NTP server in Linux

It can be seen in the image below that different default servers system is synchronized time with.

command to sync time with ntp server in linux Command to sync time with NTP server in Linux 1623202415 295 Command to sync time with NTP server in Linux

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:

command to sync time with ntp server in linux Command to sync time with NTP server in Linux 1623202416 94 Command to sync time with NTP server in Linux

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:

command to sync time with ntp server in linux Command to sync time with NTP server in Linux 1623202417 681 Command to sync time with NTP server in Linux

For Red Hot and CentOS, use:

For Fedora:

To enable the chrony daemon, use:

$systemctl enable –now chrony

command to sync time with ntp server in linux Command to sync time with NTP server in Linux 1623202417 698 Command to sync time with NTP server in Linux

Now, verify it by checking the status:

command to sync time with ntp server in linux Command to sync time with NTP server in Linux 1623202418 388 Command to sync time with NTP server in Linux

To check information about sources and other stats, use:

For detailed output, use the “-v” option with the above command:

command to sync time with ntp server in linux Command to sync time with NTP server in Linux 1623202418 583 Command to sync time with NTP server in Linux

To check the name of the server to which our computer is currently synchronized, use :

command to sync time with ntp server in linux Command to sync time with NTP server in Linux 1623202419 855 Command to sync time with NTP server in Linux

To check and modify the configuration file, use:

$vi /etc/chrony/chrony.conf z

command to sync time with ntp server in linux Command to sync time with NTP server in Linux 1623202419 165 Command to sync time with NTP server in Linux

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.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More