How to Avoid Time Drifts on Your Linux Servers with Chrony
You'd be surprised how many problems a Linux system can have if its time is not synchronized correctly. Here's how to use Chrony to get NTP in line.
Aug 15th, 2024 5:00pm by
Featured image via Unspash.
What You’ll Need
To work with Chrony, you’ll need one or more Linux servers and a user with sudo privileges. Before we get to Chrony, there’s one task you must complete first.Setting Your Time Zone
To make sure your servers have the right time, Chrony requires that all of them must be configured for the right time zone. If your servers are all set to the same wrong time zone — or if they’re set to different time zones — Chrony will be of no use to you. Ergo, let’s set the time zone on your servers. This step is taken care of with thetimedatectl command, which is installed on most Linux servers by default. Before you do this, you’ll want to know which timezone you should set. To view a listing of all timezones, issue the command:
timedatectl list-timezonesScroll through that listing until you find the correct one for your area. For example, if you live in Louisville, Ky., the proper time zone is America/Kentucky/Louisville and is set like this:
sudo timedatectl set-timezone America/Kentucky/LouisvilleOnce you’ve done that, you can verify the change with:
timedatectlMake sure you do the above on all of your servers (be they on bare metal, virtual machines or containers). You’re now ready for Chrony.
Installing Chrony
Chrony is found in the standard repositories for most distributions, which means the installation is very easy. For example, on a Ubuntu-based distribution, the installation command would be:sudo apt-get install chrony -yIf you’re on a Fedora-based distribution, the command is:
sudo dnf install chrony -yFor Arch-based distributions:
sudo pacman -S chronyOnce Chrony is installed, make sure to start and enable it with the command:
sudo systemctl enable --now chronyd
Enabling the Chrony NTP service
Next, you must enable the Chrony NTP service with the command:sudo timedatectl set-ntp yesYou’ll receive no output from the above command. With that taken care of, check the time with:
timedatectlIt should be spot on. Not only that, but you should also now see that the NTP service is listed as active, which means Chrony is keeping your time in check. Do note that if you had to change the timezone of your machine, you should reboot so the changes take effect.
Configuring Chrony
You shouldn’t have to do anything to Chrony to make it work correctly. Should you want to investigate the configuration, you can open the file for editing with:sudo nano /etc/chrony.confIf you find the file isn’t there, try the command:
sudo nano /etc/chrony/chrony.confAt the top of the file, you’ll find a single public server listed that is used to keep time in sync. On my AlmaLinux test server, that line is:
pool 2.almalinux.pool.ntp.org iburstIf you want, you can always change the default pool. For example, according to the NTP Pool Project, you could use the following pools for the United States: server 0.us.pool.ntp.org server 1.us.pool.ntp.org server 2.us.pool.ntp.org server 3.us.pool.ntp.org There are several other options you can look through but you’ll most likely want to keep them as is. You can also configure your Linux machine as a Chrony NTP server. For this, you must uncomment (remove the leading # character) the following lines in the Chrony configuration file:
allow 192.168.0.0/16
local stratum 10
sudo systemctl restart chronydMake sure to allow the NTP service through your firewall. For example, on AlmaLinux that would require the following two commands:
sudo firewall-cmd --add-service=ntp –permanent sudo firewall-cmd --reloadYou could then configure your NTP server within the chrony.conf files of the clients on your network. For example, if your NTP server is at 192.168.1.210, you could add the following in the Chrony config file:
pool 192.168.1.210 iburst maxsources 4At this point, your client will remain in sync with your server. As long as your server is in sync with the NTP pool, any server (or desktop) that uses it as a time server will remain in sync. Avoid time-related issues with this simple-to-use tool, and you’ll pull less hair and lose less sleep.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.