Raspberry Pi - Synchronize time in an environment where the Internet is not available

 Single Board Computer (SBC), such as Raspberry Pi, does not have a Real Time Clock (RTC) that maintains the current time.  At the time of system boot, they connect to the Internet's time server and synchronize the current time.  If the Internet is not connected, the last time the system was used remains the same. In most cases, there is no problem because it is an environment where Internet connectivity is possible. However, there are times when Internet access is blocked due to security issues such as well.

Recently I had to build an environment like the following picture.

One Raspberry Pi connected WiFi and LAN to two different networks (192.168.11.0/24 and 192.168.10.0/24).
And the rest of the Raspberry Pi connected to the LAN network (192.168.10.0/24). Therefore, Raspberry Pi connected to LAN is blocked from internet access.

The Raspberry Pi connected to the 192.168.10.0/24 network has no Internet access and only needs to communicate with Raspberry Pi using the 192.168.10.100 IP. But there is one problem. When the system is restarted, Raspberry Pi connected to the 192.168.10.0/24 network cannot synchronize time because it does not have an Internet connection. The 192.168.10.100 Raspberry Pi, which is inevitably Internet-connected, should be used to synchronize time.


Time Synchronization with HAProxy

How to use HAProxy is explained  detailed  in 

The HAProxy can be installed and used in 192.168.10.100 Raspberry Pi. I will not explain how to use HAProxy because it is explained in detail in the above link.


Time Synchronization with a local time server

The second method is to install and use the local time server on 192.168.10.100 Raspberry Pi. This is the most common and easy way.

The latest Raspberry Pi OS(Raspberry Pi OS) uses systemd's timesyncd service instead of ntpd service for time synchronization.

 

Modifying the configuration files of Raspberry Pi without Internet access

Add information from Raspberry Pi with Internet access to the host file as follows:
"192.168.10.100 midiplayer.rpi4"

root@MidiPi-12:~# cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 MidiPi-12
192.168.10.100 midiplayer.rpi4
#::1 localhost ip6-localhost ip6-loopback
#ff02::1 ip6-allnodes
#ff02::2 ip6-allrouters

 

Then add the NTP entry in the /etc/systemd/timesyncd.conf file as follows:

root@MidiPi-12:~# cat /etc/systemd/timesyncd.conf
[Time]
NTP=midiplayer.rpi4
Servers=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org


Install local time server(NTP) on the Raspberry Pi with Internet access

Raspberry Pi, which has no Internet access, will attempt to connect to Raspberry's UDP port No. 123 with an address 192.168.10.100 using timesyncd service. Therefore, install the local time server ntp on Raspberry Pi at the address 192.168.10.100.

root@midiplayer:~# apt install ntp
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libevent-core-2.1-6 libevent-pthreads-2.1-6 libopts25 sntp
Suggested packages:
  ntp-doc
The following NEW packages will be installed:
  libevent-core-2.1-6 libevent-pthreads-2.1-6 libopts25 ntp sntp
0 upgraded, 5 newly installed, 0 to remove and 118 not upgraded. 
....
.... 

 

Now check the ntp service and service port. The ntp service starts automatically with the installation.

root@midiplayer:~# service --status-all
 [ - ]  alsa-utils
 [ - ]  apparmor
 [ + ]  avahi-daemon
 [ + ]  bluetooth
 [ - ]  console-setup.sh
 [ + ]  cron
 [ + ]  dbus
 [ + ]  dhcpcd
 [ + ]  dphys-swapfile
 [ + ]  fake-hwclock
 [ - ]  fio
 [ - ]  hwclock.sh
 [ - ]  keyboard-setup.sh
 [ + ]  kmod
 [ - ]  lightdm
 [ + ]  monit
 [ + ]  mosquitto
 [ + ]  networking
 [ - ]  nfs-common
 [ + ]  ntp
 [ - ]  paxctld
 [ - ]  plymouth
 [ - ]  plymouth-log
 [ + ]  procps
 [ + ]  raspi-config
 [ ? ]  rng-tools
 [ - ]  rpcbind
 [ - ]  rsync
 [ + ]  rsyslog
 [ + ]  ssh
 [ - ]  sudo
 [ + ]  triggerhappy
 [ + ]  udev
 [ - ]  x11-common

 

And check the port of use. It can be seen that UDP port 123 is in service.

root@midiplayer:~# netstat -nlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      483/sshd
tcp        0      0 0.0.0.0:1880            0.0.0.0:*               LISTEN      386/node-red
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      790/sshd: root@pts/
tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN      453/mosquitto
tcp6       0      0 :::22                   :::*                    LISTEN      483/sshd
tcp6       0      0 ::1:6010                :::*                    LISTEN      790/sshd: root@pts/
tcp6       0      0 :::1883                 :::*                    LISTEN      453/mosquitto
udp        0      0 0.0.0.0:39167           0.0.0.0:*                           390/avahi-daemon: r
udp        0      0 0.0.0.0:21928           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21929           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21930           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21931           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21932           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21933           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21934           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21935           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21936           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21937           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21938           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21939           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21940           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21941           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21942           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21943           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21944           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21945           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21946           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:21947           0.0.0.0:*                           961/multimidicast
udp        0      0 0.0.0.0:68              0.0.0.0:*                           394/dhcpcd
udp        0      0 192.168.11.100:123      0.0.0.0:*                           1129/ntpd
udp        0      0 192.168.10.100:123      0.0.0.0:*                           1129/ntpd
udp        0      0 127.0.0.1:123           0.0.0.0:*                           1129/ntpd
udp        0      0 0.0.0.0:123             0.0.0.0:*                           1129/ntpd
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           390/avahi-daemon: r
udp6       0      0 :::49262                :::*                                390/avahi-daemon: r
udp6       0      0 fe80::109f:2450:e9d:123 :::*                                1129/ntpd
udp6       0      0 fe80::dea6:32ff:fe9:123 :::*                                1129/ntpd
udp6       0      0 ::1:123                 :::*                                1129/ntpd
udp6       0      0 :::123                  :::*                                1129/ntpd
udp6       0      0 :::5353                 :::*                                390/avahi-daemon: r
raw6       0      0 :::58                   :::*                    7           394/dhcpcd


Reboot the Raspberry Pi without Internet access

Now all the settings are over. Restart Raspberry with no internet connection and check the current time.

And you can check the results of the time synchronization operation with the systemctl status systemd-timesyncd command.

root@MidiPi-12:~# systemctl status systemd-timesyncd.service
Warning: The unit file, source configuration file or drop-ins of systemd-timesyncd.service changed on disk. Run '
systemctl daemon-reload' to reload units.
 systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/systemd-timesyncd.service.d
           └─disable-with-time-daemon.conf
   Active: active (running) since Thu 2020-11-19 20:40:56 KST; 2min 1s ago
     Docs: man:systemd-timesyncd.service(8)
 Main PID: 441 (systemd-timesyn)
   Status: "Idle."
    Tasks: 2 (limit: 4442)
   CGroup: /system.slice/systemd-timesyncd.service
           └─441 /lib/systemd/systemd-timesyncd

Nov 19 20:40:56 MidiPi-12 systemd[1]: Starting Network Time Synchronization...
Nov 19 20:40:56 MidiPi-12 systemd[1]: Started Network Time Synchronization.
Nov 19 20:41:06 MidiPi-12 systemd-timesyncd[441]: Timed out waiting for reply from 192.168.10.100:123 (midiplayer.rpi4).


Wrapping up

Time synchronization is of considerable importance in SBCs without RTCs. If it is difficult to connect to the Internet, it is easy to synchronize time using a PC that can access the Internet or a PC that is using an RTC.




댓글

이 블로그의 인기 게시물

Connecting to SQL Server on Raspberry Pi

Making VoIP Phone Using Raspberry Pi

MQTT - Mosquitto MQTT Broker setup on the Ubuntu 20.04