Keeping the date and time accurate on a Linux system is very important because many services, such as cron jobs and scripts, rely on accurate time to function properly. 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 clocks of a Linux system with available online NTP servers.
The good old ntpd daemon that was used to synchronize time and date settings is outdated and not available for modern Linux systems such as Ubuntu 20.04. Fedora 30 and CentOS 8. Instead, we have chrony, which is an NTP implementation developed by RedHat.
Chrony makes the best choice for the following reasons:
- Chrony synchronizes time much faster than its predecessor, NTP.
- This compensates for network latency and latency issues.
- It still performs well even with network quality degradation.
- You can set up a local server using chrony to act as a time server from which the rest of the client PCs can retrieve the date and time settings.
Chrony includes the chronyd daemon and the chronyc command line tool. The Chronyd daemon runs in the background and synchronizes the system time with the servers defined in the /etc/chrony.conf file.
Chronyc command line utility allows users to interact with Chrony and extract as much information as possible. We’ll start by installing Chrony on various Linux distributions.
Install Chronyd on Linux
Chronyd is installed by default on modern systems. However, chrony is not included on older Linux systems that still rely on the legacy NTP package.
So, this is how you can install Chrony.
On Ubuntu / Debian / Mint
$ sudo apt-get install chronyd
On CentOS / RHEL
$ sudo yum install chronyd
After installation, enable and start the Chronyd daemon as follows:
$ sudo systemctl --enable now chronyd
Then confirm the status as follows:
$ sudo systemctl status chronyd
As you can see, the chronyd daemon is active and working as expected.
Chrony config file
Chronicle settings are defined in the /etc/chrony.conf or /etc/chrony/chrony.conf configuration file. No initial intervention is required as the defaults already sync your system with the available NTP server pools. Major Linux distributions such as Ubuntu, CentOS, RHEL and Fedora, have their own NTP pools by default.
Check time synchronization with Chronyc
To verify that Chrony is actually running, and to 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 offset and offset, not to mention some parameters, run:
$ chronyc sources
With the -v flag, you can get more details as shown:
$ chronyc sources -v
You can also confirm that Chrony is synced using the command below. The Reference ID gives you the name of the server that your system points to to get the time and date settings. Other details, such as Last offset and System time, show how far the system is from the NTP server.
$ chronyc tracking
The timedatectl command will also come in handy and will help you find out if the NTP service is enabled or not.
Configure NTP Client
To configure another system on your local network as a client and point it to your NTP server, follow these steps.
On the NTP server navigate to the Chrony config file, uncomment the allow directive and specify the subnet mask.
Save and exit the config file.
Be sure to allow the NTP service through the firewall. In our case, we will open the firewall on CentOS 8 as follows:
$ sudo firewall-cmd --permanent --add-service = ntp
Then reboot for the changes to take effect:
$ sudo firewall-cmd --reload
Then restart the chronyd daemon.
$ sudo systemctl restart chronyd
On the client or remote system, uncomment any NTP pools and add the line shown. The provided IP address is the IP address of the NTP server. This will of course be different in your case, so edit it accordingly.
Again, restart the chronyd daemon to reflect the changes.
$ sudo systemctl restart chronyd
The client system will now point to an NTP server on the same network as shown.
We have demonstrated how to set up NTP on Linux using the Chrony service and how to configure the client system to point to an NTP server.