Syncing my raspberry pi's time


#1

Hello. I’m trying to sync clocks from one raspberry Pi that has a rtc fitted and working to
two other raspberry pi’s. I’ve searched and tried a few suggestions with no luck, Ive edited /etc/ntp. Config on all 3 systems but they never sync. I only occasionally have Internet access so they never update by themselves.


#2

If you want the Pi with the RTC to be the “official” clock, then you need to set it up as a higher stratum than the other machines. I use stratum 3 for my primary NTP server, which allows it to sync to stratum 1 and 2 servers on the Internet.

Try something like the following in ntp.conf on the Pi with the RTC (change the 192.168.x.x IP addresses to fit your setup):

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery

# Permit all access over the loopback interface.
restrict 127.0.0.1

# -- CLIENT NETWORK -------
# Permit systems on this network to synchronize with this
# time service.  Do not permit those systems to modify the
# configuration of this service.
restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap nopeer

# Undisciplined Local Clock.
server 127.127.1.0
fudge 127.127.1.0 stratum 3

Then, on the “client” systems (change the serverpi.ip.address to the actual address of the Pi that has the RTC:

# --- OUR TIMESERVERS -----
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.

server serverpi.ip.address iburst

# Undisciplined Local Clock.
server 127.127.1.0
fudge 127.127.1.0 stratum 4

Any lines I didn’t paste (like driftfile, etc.) in either config can remain at the defaults.

Basically, an NTP server won’t get the time from another NTP server that has a higher stratum number, because it thinks that server is less accurate. If the stratum number is the same, then you need to use the peer line to allow the two systems to mutually sync. You can still use the server line for a one-way sync when the stratum number is the same, but this isn’t best practice.


#3

Thank you very much, I tried your solution with no success. I’m starting to think it’s my router/WiFi set up that is stopping ntp from working. This has been the only thing I’ve had trouble with setting up on raspberry pi’s and my system isn’t really standard. I will connect pi’s directly and try again.


#4

To see what the ntpd on each Pi is doing, run the following:

ntpq -p

This will show where the ntpd on that Pi is getting the time from, and how far off from that source it is. The one with the RTC should have only one line (LOCAL), while the others should have an extra line that references the one with the RTC. The fields (from first to last) output by ntpq are:

  • remote -> remote machine this machine is trying to connect to
  • refid -> where the remote machine is getting its time from
  • st -> stratum of this source
  • t -> type of connection (u = unicast, b = broadcast, l = local)
  • when -> time in seconds since the last received packet
  • poll -> how often (in seconds) to contact the source
  • reach -> octal bitmask of the last 8 contact attempts, with set bit meaning contact worked
  • delay -> network delay (in milliseconds) to that source
  • offset -> difference (in milliseconds) between our clock and this one
  • jitter -> measure of how much the offset varies each time we contact

Note, too, that NTP will not change the time if the clocks on the machines are too far off. You might need to manually set the time on the machines first…as long as they are only a few seconds off, that’s close enough.


#5

I tried this code, it actually listed the name of my osmc main box in the list, at the top, yet my slave raspberry pi’s haven’t updated.
Maybe it is the difference in time being too big that stops the sync from happening. I do turn the slave pi’s off. I think more rtc modules are needed, I’m determined to sort this one way or another this weekend.
Thanks for your help


#6

Add the following to /etc/rc.local on the slaves:

/usr/bin/sntp -S ip.of.master.pi

This will force a time sync to the master right after boot, regardless of how big the difference is. After that, ntpd should keep the time correct.