NTP

From Autopilot Wiki
Jump to: navigation, search

NTP is a Protocol for synchronizing clocks on computers. Used locally over a quiet network, it can synchronize clocks within dozens of microseconds. It has several different implementations, the default for RaspiOS is timesyncd, but if you want to synchronize clocks locally you will need to use another tool like ntpd or chrony.

See Configuration

The default tool for querying and setting time settings is timedatectl. To see the general settings, use timedatectl status

>>> timedatectl status
               Local time: Fri 2022-04-29 17:22:54 PDT
           Universal time: Sat 2022-04-30 00:22:54 UTC
                 RTC time: n/a
                Time zone: America/Los_Angeles (PDT, -0700)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

Setup Local Synchronization

Switch to Chrony

Since systemd-timesyncd doesn't include an NTP server, we need to disable it and switch to Chrony

sudo timedatectl set-ntp off
sudo apt install chrony -y

Configure a Server

The server will continue to synchronize with external servers, but allow clients on the local network to sync with it.

Edit your /etc/chrony/chrony.conf file such that it includes these lines:

local stratum 10
allow 192.168.0
makestep 0.001 3

Note that the allow config specifies our local IP address being underneath 192.168.0.*, adjust this if your local network uses a different IP range. We need to adjust the makestep from its default to make chrony adjust the system clock with a finer grain than 1 second (we use 1ms), but only on the first 3 synchronization cycles so we don't go backwards in time later on. The local configuration allows us to synchronize clients even if we haven't synchronized with external servers for awhile.

Then restart the chrony daemon to make our changes take effect

sudo systemctl restart chronyd

And see the docs

Configure a Client

Do the same process of switching to using chrony described previously, and then configure your client to get its time from the local server.

Change your /etc/chrony/chrony.conf file so it has these options:

# pool 2.debian.pool.ntp.org iburst
server 192.168.0.101 iburst minpoll 5 maxpoll 8
makestep 0.001 3

Where we disable the use of the default pool and use our local server instead --- switch 192.168.0.101 with the IP of your chrony server. We also use the iburst argument to synchronize with a burst of queries when we start the daemon, and the minpoll and maxpoll settings to configure how frequently the client queries the server as a power of 2 seconds (so 1 == 2 seconds, 2 == 4 seconds, 5 == 32, and so on).

Restart the chronyd process and then confirm you're synchronizing with your local server:

>>> chronyc sourcestats -v
                             .- Number of sample points in measurement set.
                            /    .- Number of residual runs with same sign.
                           |    /    .- Length of measurement set (time).
                           |   |    /      .- Est. clock freq error (ppm).
                           |   |   |      /           .- Est. error in freq.
                           |   |   |     |           /         .- Est. offset.
                           |   |   |     |          |          |   On the -.
                           |   |   |     |          |          |   samples. \
                           |   |   |     |          |          |             |
Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
==============================================================================
raspberrypi                 1   0     0     +0.000   2000.000    -68us  4000ms

You should only see the name or IP of your local server!

Over time chrony will learn the differences in clock speeds and compensate for the drift between them, so the synchronization should get better with more samples :)

References

Docs

See Also

Example Wikipedia Transclude

{{:wikipedia:SSH}} stub Synchronization