Time Synchronization with Chrony in 5 Minutes
An out-of-sync server clock causes countless headaches: JWT authentication failures, inconsistent log timestamps, or misfiring cron jobs. If you need a quick fix, just follow these 3 steps:
Step 1: Install the Chrony package
Run the command corresponding to your distribution:
# On Ubuntu / Debian
sudo apt update && sudo apt install chrony -y
# On RHEL / AlmaLinux / Rocky Linux / CentOS
sudo dnf install chrony -y
Step 2: Enable the service
# On Ubuntu/Debian, the service is usually named chrony
sudo systemctl enable --now chrony
# On the RHEL/CentOS family, the service is named chronyd
sudo systemctl enable --now chronyd
Step 3: Verify synchronization sources
chronyc sources -v
Look at the leftmost column. A ^* symbol preceding a server IP indicates that your system has locked onto the reference time and is synchronizing stably.
Why Servers Cannot Run on Unsynchronized Time
Imagine an online payment gateway. An API node records a top-up transaction at 10:00:00, but the database lags by 3 seconds and records it at 09:59:57. The result is a completely corrupted event timeline. Fraud detection triggers false alarms, user session tokens suddenly invalidate, and the on-call operations team spends all night sorting through the mess, which also makes configuring auditd on Linux to trace security incidents practically impossible.
Back in 2021, I handled an unforgettable debugging incident on a MySQL Master-Replica cluster. The replica continuously threw alerts as Seconds_Behind_Master fluctuated wildly from a few seconds to thousands of seconds. I checked everything from disk I/O and network bandwidth to buffer pool metrics, but found nothing unusual. Only when I ran date on both nodes did the truth emerge: the two servers were out of sync by exactly 14 seconds. A minor time configuration oversight cost an entire nerve-wracking afternoon.
Why Chrony Outperforms Traditional ntpd
NTP (Network Time Protocol) allows systems to fetch standard time from atomic clocks or GPS satellite references. Historically, ntpd was the standard tool. However, Chrony was designed later to tackle modern infrastructure challenges much more effectively, as detailed in our guide on Linux time sync and why Chrony is the modern standard over NTP:
- Ultra-fast convergence: Instead of taking 15 to 30 slow minutes like
ntpd, Chrony synchronizes the system clock within seconds of startup. - Optimized for virtual machines and containers: When VMs are suspended, snapshotted, or live-migrated via vMotion, virtual clocks often jump. Chrony compensates smoothly without crashing time-sensitive processes.
- Clock drift resilience during network outages: Chrony constantly computes the hardware clock drift of the motherboard’s crystal oscillator. Even if the network drops for days, the machine maintains an accurate clock rate.
Deconstructing the Configuration File
The configuration file is located at /etc/chrony/chrony.conf (Ubuntu/Debian) or /etc/chronyd.conf (RHEL/CentOS). You only need to understand these 4 core directives:
# Public NTP server pool
pool pool.ntp.org iburst
# Record the hardware clock drift rate to a file
driftfile /var/lib/chrony/drift
# Step the clock immediately if offset exceeds 1.0 second in the first 3 updates
makestep 1.0 3
# Synchronize the system clock to the hardware real-time clock (RTC)
rtcsync
The iburst directive is invaluable: it instructs Chrony to send a burst of 4–8 probe packets immediately upon daemon startup, allowing the server to synchronize instantly rather than waiting for slow poll cycles.
Production and Internal Network Configuration
1. Prioritize Geographically Close NTP Servers
The default global pool spans worldwide servers, which can introduce higher network latency. If your servers are hosted in Vietnam or Singapore, choose regional Asian pools or public servers from Google/Cloudflare for the lowest ping:
server time.google.com iburst
server time.cloudflare.com iburst
server 0.asia.pool.ntp.org iburst
server 1.asia.pool.ntp.org iburst
Save the file and restart the service:
sudo systemctl restart chrony # Ubuntu/Debian
sudo systemctl restart chronyd # RHEL/CentOS
2. Setting Up an NTP Server for an Internal LAN
In environments with hundreds of servers, opening outbound internet access on port 123 for every machine wastes bandwidth and poses security risks. The best practice is to deploy a dedicated Master NTP Server within your local network, configured via tools like nmcli Linux to maintain reliable addressing.
On the Master node, open the configuration file and allow your internal subnet to query time:
# Allow access from the entire local subnet
allow 192.168.1.0/24
Then permit UDP port 123 through the firewall using Linux firewalld or UFW:
# With UFW (Ubuntu/Debian)
sudo ufw allow 123/udp
# With Firewalld (RHEL/CentOS)
sudo firewall-cmd --add-service=ntp --permanent
sudo firewall-cmd --reload
On all other client nodes in the LAN, simply edit their configuration to point directly to the Master’s IP (for example: server 192.168.1.10 iburst).
Battle-Tested Tips to Avoid Outages
- Quick guide to source status indicators:
^*: Primary reference source (NTP master) currently used for synchronization.
^+: Reliable secondary source, ready to take over if the primary source drops.
^?: Unreachable source. Check DNS resolution or verify that your firewall is not blocking outbound UDP port 123. - Never run
date -son Production servers: Manually setting the clock forces an abrupt time step. Backward time jumps can break transaction isolation in PostgreSQL and MySQL or freeze background worker queues. Chrony is much safer because it uses slewing: gradually speeding up or slowing down the clock ticks to align time smoothly without disrupting applications. - Monitor clock offset with
chronyc tracking: Check the System time line. If this value remains within0.000050 +/- 0.000100 seconds(sub-millisecond precision), your system is synchronizing exceptionally well.
