Difference between revisions of "NTP"

From Autopilot Wiki
Jump to: navigation, search
(Created page with "NTP is a Protocol for synchronizing clocks on computers. Used locally over a quiet network, it can synchronize clocks within dozens of microseconds == Setup Local Syn...")
 
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[NTP]] is a [[Protocol]] for synchronizing clocks on computers. Used locally over a quiet network, it can synchronize clocks within dozens of microseconds
+
[[NTP]] is a [[Protocol]] for [[Strategy For::Synchronization|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 <code>timedatectl</code>. To see the general settings, use <code>timedatectl status</code>
 +
 
 +
<pre>
 +
>>> 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
 +
</pre>
  
 
== Setup Local Synchronization ==
 
== Setup Local Synchronization ==
  
 +
=== Switch to Chrony ===
 +
 +
Since systemd-timesyncd doesn't include an NTP server, we need to disable it and switch to Chrony
 +
 +
<pre>
 +
sudo timedatectl set-ntp off
 +
sudo apt install chrony -y
 +
</pre>
 +
 +
=== 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 <code>/etc/chrony/chrony.conf</code> file such that it includes these lines:
 +
 +
<pre>
 +
local stratum 10
 +
allow 192.168.0
 +
makestep 0.001 3
 +
</pre>
 +
 +
Note that the <code>allow</code> 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 <code>makestep</code> 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 <code>local</code> 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
 +
 +
<pre>
 +
sudo systemctl restart chronyd
 +
</pre>
 +
 +
And see the [https://chrony.tuxfamily.org/faq.html#_how_do_i_make_an_ntp_server 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 <code>/etc/chrony/chrony.conf</code> file so it has these options:
 +
 +
<pre>
 +
# pool 2.debian.pool.ntp.org iburst
 +
server 192.168.0.101 iburst minpoll 5 maxpoll 8
 +
makestep 0.001 3
 +
</pre>
 +
 +
Where we disable the use of the default pool and use our local server instead --- switch <code>192.168.0.101</code> with the IP of your chrony server. We also use the <code>iburst</code> argument to synchronize with a burst of queries when we start the daemon, and the <code>minpoll</code> and <code>maxpoll</code> 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:
 +
 +
<pre>
 +
>>> 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
 +
</pre>
 +
 +
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 ==
 
== References ==
Line 11: Line 91:
 
* https://www.eecis.udel.edu/~mills/ntp.html - David Mill's big collection of NTP benchmarks and information
 
* https://www.eecis.udel.edu/~mills/ntp.html - David Mill's big collection of NTP benchmarks and information
 
* https://unix.stackexchange.com/a/178023/487659 - an elusive answer on local sync
 
* https://unix.stackexchange.com/a/178023/487659 - an elusive answer on local sync
 +
* https://unix.stackexchange.com/questions/305643/ntpd-vs-systemd-timesyncd-how-to-achieve-reliable-ntp-syncing - comparison of ntpd and systemd-timesyncd
 +
* https://www.linuxtechi.com/sync-time-in-linux-server-using-chrony/ - basic chrony usage information
 +
* [[References DOI::10.1109/CSNDSP49049.2020.9249632]] - "Analysis of Timekeeping in Experimentation," measures and compares NTP implementations on the raspi
 +
 +
=== Docs ===
 +
 +
* [https://chrony.tuxfamily.org/ Chrony Homepage]
 +
* [https://chrony.tuxfamily.org/doc/4.2/chrony.conf.html Chrony Configuration]
 +
 +
== See Also ==
 +
 +
* [[Localization]]
 +
 +
== Example Wikipedia Transclude ==
 +
 +
{{:wikipedia:SSH}}
  
 
[[Category:Software]]
 
[[Category:Software]]
 
[[Has Completion Status::stub]]
 
[[Has Completion Status::stub]]
[[About Problem::Synchronization]]
+
[[Strategy For::Synchronization]]

Latest revision as of 16:55, 26 October 2022

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