How to sync Raspberry Pi system clock?

4.2k Views Asked by At

I have built a custom Linux OS for Raspberry Pi CM4 using Buildroot. I am able to boot it but my system clock is not synchronized. Its set to some static time and I want to show real time instead. I have selected BR2_PACKAGE_RTC_TOOLS=y, BR2_PACKAGE_UTIL_LINUX_HWCLOCK=y and BR2_PACKAGE_NTP=y, BR2_PACKAGE_NTP_NTPDATE=y, BR2_PACKAGE_NTP_NTPTIME=y in menuconfig. But still it's same result.

The timedatectl shows:

Local time: Tue 2022-01-18 11:52:05 UTC
           Universal time: Tue 2022-01-18 11:52:05 UTC
                 RTC time: n/a
                Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: no
              NTP service: active
          RTC in local TZ: no

The systemctl status systemd-timesyncd shows:

systemd-timesyncd.service - Network Time Synchronization
     Loaded: loaded (/usr/lib/systemd/system/systemd-timesyncd.service; enabled;
     Active: active (running) since Tue 2022-01-18 11:35:47 UTC; 17min ago
       Docs: man:systemd-timesyncd.service(8)
   Main PID: 194 (systemd-timesyn)
     Status: "Daemon is running"
      Tasks: 1 (limit: 1009)
        CPU: 396ms
     CGroup: /system.slice/systemd-timesyncd.service
             └─194 /usr/lib/systemd/systemd-timesyncd

Jan 18 11:35:46 br2rauc systemd[1]: Starting Network Time Synchronization...
Jan 18 11:35:47 br2rauc systemd[1]: Started Network Time Synchronization.

And timedatectl show-timesync results in:

FallbackNTPServers=time1.google.com time2.google.com time3.google.com time4.google.com
RootDistanceMaxUSec=5s
PollIntervalMinUSec=32s
PollIntervalMaxUSec=34min 8s
PollIntervalUSec=0
Frequency=0

I don't know why but /etc/systemd/timesyncd.conf looks like not set

[Time]
#NTP=
#FallbackNTP=time1.google.com time2.google.com time3.google.com time4.google.com
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048
#SaveIntervalSec=60

Do I need to set any value for NTP?

I also tried by enabling BR2_PACKAGE_HTPDATE=y and systemctl status htpdate.status shows:

htpdate.service - htpdate daemon
     Loaded: loaded (/usr/lib/systemd/system/htpdate.service; enabled; vendor pr
     Active: active (running) since Tue 2022-01-18 11:35:48 UTC; 22min ago
    Process: 221 ExecStart=/usr/sbin/htpdate -D -i /run/htpdate.pid $HTPDATE_ARG
   Main PID: 228 (htpdate)
      Tasks: 1 (limit: 1009)
        CPU: 26ms
     CGroup: /system.slice/htpdate.service
             └─228 /usr/sbin/htpdate -D -i /run/htpdate.pid -a -s -t https:/

Jan 18 11:35:48 br2rauc systemd[1]: Starting htpdate daemon...
Jan 18 11:35:48 br2rauc htpdate[228]: htpdate version 1.3.3 started
Jan 18 11:35:48 br2rauc htpdate[228]: www.google.com host or service unavailable
Jan 18 11:35:48 br2rauc systemd[1]: Started htpdate daemon.

As you can see above date and time is set to Jan 18. While booting Pi is not connected to internet. And I connect to internet using connman after system boot. But even after restarting systemd-timesyncd it's same result.

Can anyone please let me know how to properly synchronize the system clock?

Your help will be much appreciated.

thanks in advance

P.S: Please let me know if any info is missing here.

2

There are 2 best solutions below

0
On

htpdate did have an issue up to version 1.3.2 in combination with buildroot (actually non-glibc distributions). Please use the latest from https://github.com/twekkel/htpdate

In case htpdate is started before network is available, the time synchronization can take some time... try using '-m 5' to speed up that process.

0
On

Quick and easy way to sync your RPi clock:

sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"