Linux Commands

How to Install NTP Using Chrony in Linux

Keeping accurate date and time on a Linux system is essential because many services such as cron jobs and scripts rely on accurate time to function as expected. The Network Time Protocol, abbreviated as NTP, is a protocol that maintains accurate time on a Linux system. It is an internet protocol that plays a role in synchronizing the clock of a Linux system with the available online NTP servers.

The good old ntpd daemon that was used to synchronize time and date settings has been deprecated and is not available for modern Linux systems such as Ubuntu 20.04, Fedora 30, and CentOS 8. In its place, we have chrony, which is an implementation of NTP which RedHat developed.

Chrony makes for a better choice for the following reasons:

  • Chrony synchronizes time much faster than its predecessor, NTP.
  • It makes up for latency issues and network delays.
  • It still works well even with network degradation.
  • You can configure a local server with chrony to act as a time server from which the rest of the client PCs can obtain date and time settings.

Structure of Chrony

Chrony comprises the chronyd daemon and chronyc command-line tool. The chronyd daemon runs silently in the background and syncs the time of the system with the servers defined in the /etc/chrony.conf file.

The chronyc command-line utility allows users to interact with chrony and extract as much information as possible.
We will start by installing Chrony on various Linux distributions.

Install Chronyd in Linux

In modern systems, Chronyd comes installed by default. However, chrony is not included in older Linux systems that still rely on the deprecated NTP package.

So, here’s how you can install Chrony.

On Ubuntu / Debian / Mint

$ sudo apt-get install chronyd

On CentOS / RHEL

$ sudo yum install chronyd

Once installed, enable and start Chronyd daemon as follows:

$ sudo systemctl --enable now chronyd

Then confirm the status as follows:

$ sudo systemctl status chronyd

As you can observe, the chronyd daemon is active and running as expected.

Chrony configuration file

Chrony settings are defined in the /etc/chrony.conf or /etc/chrony/chrony.conf configuration file. Initially, no intervention is required since the default values already sync your system with available NTP server pools. Major Linux distros such as Ubuntu, CentOS, RHEL, and Fedora have their default NTP pools.

From the configuration file below, the NTP server pool to which the Linux system is synced is pool 2.centos.pool.ntp.org iburst

For Ubuntu, this appears as shown.

Check time synchronization with Chronyc

To confirm that indeed Chrony is running and view the peers and servers to which it is connected, run the command:

$ chronyc activity

To view a detailed list of time servers, their IP addresses, time skew, and offset, to mention but a few parameters, execute:

$ chronyc sources

With the -v flag, you can obtain more verbose information as shown:

$ chronyc sources -v

You can also confirm that the chrony is synchronized using the command below. The Reference ID gives you the server’s name that your system points to obtain the time and date settings. Other details such as Last offset and System time indicate how far off the system is from the NTP server.

$ chronyc tracking

The timedatectl command also comes in handy and helps you know if the NTP service is enabled or not.

Configure NTP client

To configure another system in your LAN as a client and point it to your NTP server, here are the steps to follow.

On the NTP server, head over to the Chrony configuration file and uncomment the allow directive and specify the subnet mask. In my case, the subnet mask is 192.168.2.0/24.

Save and exit the configuration file.

Be sure to allow the NTP service across the firewall. For my case, I will open the firewall on CentOS 8 as follows:

$ sudo firewall-cmd --permanent --add-service=ntp

Then reload to effect the changes:

$ sudo firewall-cmd --reload

Next, restart chronyd daemon.

$ sudo systemctl restart chronyd

On the client or remote system, uncomment any NTP pools and add the line shown. The IP address provided is the IP of the NTP server. For your case, this, of course, will be different, so edit that accordingly.

server 192.168.2.109

Again, restart chronyd daemon for the changes to be reflected

$ sudo systemctl restart chronyd

The client system will now be pointed to the NTP server on the same network as shown.

Conclusion

We have demonstrated how to install NTP on Linux using Chrony service and how to configure a client system to point to an NTP server.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.