1. Install NTP (Network Time Protocol)
The first step is to install the NTP package on your Raspberry Pi. This can be done by running the following command in the terminal:
sudo apt-get install ntp
2. Configure NTP
Once the NTP package is installed, you will need to configure it to sync with a time server. This can be done by editing the /etc/ntp.conf file.
Open the file in a text editor:
sudo nano /etc/ntp.conf
Add the following lines to the end of the file, replacing “time.example.com” with the address of the time server you wish to sync with:
server time.example.com
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
Save the file and exit the text editor.
3. Start the NTP Service
Once the configuration is complete, you can start the NTP service by running the following command:
sudo service ntp start
4. Check the Time
You can check the time on your Raspberry Pi by running the following command:
date
If the time is not correct, you can force the Raspberry Pi to sync with the time server by running the following command:
sudo ntpdate -u time.example.com
Once the time is synced, you can check the time again with the “date” command.
Do you have issues with the time configuration on Raspberry Pi? Or maybe you want to sync the time with another server? In this post, I’ll teach you all you need to know about time synchronization on Raspberry Pi (and on Linux in general).
On a new installation, a Raspberry Pi uses timedatectl for time synchronization. Servers can be updated in the configuration file, located at /etc/systemd/timesyncd.conf. The NTP service can also be installed if needed.
I’ll explain how time synchronization works on Linux and how to configure it the way you want.
I’ll share the two ways of doing this on a Raspberry Pi:
– Timedatectl: included out of the box, pretty easy to configure.
– NTP: classic way to do this on Linux, for more compatibility with other systems.
And finally, I’ll give you other useful commands for date and time on Raspberry Pi.
By the way, if you are really interested in improving your skills on Raspberry Pi, I highly recommend checking out my e-book here. It’s a 30-day challenge from beginner to master, with step-by-step tutorials and many projects to practice along the way.
Introducing time synchronization
Before going further with technical information and commands, I want to introduce how time synchronization works on Linux on Raspberry Pi and on most modern devices.
On a network, it’s crucial to have the same time on all the computers.
For example, you can’t connect to Active Directory or Samba shares if your computer is even 5 minutes late (check my tutorial on how to join an Active Directory with a Raspberry Pi).
So, we need to set up time-synchronization systems on the network.
The NTP protocol has this role. Basically, you configure your computer to ask the time to another computer, and to use the answer to set its current time (it’s a little more complex than that, but remembering this is already great).
Typically, you have a master server on each network which gets the current time from an Internet server.
And then all computers synchronize with this master.
In this post, I’ll show you how to do this configuration using timedatectl or NTP directly.
If you need more details about the theory before going further, this video is pretty good:
By the way, if you are just looking to set the date and time manually on Raspberry Pi OS, there are easier ways to do this. You don’t necessarily need to change anything about the time synchronization.
And I also have an article on how to do this on Ubuntu if you are not using Raspberry Pi OS: it’s similar but not all the commands are the same: How To Sync The Ubuntu System Clock? (GUI, TimeDateCtl & NTP).
Time synchronization on Raspberry Pi: Timedatectl
What is Timedatectl on Raspberry Pi?
Timedatectl is a tool to set up the date and time. The recent Raspberry Pi OS versions include timedatectl by default.
The first command I’ll show you is how to check the current status:
timedatectl status
This should give you something like this:
So you’ll get:
- The local time.
- The universal time (same thing by default).
- The RTC time if configured (module not included on Raspberry Pi).
- The current time zone (GMT by default).
- The current network time synchronization status.
As you can see, my Raspberry Pi is already time synchronized by default (except the time zone).
Are you a bit lost in the Linux command line? Check this article first for the most important commands to remember, and a free downloadable cheat sheet so you can have the commands at your fingertips.
Timedatectl commands
Before going further in the timedatectl configuration, I want to show you some useful commands you can use directly.
List time zones
If you need to change the default time zone, you first need to know all available values.
To do this, use this command:
timedatectl list-timezones
As the list is big, you can filter it with the grep command:
timedatectl list-timezones | grep America timedatectl list-timezones | grep Sydney
Note your local time zone and use it with the next command.
Set time zone
To set the current time zone, use this command:
sudo timedatectl set-timezone <time zone>
For example:
sudo timedatectl set-timezone America/New_York sudo timedatectl set-timezone Europe/Paris sudo timedatectl set-timezone Australia/Sydney
Use the timedatectl status again to check that the current time is correct.
You can also change the time zone in raspi-config > Localization options > Change time zone.
Set the time manually
You can also set the time manually with timedatectl.
Here is how:
sudo timedatectl set-time 'Y:M:D HH:mm:ss' sudo timedatectl set-time 'Y:M:D' sudo timedatectl set-time 'HH:mm:ss'
sudo timedatectl set-time '12:00:00'
But to do this, you need to disable the time synchronization (see next paragraph).
Enable or disable the time synchronization
If you want to disable or enable the time synchronization, use these commands:
sudo timedatectl set-ntp false sudo timedatectl set-ntp true
Note: you may need to reboot the Raspberry Pi to apply this change (see comments).
That’s it, you now know the basic timedatectl commands.
Sync time with a server: the Timedatectl configuration file
Here is how to configure the time synchronization with a server by using timedatectl:
A reboot might be necessary to update the configuration, but your date and time will now be synchronized with the servers defined in this configuration file.
Time synchronization on Raspberry Pi: NTP
NTP introduction
The other way to do time synchronization on Linux is to use NTP. Even if Debian and Ubuntu are replacing it with timedatectl on new versions, it’s still a common software on Linux systems.
I’m not sure to understand all the differences between both and why they are moving to timedatectl.
But in my mind, I think that timedatectl is for clients, easy to use and configure out of the box.
And NTP is probably better on a server, to sync time with it and have more configuration options.
NTP configuration
NTP installation
As I said, NTP is not available by default on Raspberry Pi OS.
You need to install it with apt:
sudo apt install ntp
NTP configuration
The configuration file for NTP is available here: /etc/ntp.conf.
You can edit it to set a new server for time synchronization (lines beginning with “pool”).
If you want to use your Raspberry Pi as an NTP server, it’s also in this file that you can change the server configuration (restrict access, broadcast time, etc.).
NTP commands
NTP has fewer commands than timedatectl as everything is in the configuration file.
But you can use this one to manage the ntp server daemon:
sudo service ntp status | start | stop | restart
NTPDate
NTPDate is an additional command you can install and use to force time synchronization.
Installation:
sudo apt install ntpdate
Check your current time delay compared to a server:
sudo ntpdate -q 0.us.pool.ntp.org
In my case, I got a 0.005s offset. It confirms that the NTP server is working fine.
Fix the delay now:
The NTP daemon will fix the delay step by step.
But if you want to fix it now, you can use:
sudo service ntp stop sudo ntpdate 0.us.pool.ntp.org sudo service ntp start
You need to stop the NTP daemon before using ntpdate to free the port.
You’ll get something like this:
Video
Here is a video to show you exactly the step-by-step process to do this:
You can also subscribe to see all the news videos in your YouTube Feed:
FAQ
How do I sync the time on my Raspberry Pi?
Time synchronization is enabled by default on Raspberry Pi OS. You can change the timezone with raspi-config or the Raspberry Pi configuration tool, to set the correct time on your system. On the first boot, the welcome menu will also ask for your time zone.
How do I manually set the date and time on Raspberry Pi?
As a general rule, you’ll use raspi-config to set the date and time on a Lite version or the Raspberry Pi configuration tool if you have a desktop environment. You can also use the command sudo timedatectl set-time ‘HH:mm:ss’ to set the time in one command.
How do I find the date and time on my Raspberry Pi?
With a desktop version, the date and time are displayed in the top-right corner, but you can type “date” in a terminal to get the current date and time on any Linux distribution.
In a nutshell
Timedatectl commands
Command | Description |
---|---|
timedatectl status | Show the current configuration |
timedatectl list-timezones | List the timezones you can use |
sudo timedatectl set-timezone <zone> | Set your current timezone |
sudo timedatectl set-time <date-time> | Set the date/time manually |
sudo timedatectl set-ntp <true/false> | Enable or disable time synchronization |
NTP commands
Command | Description |
---|---|
sudo apt install ntp ntpdate | Install NTP and ntpdate |
service ntp status | start | stop | restart | Manage the NTP service |
sudo ntpdate -q 0.us.pool.ntp.org | Check the time delay compared to a server |
sudo ntpdate 0.us.pool.ntp.org | Change the synchronization server (NTP service should be turned off) |
If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!
Conclusion
You now know how to change the date and time on your Raspberry Pi and how to synchronize the clock from several Linux computers on the same network.
This may seem not so useful at home or in a small network, but it’s an essential component in big networks.
More tutorials like this one on RaspberryTips:
Additional Resources
Not sure where to start?
Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Watch the Raspberry Pi Bootcamp course now.
Master your Raspberry Pi in 30 days
Don’t want the basic stuff only? If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.
Download the e-book.
VIP Community
If you just want to hang out with me and other Raspberry Pi fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.
Need help building something with Python?
Create, understand, and improve any Python script for your Raspberry Pi.
Learn the essentials step-by-step without losing time understanding useless concepts.
Get the e-book now.
You can also find all my recommendations for tools and hardware on this page.
How to Sync Time with a Server on Raspberry Pi
Keeping your Raspberry Pi’s time in sync with a server is important for many applications. Whether you’re running a web server, a game server, or a home automation system, having the correct time is essential. Fortunately, it’s easy to sync your Raspberry Pi’s time with a server. Here’s how to do it.
Step 1: Install NTP
The first step is to install the Network Time Protocol (NTP) package. This is a protocol that allows computers to synchronize their clocks with a server. To install NTP, open a terminal window and type the following command:
sudo apt-get install ntp
Once the installation is complete, you can move on to the next step.
Step 2: Configure NTP
Now that NTP is installed, you need to configure it. To do this, open the NTP configuration file in a text editor:
sudo nano /etc/ntp.conf
In the file, you’ll see a list of servers. These are the servers that your Raspberry Pi will use to sync its time. You can add or remove servers as needed. Once you’ve made your changes, save the file and exit the text editor.
Step 3: Start the NTP Service
Now that NTP is configured, you need to start the service. To do this, type the following command in a terminal window:
sudo service ntp start
This will start the NTP service and your Raspberry Pi will begin syncing its time with the servers you specified in the configuration file.
Step 4: Check the Time
Once the NTP service is running, you can check the time on your Raspberry Pi by typing the following command in a terminal window:
date
This will display the current date and time on your Raspberry Pi. If it’s not correct, wait a few minutes and try again. The time should eventually sync up with the server.
Conclusion
Syncing your Raspberry Pi’s time with a server is a simple process. All you need to do is install the NTP package, configure it, start the service, and check the time. With just a few steps, you can keep your Raspberry Pi’s time in sync with a server.