Kovasky Buezo

Set Up a GPS NTP Server with chrony for Your Homelab

By 3 min readedited on

Terminal output of chronyc sources and chronyc tracking: GPS selected as the stratum 1 reference, system time 0.000018 seconds off

Intro

I wanted to easily synchronize the time between my VMs and decided to set up an ntp server to accomplish this. Since a USB GPS device is relatively inexpensive, I thought it would be a great addition to the project.

For the following examples, I used aptitude and vim.

How to Set Up Chrony with gpsd

Setting up chrony with a GPS turns your host into a stratum 1 NTP server. The USB GPS locks onto satellite time, gpsd reads it from the serial device, and chrony uses it as a reference clock and serves it to the rest of the LAN. My VMs sync to that host instead of reaching out to public NTP servers. This matters for identity, as services relying on Kerberos reject logins when a host’s clock drifts more than 5 minutes from the domain controller.

Prerequisite Packages

Run the following command to ensure we have the necessary packages:

sudo apt-get update && sudo apt-get install gpsd gpsd-clients chrony

Check /dev for GPS

Once the gps is connected to the pc, we need to figure out the assigned device. This can be any of /dev/ttyUSB0, /dev/ttyACM0 or /dev/ttyAMA0.

If unsure of which device it is, we can always run:

cat /dev/$device

We should be able to see a stream with coordinates and strings like “GPGGA” and “GPGSA”.

Modify gpsd.conf and start the service

After getting the correct device, we will need to modify the configuration file located at “/etc/default/gpsd”. It should contain the following:

/etc/default/gpsd
START_DAEMON="true"
USBAUTO="true"
DEVICES="/dev/ttyACM0"
GPSD_OPTIONS="-n"

After this we should enable and start the service:

sudo systemctl enable gpsd && sudo systemctl start gpsd

Modify chrony.conf and start the service

The configuration file will either be in “/etc/chrony.conf” or “/etc/chrony/chrony.conf”

chrony.conf
driftfile /var/lib/chrony/chrony/drift

allow

refclock SHM 0 refid GPS precision 1e-1 offset 0.9999 delay 0.2
refclock SHM 1 refid PPS precision 1e-7

After this we should enable and start the service:

sudo systemctl enable chronyd && sudo systemctl start chronyd

Checking chrony status

Theres a few things we can check to see if everything was set up correctly. To see if chrony takes in the gps data, we can run the following:

chronyc sources

And to see if it is working as intended, we can run:

chronyc tracking

The output should look similar to the image at the top of this post.

Troubleshooting

If chronyc sources comes back empty, check that gpsd is running with systemctl status gpsd and that /dev/ttyACM0 is actually your GPS.

If chronyc tracking shows no reference, run gpsmon and make sure the GPS has a fix. You should see coordinates and the GPGGA/GPGSA strings. Without a fix, gpsd has nothing to feed the SHM refclocks.

The refclock SHM 1 refid PPS line only does something if your GPS outputs a PPS signal. Most cheap USB units don’t, so the SHM 0 line is the one doing the work.

Done!

After following these steps, we should have a working ntp server and all that is left is to point any client to the ip of the host running chrony.

↑↓ selectEnter openEsc close