Time Sync from the Network on the Raspberry Pi
Originally published on pimylifeup.com by Emmet on 7 November 2022
Learn how to sync and manage the time on your Raspberry Pi with this helpful tutorial. The Raspberry Pi relies on external time servers for time synchronization, as it lacks its own real-time clock. In this guide, we’ll show you how to enable or disable network time sync, change time sync network servers, set the timezone, and even manually set the time on your Raspberry Pi.
You might need these:
Recommended
Optional
Raspberry Pi Network Time Sync:
Raspberry Pi OS utilizes the timedatectl package to keep its time in sync. By default, it calls time servers using the NTP protocol for accurate timekeeping. To change the time sync servers, you can modify the “timesyncd” configuration file. Simply find the line defining the NTP servers and replace it with your desired servers. After making changes, restart the “systemd-timesyncd” service to apply them. You can verify the new time servers using the “timedatectl show-timesync” command.
1. 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
2. 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
3. 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.
4. After making changes to the time sync servers your Raspberry Pi is using, you will need to restart the “systemd-timesyncd
” service.
You can restart this service by running the following command within the terminal.
sudo systemctl restart systemd-timesyncd
Copy
5. You can verify that your Raspberry Pi is now using the new time servers by using the following command within the terminal.
This command will output information about the time sync, including the server we are using to synchronize that time.
timedatectl show-timesync
6. Below is an example of the data returned from the above command. 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
Disabling or Enabling Network Time Sync:
Using the timedatectl tool, you can easily enable or disable network time synchronization. The “sudo timedatectl set-ntp true” command enables network time sync, while “sudo timedatectl set-ntp false” disables it. The command “timedatectl” will show if the NTP service is active or inactive.
1. To enable the network time synchronization on your Raspberry Pi, you will want to use the following command within the terminal.
sudo timedatectl set-ntp true
2. Likewise to disable the network time sync, all you need to do is use the command below.
sudo timedatectl set-ntp false
3. You can confirm whether your Raspberry Pi is configured to sync the time from the network using the timedatectl command.
timedatectl
From this result, the “NTP service:
” will either be marked as “inactive
” (Disabled) or “active
” (Enabled).
Local time: Sat 2022-11-05 12:42:30 GMT
Universal time: Sat 2022-11-05 12:42:30 UTC
RTC time: n/a
Time zone: Europe/London (GMT, +0000)
System clock synchronized: yes
NTP service: inactive
RTC in local TZ: no
Setting the Timezone:
When syncing time from networked servers, Raspberry Pi retrieves time in UTC and then applies an offset based on the timezone. By default, Raspberry Pi OS is set to the “Europe/London” timezone. You can change it to your local timezone using the “sudo timedatectl set-timezone TIMEZONE” command.
sudo timedatectl set-timezone TIMEZONE
For example, if you wanted to change your time zone to “Australia/Hobart
“, we would use the following.
sudo timedatectl set-timezone Africa/Johannesburg
If you prefer not to use this command, it is possible to set the time zone using the Raspberry Pi configuration tool.
Manually Setting Raspberry Pi’s Time:
If you disable network time sync, you can manually set the time using the “sudo timedatectl set-time TIMEORDATE” command. The time format can be in Y:M:D HH:mm:ss, Y:M:D, or HH:mm:ss.
With this tutorial, you can easily synchronize your Raspberry Pi’s time with networked time servers, keeping accurate time without relying on a real-time clock. Experiment with the provided commands and explore other Raspberry Pi guides and projects to enhance your experience.
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'
Check out this article if its your first time setting a Raspberry Pi up and if you’re eager to follow these tutorials but dont have a Raspberry Pi then get one from our online store!
Read More: Time Sync from the Network on the Raspberry Pi @Raspberry_Pi #PiDay #RaspberryPi
Disclaimer: The content in this post includes excerpts from other blogs and websites. Full credit for the original work goes to the respective authors and publications. This content is shared here for informational purposes only, to give our customers direct insight on where to purchase the mentioned items, and all rights remain with the original creators.