March 01, 2023

NTP server on Raspberry & Linux(Offline)

Uppdated 2023-02-01 !

The use case, creating an offline NTP, was that I'm on our boat sometimes don't have an Internet connection but always have a connected GPS. The GPS updates time on a Raspberry(RPi) running Signal K.

The actual issue is that there are other devices on the LAN that needs to be updated and then you need an NTP server.

Of course this setup is also valid for a RPi/server, with a RTC, acting as a NTP server in an isolated LAN.  

Introduction
Its always nice to know what you are dealing with so on Wikipedia you will get some basic info regarding NTP.

RPi NTP client
The latest RPi OS use the systemd service systemd-timesyncd as NTP client and please note that this package 
will be REMOVED installing the NTP server.

RPi NTP server
The recommendation for a RPi is to use the systemd service chronyd as NTP server. If installed it will also take over as the NTP client.

The old implementation is NTP and here you can see the differences. If you prefer this package, please check install hints below.

Installation
Install and configure server
Installing the "chrony" NTP server is done in a terminal/ssh session via 

sudo apt install chrony

and then edit the config file with

sudo nano /etc/chrony/chrony.conf


adding, at the end, these 3 lines


server 127.127.1.0
allow
local


will create a NTP pseudo server and using the internal clock as reference. Full specification of the settings in the file, and a manual, are found here. If not using a RTC comment the line so it looks like 


# rtcsync


Restart, after the changes, with


sudo systemctl daemon-reload
sudo systemctl restart chronyd


Check status with


sudo systemctl status chronyd


Configure clients

For other RPi:s/devices, acting as clients, in the LAN that has to be synched, edit the config file with 


sudo nano /etc/systemd/timesyncd.conf


and after the "[Time]" line just add 


NTP=nnn.nn.n.n


which is the IP, or hostname, of your NTP server. Then restart and check with


sudo systemctl daemon-reload
sudo systemctl restart systemd-timesyncd
sudo systemctl status systemd-timesyncd


Checking

The server

Issue the command 


chronyc sources -v


you will see something like 


210 Number of sources = 1


  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.

 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,

| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.

||                                                 .- xxxx [ yyyy ] +/- zzzz

||      Reachability register (octal) -.           |  xxxx = adjusted offset,

||      Log2(Polling interval) --.      |          |  yyyy = measured offset,

||                                \     |          |  zzzz = estimated error.

||                                 |    |           \

MS Name/IP address         Stratum Poll Reach LastRx Last sample               

===============================================================================

^* 127.127.1.0                10    6     1   62   +4870ns[+4870ns] +/-   10us


This shows you are up and running off line with your local NTP server. 


If you are going online, and wait a little, you will see 


chronyc sources

210 Number of sources = 5

MS Name/IP address         Stratum Poll Reach LastRx Last sample               

===============================================================================

^? 127.127.1.0                 3    6   377    59    -24ms[  +11us] +/-   53ms

^+ 158.69.35.226               2    6   377    28  -5199us[-5199us] +/-   77ms

^- 103.106.65.219              2    6   377    25    +24ms[  +24ms] +/-  219ms

^+ 5.9.113.140                 3    6   377   148    -35ms[  +35ms] +/-   80ms

^* 217.168.87.226              2    6   377    28  +1065us[  +29ms] +/-   35ms


There are several nice commands, for example


chronyc clients


which checks which clients that have accessed the server. But there could also be an 


501 Not authorised 


as a response. If so use the command with "sudo", or as "root".


The client 

With command


timedatectl show-timesync


and showing


SystemNTPServers=192.168.254.10

FallbackNTPServers=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org

ServerName=192.168.254.10

ServerAddress=192.168.254.10

RootDistanceMaxUSec=5s

PollIntervalMinUSec=32s

PollIntervalMaxUSec=34min 8s

PollIntervalUSec=4min 16s

NTPMessage={ Leap=0, Version=4, Mode=4, Stratum=10, Precision=-20, RootDelay=0, RootDispersion=0, Reference=7F7F0101, OriginateTimestamp=Mon 2021-05-10 18:42:17 CEST, ReceiveTimestamp=Mon 2021-05-10 18:42:17 CEST, TransmitTimestamp=Mon 2021-05-10 18:42:17 CEST, DestinationTimestamp=Mon 2021-05-10 18:42:17 CEST, Ignored=no PacketCount=7, Jitter=18h 10min 56.171885s }

Frequency=-6438843


There are a lot more to be said about NTP so just Google for your use case !


Remarks

Signal K

Using Signal K, as navigation aid, there is a plugin "@signalk/set-system-time" which will update the Raspberry(RPi) clock, or other chosen server HW, with the time information from the satellites. 


Install Signal K ?


Upgrade from Buster to Bullseye

After an upgrade the time sync didn't work so installing chrony without any config solved my issue.


NTP install

Hints....with same "local" functions as using "chrony" above.


sudo apt install ntp
#config
sudo nano /etc/ntp.conf
# adding
server 127.127.1.1 iburst prefer
fudge 127.127.1.1 stratum 8
# restarting, enable att power on
sudo systemctl restart ntp
sudo systemctl status ntp
sudo systemctl enable ntp
# checking server
ntpq -p
# checking client
timedatectl

4 comments:

  1. Great post! I'd been looking for a way to sync all my devices to the GPS, and since the RPi syncs through SignalK, it became easy to set up the NPT and point everthing else to it. Thanks!

    ReplyDelete
  2. Thank you - works perfectly

    ReplyDelete

Feel free to leave a comment ! ... but due to a lot of spam comments I have to moderate them. Will reply ASAP !