Commands 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:

timedatectl


It indicates that 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:

sudo apt install ntp

For Fedora:

sudo dnf install ntp

For Arch Linux:

sudo pacman -S ntp

For Red Hot and CentOS:

sudo yum install ntp

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:

systemctl status ntp

How to Check NTP Stats

To check NTP stats, use the command:

ntpstat

How to Monitor NTP Daemon

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

ntpq -p

How to Modify the “ntp.conf” File

You can modify the NTP configuration file according to your preference. To open the files, use:

nano /etc/ntp.conf

Or if you have vim installed, then use:

vi /etc/ntp.conf

It can be seen in the image below that different default server systems are synchronized with time.

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:

sudo apt install chrony

For Red Hot and CentOS, use:

sudo yum install chrony

For Fedora:

sudo dnf install chrony

To enable the chrony daemon, use:

systemctl enable --now chrony

Now, verify it by checking the status:

systemctl status chrony

To check information about sources and other stats, use:

chronyc sourcestats

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

chronyc sourcestats -v

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

chronyc tracking


To check and modify the configuration file, use:

vi /etc/chrony/chrony.conf

The above command is for Debian and Ubuntu, for Red Hot, Fedora, and CentOS, use:

vi /etc/chrony.conf

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 are not based on “systemd”. There is another utility for NTP configuration called “Chrony”, which has some additional features but essentially does the same job.



from https://ift.tt/BhCyeIH

Post a Comment

0 Comments