User avatar
jesdefer
Posts: 6
Joined: Tue Jul 24, 2018 10:53 am

NTP between two Pi Zero

Tue Jul 24, 2018 11:25 am

Hi! I'm trying to synchronize two Pi Zero (both headless) using NTP.

One Pi Zero is used as a NTP server (192.168.4.1) which is stratum 2 (don't look to an internet server, but itself clock). In the file associated to the ntp.conf of the server I have written:

Code: Select all

# ntp.conf
#
# file to hold drift date
driftfile /var/lib/ntp/ntp.drift
#
# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/
#
statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
#
# either get accurate time from upstream timeservers - ntp will call them
# as required, or use this machine's system time as the source
# uncomment lines starting ## in one or other option 
# option A - upstream timeserver
# Specify one or more NTP servers.
# If you can access timeservers through port 123
# list them here, else this timeserver will
#  just fall back on system time - uncomment the server 127.127.1.0 line
# Server pool names - change and uncomment
##server 0.ca.pool.ntp.org iburst dynamic
##server 1.ca.pool.ntp.org iburst dynamic
##server 2.ca.pool.ntp.org iburst dynamic
##server 3.ca.pool.ntp.org iburst dynamic
#
# Option B - system time as time source
# fall back to using this machine's system time if no internet ntp connectivity
# uncomment the next line if you are going to use this machine's
# clock as the time source		
server 127.127.1.0
#
# Set the timeserver stratum
# 'fudge' the server - make it look like a different stratum
# if there is internet ntp connectivity, make it a stratum 10 server
#  but if using system time it's going to be the prime source 
#  so make it stratum 1 or 2 
fudge 127.127.1.0 stratum 2
#
# Provide time to your local subnet
# adjust address to suit your private network
broadcast 192.168.4.255
#
In the client side (192.168.4.2) I have written the following lines in the ntp.conf file:

Code: Select all

# client configuration
#
# Allow ntp to make adjustments of greater than 1000 seconds
tinker panic 0
#
driftfile /var/lib/ntp/ntp.drift
#
# Use the ntp server - all local machines will be synchronized
# change this ip address to the address of your timeserver
server 192.168.4.1 iburst
#
# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
#
# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1
#
I have done all the commands possible to make the synchronization. Here it is an example of a combination of them in the client side:

Code: Select all

sudo systemctl stop ntp
sudo ntpdate-debian
sudo systemctl start ntp
For all these possibilities the answer is:

Code: Select all

no server suitable for synchronization found
Initially I thought that the problem was that the offset of time between client and server was too high, but I have change the date of the client to be as close as possible to the server using sudo date -s "07/20/2018 03:20", but this didn't fix my problem.
Moreover, I have checked if the port 123 was blocked, but in both sides was opened.

Is my server (host) blocking ntp packages?

I have also seen a big lag in my shell window associated to the client side after executing any command related with ntpdate.

Thanks!

jesdefer :D
Last edited by jesdefer on Thu Jul 26, 2018 9:25 am, edited 2 times in total.
Python and peace! :D

jesdefer

User avatar
lmarmisa
Posts: 1260
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: NTP between two Pi Zero

Tue Jul 24, 2018 1:40 pm

These commands will be useful:

Code: Select all

ntpq -pn
timedatectl status
First step: check that the server is working properly.

Second step: verify the subnet between both RPis (use the command ping on both sides).

Third step: verify that the client is able to synchronize date/time with the server.

epoch1970
Posts: 4500
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: NTP between two Pi Zero

Tue Jul 24, 2018 1:49 pm

You have no "restrict" option on the server side, which AFAIK defaults to a setup working in client role but not server role.
This page provides guidance and this one defines what the various options to restrict mean.

Within a LAN I usually use

Code: Select all

restrict -4 default notrap nomodify
restrict -6 default notrap nomodify
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

User avatar
jesdefer
Posts: 6
Joined: Tue Jul 24, 2018 10:53 am

Re: NTP between two Pi Zero

Tue Jul 24, 2018 3:21 pm

Thanks for all for your quick answers!

I have tried all of them, but the behaviour remains equal.

I have tried to add another Raspberry as a client and when I do:

Code: Select all

sudo ntpdate-debian
Both clients present the lag I have told you before.
I think is a problem of the server, isn't it?

jesdefer :D
Python and peace! :D

jesdefer

User avatar
lmarmisa
Posts: 1260
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: NTP between two Pi Zero

Tue Jul 24, 2018 6:37 pm

jesdefer wrote:
Tue Jul 24, 2018 3:21 pm

I have tried to add another Raspberry as a client and when I do:

Code: Select all

sudo ntpdate-debian
I do not know the command ntpdate-debian. Are you using a raspbian distro?.

I believe that you need to install the package ntp on both RPis:

Code: Select all

sudo apt-get install ntp
Then you have to configure the file /etc/ntp.conf properly.

If you change the contents of the configuration, you can restart the program typing this command:

Code: Select all

sudo service ntp restart
Other commands like ntpq -pn or timedatectl status are suitable to check if ntp is working properly:

Code: Select all

[email protected]:~ $ ntpq -pn
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 132.163.4.103   .STEP.          16 u    - 1024    0    0.000    0.000   0.000
#192.168.18.1    10.27.128.1      7 u  850 1024  377    0.392    5.763   5.460
+216.239.35.0    .GOOG.           1 u  819 1024  377  118.479   -0.073   0.228
+216.239.35.4    .GOOG.           1 u  376 1024  377  129.493    0.217   0.973
*216.239.35.8    .GOOG.           1 u  911 1024  377   32.836   -0.046   0.269
-216.239.35.12   .GOOG.           1 u  832 1024  377  280.915    0.289   0.305
#128.138.140.44  .NIST.           1 u 110m 1024  100  167.271    1.400   2.876
[email protected]:~ $ timedatectl status 
      Local time: Tue 2018-07-24 20:36:23 CEST
  Universal time: Tue 2018-07-24 18:36:23 UTC
        RTC time: n/a
       Time zone: Europe/Madrid (CEST, +0200)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no
[email protected]:~ $ 

User avatar
jesdefer
Posts: 6
Joined: Tue Jul 24, 2018 10:53 am

Re: NTP between two Pi Zero

Wed Jul 25, 2018 7:02 am

lmarmisa wrote:
Tue Jul 24, 2018 6:37 pm

I do not know the command ntpdate-debian. Are you using a raspbian distro?.

I believe that you need to install the package ntp on both RPis:

Code: Select all

sudo apt-get install ntp
Then you have to configure the file /etc/ntp.conf properly.

If you change the contents of the configuration, you can restart the program typing this command:

Code: Select all

sudo service ntp restart
I think it was clear that all the devices in the net have previously installed ntp. It was my fault. I think I have properly configured my conf files and I do restart of the service everytime I change these files. The line ntpdate-debian is used to force the client to synchronize with the client.
lmarmisa wrote:
Tue Jul 24, 2018 6:37 pm
Other commands like ntpq -pn or timedatectl status are suitable to check if ntp is working properly:

Code: Select all

[email protected]:~ $ ntpq -pn
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 132.163.4.103   .STEP.          16 u    - 1024    0    0.000    0.000   0.000
#192.168.18.1    10.27.128.1      7 u  850 1024  377    0.392    5.763   5.460
+216.239.35.0    .GOOG.           1 u  819 1024  377  118.479   -0.073   0.228
+216.239.35.4    .GOOG.           1 u  376 1024  377  129.493    0.217   0.973
*216.239.35.8    .GOOG.           1 u  911 1024  377   32.836   -0.046   0.269
-216.239.35.12   .GOOG.           1 u  832 1024  377  280.915    0.289   0.305
#128.138.140.44  .NIST.           1 u 110m 1024  100  167.271    1.400   2.876
[email protected]:~ $ timedatectl status 
      Local time: Tue 2018-07-24 20:36:23 CEST
  Universal time: Tue 2018-07-24 18:36:23 UTC
        RTC time: n/a
       Time zone: Europe/Madrid (CEST, +0200)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no
[email protected]:~ $ 
When I execute this commands, what I get from the server is the following:

Code: Select all

[email protected]:~ $ sudo service ntp restart
[email protected]:~ $ ntpq -pn
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 127.127.1.0     .LOCL.           2 l   18   64    1    0.000    0.000   0.002
 192.168.4.255   .BCST.          16 B    -   64    0    0.000    0.000   0.002
[email protected]:~ $ timedatectl status 
      Local time: Fri 2018-07-20 06:21:37 CEST
  Universal time: Fri 2018-07-20 04:21:37 UTC
        RTC time: n/a
       Time zone: Europe/Madrid (CEST, +0200)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no
In the client side what I have is the following:

Code: Select all

[email protected]:~ $ sudo service ntp restart
[email protected]:~ $ ntpq -pn
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.4.1     .INIT.          16 u    -   64    0    0.000    0.000   0.000
[email protected]:~ $ timedatectl status 
      Local time: Fri 2018-07-20 06:29:20 CEST
  Universal time: Fri 2018-07-20 04:29:20 UTC
        RTC time: n/a
       Time zone: Europe/Madrid (CEST, +0200)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no
Python and peace! :D

jesdefer

User avatar
lmarmisa
Posts: 1260
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: NTP between two Pi Zero

Wed Jul 25, 2018 9:30 am

Hmmm. I think it's not a good idea to restart the service (sudo service ntp restart) and immediately look at its statistics (ntpq -pn). When you restart the service, the statistics are restarted too. :o

Therefore, wait a few minutes and then type the two commands ntpq -pn.

This link will help you to understand the outputs of the commands:

https://pthree.org/2013/11/05/real-life-ntp/

Anyway, although the statistics are incomplete, the ntp service seems to be working on both sides.

User avatar
jesdefer
Posts: 6
Joined: Tue Jul 24, 2018 10:53 am

Re: NTP between two Pi Zero

Thu Jul 26, 2018 9:23 am

I have just found out that I have to wait 5 minutes since I start my server. After this 5 minutes, I can synchronize my client using ntpdate:

Code: Select all

[email protected]:~ $ sudo ntpdate -B 192.168.4.1
26 Jul 07:58:00 ntpdate[574]: adjust time server 192.168.4.1 offset 0.000033 sec
Thanks for all the answers!
I will work on how to reduce this time. In case anyone can solve it please response!
Python and peace! :D

jesdefer

User avatar
lmarmisa
Posts: 1260
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: NTP between two Pi Zero

Thu Jul 26, 2018 10:14 am

jesdefer wrote:
Thu Jul 26, 2018 9:23 am
I have just found out that I have to wait 5 minutes since I start my server. After this 5 minutes, I can synchronize my client using ntpdate:

Code: Select all

[email protected]:~ $ sudo ntpdate -B 192.168.4.1
26 Jul 07:58:00 ntpdate[574]: adjust time server 192.168.4.1 offset 0.000033 sec
Thanks for all the answers!
I will work on how to reduce this time. In case anyone can solve it please response!
A few comments:

1) If ntp is working properly on the client RPi, you do not need the command ntpdate.

2) If ntp is running, your command sudo ntpdate -B 192.168.4.1 should not work because the standard NTP port is in use:

Code: Select all

[email protected]:~ $ sudo ntpdate -B 192.168.18.1
26 Jul 11:49:26 ntpdate[4524]: the NTP socket is in use, exiting
[email protected]:~ $ 
You should select the option -u instead:

Code: Select all

[email protected]:~ $ sudo ntpdate -u 192.168.18.1
26 Jul 11:51:06 ntpdate[4551]: adjust time server 192.168.18.1 offset -0.000393 sec
[email protected]:~ $
Therefore I am not sure if your ntp client is really running or not.

3) The time adjust of your command was minimal: only 33 usec!!!. Therefore, the time was already synchronized when you typed that command.

4) The time/date synchronization is a continuous process. The clock reference of the client is not perfect and, once synchronized, a small deviation will occur. These small deviations accumulate and must be corrected with successive synchronizations. This task is carried out automatically by the program ntp.

Return to “Networking and servers”