How to sync your Raspberry Pi to remote time servers

Published December 10, 2023

The Raspberry Pi does not come with a built-in clock. My Raspberry Pis lose their time within a few days. In order for the Raspberry Pi to retain the current time, we set the OS to make calls to remote time servers. In our case, we will make our devices sync to time.nist.gov time server.

How to enable your Raspberry Pi to sync time with network time servers

What is a Time Server?

A time server is a server that reads the actual time from a reference clock and distributes the relevant time to its clients. The most common protocol for distributing and syncronizing time is the Network Time Protocol (NTP).

For Linux, and the Raspberry Pi OS in particular, we will use the timedatectl program to see the current date and time.

timedatectl

Run this command:

timedatectl status

or

timedatectl

You should get output similar to this:

$ timedatectl status
               Local time: Thu 2023-11-23 20:55:24 EST
           Universal time: Fri 2023-11-24 01:55:24 UTC
                 RTC time: n/a
                Time zone: America/New_York (EST, -0500)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no

If you get an error about timedatectl not being installed, install it in the next step.

Install systemd

First make sure you have systemd installed on your Raspberry Pi OS. You can run this, just to make sure.

sudo apt install systemd systemd-timesyncd

That will install timedatectl as well.

Edit the timesyncd conf file:

sudo vi /etc/systemd/timesyncd.conf 

It will be something like this:

[Time]
#NTP=
#FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048

Change the FallbackNTP line to this:

FallbackNTP=time.nist.gov

Now, start systemd-timesyncd service:

sudo systemctl start systemd-timesyncd

Verify that Raspberry Pi is using the new Network Time Tervers

To verify that your Raspberry Pi is using the time.nist.gov time server, run this:

timedatectl show-timesync

You should get something similar to this:

$ timedatectl show-timesync
SystemNTPServers=139.78.100.163
FallbackNTPServers=time.nist.gov
ServerName=139.78.100.163
ServerAddress=139.78.100.163
RootDistanceMaxUSec=5s
PollIntervalMinUSec=32s
PollIntervalMaxUSec=34min 8s
PollIntervalUSec=4min 16s
Frequency=0

Enable or Disable Network Time Server

To enable the network time server synchronization, run this:

sudo timedatectl set-ntp true

To disable the network time server synchronization, run this:

sudo timedatectl set-ntp false

Let us keep the time server synchronization enabled.

Confirm whether the Raspberry Pi is Configured to Sync

Run this to configure whether the Raspberry Pi is configured to sync from the time server:

timedatectl

The output will be similar to this:

$ timedatectl
               Local time: Thu 2023-11-23 20:57:25 EST
           Universal time: Fri 2023-11-24 01:57:25 UTC
                 RTC time: n/a
                Time zone: America/New_York (EST, -0500)
System clock synchronized: no
              NTP service: active
          RTC in local TZ: no

Possible Errors

If you get an error like this:

$ sudo systemctl start systemd-timesyncd
Failed to restart systemd-timesyncd.service: Unit systemd-timesyncd.service is masked.

The solution would be to install systemd-timesyncd if it is not installed already.

sudo apt install systemd-timesyncd

Then, run the service:

sudo systemctl restart systemd-timesyncd

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: December 10, 2023.     This post was originally written on November 24, 2023.