Linux NTP and Chrony Service Configuration

Network Time Protocol (NTP) is used to synchronize the system clock of a computer to a reference time source. In Linux, the chronyd service is a popular implementation of NTP.

What is Chrony?

Chrony is a versatile implementation of NTP. It is designed to perform well in a wide range of conditions, including systems with intermittent network connections or systems that are frequently suspended and resumed.

Installing Chrony

To install Chrony on a Linux system, use the following commands:

# On RHEL/CentOS
sudo yum install chrony

# On Ubuntu/Debian
sudo apt install chrony
    

Configuring Chrony

The main configuration file for Chrony is /etc/chrony/chrony.conf. Below is an example configuration:

# Use public NTP servers
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

# Allow NTP client access from local network
allow 192.168.0.0/24

# Specify the drift file
driftfile /var/lib/chrony/drift

# Log statistics
logdir /var/log/chrony
    

Starting and Enabling Chrony

After configuring Chrony, start and enable the service:

sudo systemctl start chronyd
sudo systemctl enable chronyd
    

Verifying Chrony

To check the status of Chrony and verify synchronization, use the following commands:

# Check service status
sudo systemctl status chronyd

# Verify synchronization
chronyc tracking
chronyc sources
    

Using Cron to Automate Tasks

Cron is a time-based job scheduler in Linux. You can use it to automate tasks like restarting the Chrony service periodically. For example:

# Edit the crontab
crontab -e

# Add the following line to restart Chrony every day at midnight
0 0 * * * sudo systemctl restart chronyd
    

Conclusion

Chrony is a powerful and flexible NTP implementation for Linux systems. By configuring it properly and using tools like Cron, you can ensure accurate time synchronization and automate related tasks.