Self-Hosted Network Time with Chrony
7 min read
Every box on the LAN talking to the public NTP pool is twenty machines' worth of queries going out over a link that will eventually go down, and when it does, twenty clocks start drifting independently. That matters more than it sounds. ZFS send/recv streams, incremental backups, correlated logs across hosts, TLS certificate validity windows and TOTP codes all care about clocks agreeing with each other. During a WAN outage they care about the LAN agreeing with itself far more than about the LAN agreeing with UTC.
The fix is one time server on the router. The router is always up, it already sits on every subnet, and it is the one box that will still be reachable when the uplink is not.
Why chrony and not ntpd
net-misc/ntp is effectively unmaintained upstream, and Gentoo steers new installs toward net-misc/chrony. Beyond that, chrony is simply a better fit for the situation:
- Fast initial sync.
iburstsends a burst of packets at startup instead of one poll per interval, andmakestepallows a hard step of the clock early on rather than slewing a large offset for an hour. - Tolerates intermittent links, VMs and laptops. chrony was written for machines that suspend, lose the network, or run under a hypervisor with a lying TSC. ntpd assumes a permanently connected server.
- Access control is one line.
allow 192.168.1.0/24per internal subnet, instead of ntpd'srestrict default ignoremaze where one wrong line silently blocks everything. - Real tooling.
chronyc tracking,sources,selectdataandclientstell you what the daemon actually thinks, which matters when you are the only person who will ever debug it.
The server config
On the router, /etc/chrony/chrony.conf:
pool 2.gentoo.pool.ntp.org iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
# Internal subnets only. Nothing here opens the WAN.
allow 192.168.1.0/24
allow 192.168.10.0/24
allow 192.168.20.0/24
# Keep serving time to the LAN when the uplink is down.
local stratum 10
bindcmdaddress 127.0.0.1
Line by line:
pool ... iburstpicks upstream servers from a pool name. Check the path your distribution ships; Gentoo's chrony uses/etc/chrony/chrony.conf, some distributions use/etc/chrony.conf.driftfilepersists the measured frequency error of the local oscillator, so a restart does not relearn it from scratch.makestep 1.0 3steps the clock instead of slewing if the offset is over one second, for the first three updates only. That is what gets a freshly booted box correct in seconds rather than an hour, without allowing jumps later.rtcsynchands the kernel responsibility for writing the system time back to the hardware clock periodically, so a cold boot starts close.allowis the access list: which client subnets may ask this server for time. One line per internal subnet: LAN, WiFi, VPN, storage VLAN.local stratum 10is the important one. Stratum is distance from a reference clock: a GPS or atomic source is stratum 0, a server directly attached to it is stratum 1, and each hop adds one. Stratum 10 is deliberately terrible, so any real upstream beats it. When the WAN is down and the router has no upstream at all,local stratum 10says "serve my own clock anyway, and advertise it as low quality." The LAN stays internally consistent instead of every client falling back to nothing.bindcmdaddress 127.0.0.1keeps thechronyccommand socket on loopback so nobody off-box can poke the daemon.
Firewall: udp/123 on internal interfaces only
chrony binds the NTP port on every address by default, exactly as ntpd did. The firewall is the boundary, not the daemon config, and allow is not a substitute for it. Accept udp/123 on the internal interfaces and on nothing else:
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
iifname { "br-lan", "wg0" } udp dport 123 accept
}
}
The type filter hook input line is not decoration. Without it nft creates a
plain regular chain, nothing is ever dispatched to it, and the rule below it
is never evaluated. The config loads without error and does exactly nothing,
which is the worst kind of firewall rule: you believe port 123 is scoped to
two interfaces when it is in fact open on all of them. Check with
nft list ruleset and confirm the chain shows a hook.
Substitute your own interface names, and fold this into your existing input
chain rather than pasting a policy drop chain onto a running router, which
will drop everything else you had allowed. The point is that the WAN interface is not in that list. An NTP server reachable from the internet is an amplification reflector waiting to happen.
Clients
Every other box gets a one-line config:
server 192.168.1.1 iburst
That is the whole file, plus driftfile and makestep if you want them there too. No pool lines, no upstream, no second opinion. Verify from the client:
chronyc tracking
chronyc sources
tracking should show a stratum exactly one above the router's, and sources should show the router marked ^*, meaning "selected synchronisation source." From the router, see who is actually asking:
chronyc clients
The first three minutes look broken
This trips people up every time. Immediately after starting chronyd, chronyc tracking reports something like:
Reference ID : 7F7F0101 ()
Stratum : 10
7F7F0101 is hex for 127.127.1.1, the traditional address for a local fallback clock, and the stratum is the 10 you configured. It looks like the server never found an upstream. It has not selected one yet, which is different.
chronyc selectdata
A D in the state flags means the root distance is still too large: chrony has one or two samples from the source and cannot yet bound the error tightly enough to trust it. That clears after a few polls. Do not restart the daemon, do not start rewriting the config. Wait three minutes and look again.
Swapping out ntpd on Gentoo
Install chrony first, write the config, and only then take ntpd down, so the window with no time daemon is a few seconds.
Run the emerge on its own, interactively; do not paste it in the middle of a block of other commands, because -a will eat the next line as its answer:
emerge -a net-misc/chrony
Then:
/etc/init.d/ntpd stop
rc-update del ntpd default
rc-update del ntp-client default
rc-update add chronyd default
/etc/init.d/chronyd start
ntp-client is the one-shot "set the clock at boot" service and ntpd is the daemon; both come from net-misc/ntp, so both go away with the package. Clean up:
emerge --deselect net-misc/ntp
emerge -a --depclean net-misc/ntp
Check rc-update show default afterwards to confirm nothing still references the old services.
Worth adding
- Hand the server out over DHCP. DHCP option 42 (NTP servers) means new devices find the router without per-host config. Set it in your DHCP server's scope for each subnet.
- A second server. If you have a NAS that is always on, run chrony there too and give clients both, with
preferon the router line so it wins while it is healthy. - Hardware timestamping.
hwtimestampon a NIC that supports it removes a chunk of software jitter. Check whether your NIC and driver actually support it before adding the line; most consumer hardware does not. - Not PTP. PTP gets you sub-microsecond accuracy and wants switch support to do it properly. For a home lab or a small office, chrony over plain NTP lands inside a millisecond on the LAN, which is several orders of magnitude better than anything here needs.