Architecture

Clock Skew on Servers: Why Time Drifts and How to Fix It with chrony

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

Every server has a clock, and no two clocks agree perfectly. The difference between them is called clock skew, and the gradual change of a single clock away from true time is called drift. On one machine a few hundred milliseconds rarely matters. Across several servers that sign tokens, write logs, expire cache entries or order database events, the same gap can cause failed logins, confusing incident timelines and subtle data bugs. This guide explains where skew comes from, what it breaks, and how to keep a Linux VPS accurate with chrony.

Where Clock Skew Comes From

A computer keeps time with a hardware oscillator whose frequency changes slightly with temperature and load. The operating system counts ticks from that source, so small frequency errors add up to seconds per day if nothing corrects them. In a virtual machine there is an extra layer: the hypervisor exposes a virtual clock source, and a VM that is paused, migrated or restored from a snapshot can wake up with a clock that is visibly wrong.

Common causes include:

  • No time synchronization service installed or enabled.
  • Outbound UDP port 123 blocked by a firewall, so NTP queries never return.
  • A VPS restored from an old snapshot and not yet resynchronized.
  • Two time services (for example systemd-timesyncd and chronyd) fighting each other.
  • Applications running in containers that assume the host clock is correct when it is not.

What Breaks When Clocks Disagree

AreaSymptom of skew
TLS certificates"certificate not yet valid" errors when the clock runs behind
JWT and OAuth tokenstokens rejected because iat or nbf is in the future, or accepted too long
TOTP two-factor codesvalid codes rejected because the 30-second window does not line up
Logs and tracingevents across servers appear in the wrong order during an incident
Databases and cachesTTLs expire early or late; "last write wins" picks the wrong value
Cron and schedulersjobs run at unexpected times after a large correction

The last two points matter most in distributed systems. If two nodes resolve conflicts by timestamp, the node with the faster clock silently wins. This is why systems that need strict ordering use logical clocks, version numbers or consensus protocols rather than trusting wall-clock time alone. Accurate NTP narrows the problem but never removes it completely.

Checking the Current State

Start with timedatectl:

timedatectl

Look for System clock synchronized: yes and NTP service: active. If either says no or inactive, the server is not being corrected.

If chrony is installed, it gives more detail:

chronyc tracking
chronyc sources -v

In tracking, the System time line shows how far the local clock is from the reference, and Last offset shows the most recent correction. In sources, a line starting with ^* marks the server chrony is currently synced to.

Setting Up chrony on a Linux VPS

chrony handles intermittent networks and virtual machines well, which is why it is the default on many distributions.

On Debian or Ubuntu:

sudo apt update
sudo apt install chrony
sudo systemctl disable --now systemd-timesyncd 2>/dev/null
sudo systemctl enable --now chrony

On AlmaLinux or Rocky Linux:

sudo dnf install chrony
sudo systemctl enable --now chronyd

The configuration lives in /etc/chrony/chrony.conf on Debian-based systems and /etc/chrony.conf on RHEL-based ones. A sensible minimal setup:

pool pool.ntp.org iburst maxsources 4
makestep 1.0 3
rtcsync
driftfile /var/lib/chrony/chrony.drift
  • iburst speeds up the first synchronization after boot.
  • makestep 1.0 3 lets chrony jump the clock if it is more than one second off during the first three updates; after that it slews gradually so time never moves backwards abruptly.
  • rtcsync keeps the hardware clock updated.

Restart and verify:

sudo systemctl restart chrony    # chronyd on RHEL-based systems
chronyc makestep                 # optional: force an immediate correction
chronyc tracking

If synchronization never succeeds, confirm that outbound UDP 123 is allowed in both the host firewall and any cloud firewall in front of the server.

Operational Checklist

  1. Run exactly one time service per machine.
  2. Allow outbound UDP 123 to your NTP sources.
  3. Keep every server in UTC (timedatectl set-timezone UTC) and convert to local time only in the user interface.
  4. After restoring a snapshot, check chronyc tracking before putting the server back into service.
  5. Alert when the offset exceeds a threshold you choose, for example by exporting chrony metrics to your monitoring stack.
  6. In application code, allow a small leeway when validating token timestamps rather than requiring exact agreement.
  7. Never use wall-clock timestamps as the only way to order writes across nodes.

When you build a multi-server setup, synchronized clocks are part of the foundation, just like firewalls and backups. If you are planning such a setup, our guide on consistent hashing covers another building block, and you can launch test servers from the deploy page.

FAQ

How much clock skew is acceptable?

It depends on the workload. For web servers and logs, tens of milliseconds are usually fine. For token validation and TOTP, a few seconds can already cause failures. With chrony running against healthy NTP sources, a VPS typically stays well within that range; measure your own servers with chronyc tracking rather than assuming.

Should I use chrony or systemd-timesyncd?

systemd-timesyncd is a simple SNTP client that is fine for a single desktop-style machine. chrony is a full NTP implementation that handles virtual machines, network interruptions and larger corrections better, and it provides detailed diagnostics. For servers, chrony is the more common choice.

Can I fix clock skew inside a Docker container?

Containers share the host kernel clock, so you cannot set a different time inside a normal container. Fix synchronization on the host and every container on it will follow.

Related guides