Make Time for Resiliency
Table of Contents
The Project #
Every machine on a network needs to agree on the time. It ensures that logs line up across hosts, TLS certificates validate, backups and cron jobs fire when they should. For a long time I outsourced time sync on my network to a public NTP pool over the internet. This gets me within a few milliseconds and is adequate for most things. But I don’t always stop at adequate. It’s a dependency on a service I don’t control. It also degrades if my internet connection does.
Inspired by Jeff Geerling and his obsession with time keeping, I decided to build my own stratum 1 time server. It is a Raspberry Pi 4 with a GPS receiver and a pulse-per-second signal, mounted in a weatherproof enclosure on the outside of the house, and powered over Ethernet. This lets me hold my whole network within a few microseconds of UTC. It doesn’t relay on anything upstream of my home.
What “stratum” means #
NTP ranks time sources by how many hops they are from a reference clock.
Stratum 0 is the reference itself: atomic clocks, GPS satellites, the cesium standards that define the second. You (or at least I) can’t reach a stratum 0 device over the network.
Stratum 1 time sources are synced directly to a stratum 0 source. My time server is a Stratum 1 server. It reads time straight from the GPS satellites’ singals.
Stratum 2 time sources are synced to stratum 1 sources over the network. Each hop downstream adds another stratum number and possibly a little more uncertainty. This is because every network link introduces latency and jitter.
Most devices on the internet sync to stratum 2 or 3 servers. Single-digit millisecond accuracy is normal. By running my own stratum 1 server, I skip the internet leg entirely. Because I use a local reference clock, the only network between it and my machines is my own LAN.
Why run your own #
Accuracy is a plus, but the real reason is the independence that comes with self hosting a service.
Public NTP needs an internet path. When that path is slow, congested, or gone, time quality degrades, and eventually stops. A lot of computing infrastructure depends on accurate time. For example, TLS certificate checks, Kerberos, log correlation across hosts, and anything timestamped for security or forensics needs to know the time accurately. When the clocks drift apart, that infrastructure can fail in ways that can be tricky to debug. At least in part because time is not the first thing you suspect.
A local stratum 1 source also removes jitter. Syncing over the internet means time packets have to deal with whatever the network conditions are that day. NTP has to estimate and cancel that out. On a LAN, the round trip is sub-millisecond and usually very stable, so the discipline is tighter. My Pi’s clock tracks UTC to within a few nanoseconds. I have measured offsets around 3 microseconds.
There’s also a privacy aspect: a local source is one less thing bit of telemetry on the internet.
The hardware #
I was able to build this from easily obtainable hardware.
To minimize antenna cable length, I mounted the pi in a weather-sealed box I’d already mounted on the outside wall to bring coax for my amateur radio antennas into the house. The mount point gives the GPS antenna a view of the sky sufficient to lock enough satellites for a reliable time sync.
The key components are:
- Raspberry Pi 4 Model B for the compute. This does not have to be powerful; a 3B+ would work too.
- Waveshare PoE HAT (for Pi 4B/3B+) powers the unit over an Ethernet cable. The box is outside, so running power and data over one Cat6 keeps it to a single run. This is not a requirement, but it works greay where you can’t provide separate power.
- GT-U7 GPS module. This is a u-blox NEO-7M wired to the UART on the Pi. This is the reference clock. It listens to the GPS constellation and reports time, position, and a fix status. It feeds the timing signal to the software.
- 38 cm GPS whip antenna mounts on top of the box for a clear view of the sky. GPS needs line of sight to several satellites, which is why the whole unit lives outside.
- DS3231 RTC to hold time on the Pi itself. This is a temperature-compensated real-time clock. A Pi has no battery-backed clock of its own, so without this it boots up thinking it’s 1970 until GPS or NTP hands it a fix. The DS3231 holds the time across reboots and gives chrony a sane starting point on startup.
How it works #
The GPS module communicates with the Pi over the serial UART at 9600 baud, emitting NMEA sentences. (NMEA is a standard for data communication among marine electronic devices, such as GPS receivers.) These sentences carry the time, but their arrival over a UART can include tens of milliseconds of jitter. That is sufficient for identifying which second is being reported, but not for precision.
Precision comes from a separate signal: the GPS module’s pulse-per-second (PPS) output, wired to one of the Pi’s GPIO pins. The module generates a pulse whose rising edge coincides exactly with the UTC second boundary. While the NMEA sentences are only approximately timed, the PPS edge provides a precise physical reference that disciplines the system clock, making this a stratum-1 source.
To take advantage of this GPS source, the Pi runs two pieces of software. gpsd reads the NMEA stream and hands it to chrony, the NTP daemon. Chrony uses NMEA to figure out which second it’s on, then locks onto the PPS edge from /dev/pps0 as the preferred source. Just in case the GPS module fails or loses lock, I have some internet-based servers configured in chrony as a fallback. This prevents my devices from losing all timing sync just because my preferred source is not working.
To use this source, I add two refclock lines in chrony’s config:
refclock SHM 0 refid NMEA offset -0.077 precision 1e-3 noselect
refclock PPS /dev/pps0 refid PPS lock NMEA prefer
Both lines define a local reference clock (the kind chrony reads directly rather than a server it queries over the network). The first reads shared memory segment 0, which is where gpsd drops the NMEA time it parsed off the serial port. (refid NMEA is the label chrony prints for that source.) offset -0.077 is a fixed correction for latency: by the time an NMEA sentence leaves the GPS, crosses the 9600-baud serial link, and passes through gpsd, its timestamp is about 77 milliseconds old, and the negative sign tells chrony the source reads slow. precision 1e-3 caps how accurate chrony should assume this source is, at about a millisecond. noselect tells chrony never to actually sync to NMEA. Its only job is to answer which second it is.
The second line keeps time accurate. It reads the pulse train from /dev/pps0 and labels it PPS. The important option is lock NMEA. A PPS signal is just a tick once a second. It contains no label for which second any given tick marks. By itself, it is useless for absolute time. Locking it to the NMEA source tells chrony to get the second of the day from NMEA and then snap to the precise PPS edge inside that second. prefer says that when everything is healthy, PPS wins over the internet fallback servers.
On a pi, we also need some kernel-side wiring in /boot/firmware/config.txt:
dtoverlay=pps-gpio,gpiopin=18
enable_uart=1
init_uart_baud=9600
The chrony config lines name /dev/pps0 and the serial port. Those two don’t exist until the lines in config.txt create them, at boot, before chrony starts running. dtoverlay=pps-gpio,gpiopin=18 loads the pps-gpio driver and wires GPIO 18 as the PPS input. The kernel timestamps each rising edge with sub-microsecond precision and exposes it as /dev/pps0, which is where the precision comes from. enable_uart=1 turns on the serial UART that gpsd reads the NMEA stream from, and init_uart_baud=9600 sets the 9600-baud rate the GPS module talks at.
With this software running, the Pi receives the stratum 0 signal and uses it to serve NTP to the rest of the network.
What it cost #
These prices are estimates and could vary widely these days.
| Part | Cost |
|---|---|
| Raspberry Pi 4B | $45–60 |
| Waveshare PoE HAT | $20–25 |
| GT-U7 GPS module | $10–15 |
| 38 cm GPS whip antenna | $8–12 |
| DS3231 RTC module | $5–8 |
| MicroSD card | $8 |
| Weatherproof enclosure + Cat6 + misc | $15–25 |
You’re looking at $110–150 all in, buying everything new. The Pi is the single biggest expense. If you have one already, the rest of the build is under $75. (Less if you don’t need PoE or can get a good signal indoors.) That’s a good value for a high-end update to your homelab.
Was it worth it? #
For me, yes. I like the improved accuracy, but I like most the independence from the internet.
Fun Fact: I’m a “Back to the Future” fan, so I gave the NTP server x.x.x.88 as its IP address. You gotta have some fun with this stuff.
Built on: Raspberry Pi 4B, Waveshare PoE HAT, GT-U7 (u-blox NEO-7M) GPS, DS3231 RTC, gpsd, chrony.