How to Synchronize Time with NTP in Linux

[‘

n

The Network Time Protocol (NTP) is a protocol used to synchronize computer system clock automatically over a networks. The machine can have the system clock use Coordinated Universal Time (UTC) rather than local time.

n

Maintaining accurate time on Linux systems especially servers is a important task for many reasons. For example, in a networked environment, accurate timekeeping is required for accurate timestamps in packets and system logs for root-cause analysis, determining when problems occurred, and finding correlations.

n

Chrony is now the default NTP implementation package on the latest versions of Linux operating systems such as CentOS, RHEL, Fedora and Ubuntu/Debian among others and comes pre-installed by default. The package consists of chronyd, a daemon that runs in userspace, and chronyc a command-line program for monitoring and controlling chronyd.

n

Chrony is a versatile NTP implementation and performs well in a wide range of conditions (check out the comparison of chrony suite to other NTP implementations). It can be used to synchronize the system clock with NTP servers (act as a client), with a reference clock (e.g a GPS receiver), or with a manual time input. It can also be employed as an NTPv4 (RFC 5905) server or peer to provide a time service to other computers in the network.

n

In this article, you will learn how to synchronize server time with NTP in Linux using chrony.

n

Installing Chrony in Linux Server

n

In most Linux systems, the chrony command is not installed by default. To install it, execute the below command.

n

$ sudo apt-get install chrony    [On Debian/Ubuntu]rn$ sudo yum  install chrony       [On CentOS/RHEL]rn$ sudo dnf install chrony        [On Fedora 22+]rn

n

Once the installation is complete, start the chrony service and enable it to automatically start at system boot, then check if it is up and running.

n

# systemctl enable --now chronydrn# systemctl status chronydrn

n

Check Chrony Status
Check Chrony Status

n

To cross-check if chrony is now up and running fine and to see the number of servers and peers that are connected to it, run the following chronyc command.

n

# chronyc activityrn

n

Check Chrony Activity
Check Chrony Activity

n

Checking Chrony Synchronization

n

To display information (list of servers available, status, and offsets from the local clock and the source) about the current time sources that chronyd is accessing, run the following command with the -v flag shows the description for each column.

n

# chronyc sourcesrnORrn# chronyc sources -vrn

n

Check Chronyd Time Sources
Check Chronyd Time Sources

n

Concerning the previous command, to display other useful information for each of the sources currently being examined by chronyd (such as the drift rate and offset estimation process), use the sourcestats command.

n

# chronyc sourcestatsrnORrn# chronyc sourcestats -vrn

n

Check Chronyd Source Stats
Check Chronyd Source Stats

n

To check chrony tracking, run the following command.

n

# chronyc trackingrn

n

In the output of this command, the reference ID specifies the name (or IP address) if available, of the server to which the computer is currently synchronized, out of all the available servers.

n

Display Chrony Tracking
Display Chrony Tracking

n

Configuring Chrony Time Sources

n

The main chrony configuration file is located at /etc/chrony.conf (CentOS/RHEL/Fedora) or /etc/chrony/chrony.conf (Ubuntu/Debian).

n

When installing a Linux OS in the cloud, your system should have some default servers or a pool of servers added during the installation process. To add or change the default servers, open the configuratioon file for editing:

n

# vim /etc/chrony.confrnORrn# vim /etc/chrony/chrony.confrn

n

You can either add several servers using the server directive as shown.

n

server 0.europe.pool.ntp.org iburstrnserver 1.europe.pool.ntp.org iburstrnserver 2.europe.pool.ntp.org ibusrtrnserver 3.europe.pool.ntp.org ibusrtrn

n

Add NTP Servers
Add NTP Servers

n

or in most cases, it’s best to use ntppool.org to find an NTP server. This allows the system to try to find the closest available servers for you. To add a pool, use the pool directive:

n

pool 0.pool.ntp.org burstrn

n

Add a Pool of NTP Servers
Add a Pool of NTP Servers

n

There are many other options you can configure in the file. After making changes, restart the chrony service.

n

$ sudo systemctl restart chronyttrnORrn# systemctl restart chronydrn

n

To show information about the current time sources that chronyd is querying, run the following command once more.

n

# chronyc sourcesrn

n

View Chronyd Time Sources
View Chronyd Time Sources

n

To check chrony tracking status, run the following command.

n

# chronyc trackingrn

n

Check Chrony Synchronizing Status
Check Chrony Synchronizing Status

n

To display the current time on your system, check whether system clock is synchronized and whether NTP is indeed active, run the timedatectl command:

n

# timedatectlrn

n

Check Current Server Time
Check Current Server Time

n

That brings us to the end of this guide. If you have any questions, reach us via the comment section below. For more information, check out: using the chrony suite to configure NTP from the RHEL 8 documentation or using chrony to configure NTP from the Ubuntu official blog.

n

‘]