NTP

This tutorial will show you how to sync and manage the time on your Raspberry Pi and in your home lab. note that this is not limited to the raspberry pi, but can be used on all linux distro’s.

As the Pi has no built in real time clock to maintain its time we’ll need something called NTP to do this for us. NTP or Network Time Protocol is a server-client protocol that gives us accurate timing over the internet. Thru out the world there are multiple time servers awaiting for your call.

While you can add an RTC to the pi to maintain its own time, it still has to be set somehow. This is where an NTP server comes in, thought you don’t need RTC for this to work. So lets get started.

The default setting for the RPi OS is to use the timedatectl package. Which actually uses NTP to sets its time. But lets see how we can configure this.

Equipment

Below is a list of the equipment used in this tutorial.

Recommended

Optional

  • Raspberry Pi Case
  • HDMI Cable
  • Monitor
  • USB Keyboard
  • USB Mouse

Using timedatectl to Sync your Raspberry Pi’s Time

The raspberry pi uses the timedatectl package by default to keep its time in sync. This section will go over the timedatectl and how to control it.

We are fortunate as most of the hard work has already been done, the Raspberry Pi OS already has the required configurations to keep its time in sync.

Changing the Network Servers for your Raspberry Pi

The RPi is configured to use the sync servers provided by the NTP pool, This pool of servers maintains an accurate time.

In some edge cases one might want to change the servers to a different pool.

To change the time sync servers, you will need to modify the “timesyncd” configuration file. You can begin editing this file by using the nano text editor and using the following command.

nano /etc/systemd/timesyncd.conf

Note that if one does not run this as ROOT one must use SUDO.
You will need to find the following line within this file. This line is where the NTP servers are defined.

#FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org

Replace that line with the following, changing the NTP servers to the ones you want to utilize.

In this example, we changed the time server to a server provided by the US National Institute of Standards and Technology.

FallbackNTP=time.nist.gov

Once you have made changes to the file, you will need to save and quit by pressing CTRL + X, followed by Y, then the ENTER key.

After changing the time sync servers on the Raspberry Pi, a restart of the “systemd-timesyncd” is required. You can restart this service by running the following command within the terminal.

sudo systemctl restart systemd-timesyncd

To check whether the Pi is actually using the new timezone you can use the following command in the terminal window (or ssh window). This will output information about the time sync, including the server(s) that is (are) used to synchronize time.

timedatectl show-timesync

Below an example of the data returned. You can see that our “ServerName” option is now set to “time.nist.gov“.

FallbackNTPServers=time.nist.gov
ServerName=time.nist.gov
ServerAddress=132.163.97.6
RootDistanceMaxUSec=5s
PollIntervalMinUSec=32s
PollIntervalMaxUSec=34min 8s
PollIntervalUSec=1min 4s
NTPMessage={ Leap=0, Version=3, Mode=4, Stratum=1, Precision=-29, RootDelay=244us, RootDispersion=488us, Reference=NIST, OriginateTimestamp=Sat 2022-11-05 12:32:42 GMT, ReceiveTimestamp=Sat 2022-11-05 12:32:42 GMT, TransmitTimestamp=Sat 2022-11-05 12:32:42 GMT, DestinationTimestamp=Sat 2022-11-05 12:32:42 GMT, Ignored=no PacketCount=1, Jitter=0 }
Frequency=-7393031

Controlling Network Time Sync.

Enabling or disabling the time sync on the Raspberry Pi is straightforward thanks to the timedateclt tool.

Lets see how we can enable or disable the network service. One reason for doing this, is to set your time manually. When not disabling the time sync, the local time wil be overridden by the server time of the selected time servers.

To enable (true) or disable (false) the network time synchronization on your Raspberry Pi, You’ll use the timedatectl command as follows:

sudo timedatectl set-ntp truesudo timedatectl set-ntp false

To confirm whether the Raspberry Pi is configure to sync it’s time from the network we’ll use the timedatectl command once again.

timedatectl

From this result, the “NTP service:” will either be marked as “inactive” (Disabled) or “active” (Enabled).

           Local time: Sat 2023-12-30 01:43:06 CET
       Universal time: Sat 2023-12-30 00:43:06 UTC
             RTC time: n/a
            Time zone: Europe/Brussels (CET, +0100)
System clock synchronized: no
          NTP service: inactive
      RTC in local TZ: no

Setting of the Timezone

When your Raspberry Pi syncs the time from a networked server, it typically retrieves the time in UTC. Afterwards it applies an offset based on the timezone to get the time for your entered timezone. By default, Raspberry Pi OS is typically set to the “Europe/London” time zone.

To change this we can once again use the timedatectl command as shown below. Do mate sure you replace “TIMEZONE” with the correct one for your timezone.

sudo timedatectl set-timezone TIMEZONE

eg. If you wanted to change your time zone to “Australia/Hobart“, we would use the following.

sudo timedatectl set-timezone Australia/Hobart

To get a list of all possible timezones enter following command:

timedatectl list-timezones

This will spit out a list that is way to long to scroll thru. So to shorten it we’ll use the grep command right after it, eg for Europe:

timedatectl list-timezones | grep Europe

Manually Setting your Raspberry Pi’s Time

If you have disabled network time sync on your Raspberry Pi, you will want to set the time manually. Please note that since the Pi doesn’t contain a real-time clock, this value will become incorrect as soon as your device is powered off. So if you’ll do this an RTC is recommended unless you want to set the time manually each time the RPi loses its power.

To manually set the time or date, you will want to use the following command.

sudo timedatectl set-time TIMEORDATE

To manually set your Raspberry Pi’s time with this command, you will want to use one of the following formats.

  • Y:M:D HH:mm:ss
  • Y:M:D
  • HH:mm:ss

For example, if we wanted to set the time to 3 PM, we would use the command below.

sudo timedatectl set-time '15:00:00'

Conclusion

A time server allows your Raspberry Pi to keep the correct time without relying on a real-time clock.

Please comment below if you have questions about getting your Raspberry Pi to synchronize its time.

Geef een reactie

Je e-mailadres zal niet getoond worden. Vereiste velden zijn gemarkeerd met *