how to sync time raspberry pi

How to Sync Time with a Server on Raspberry Pi

If you click our links and make a purchase, we may earn an affiliate commission. Learn more

I only realized how important time sync was when one of my Raspberry Pi projects started logging the wrong timestamps. If you’re running servers, scripts, or automation, this matters more than you think. I’ll show you how to sync your Pi properly and avoid the most common issues.

When installing Raspberry Pi OS, the system uses the timedatectl command for time synchronization. Default time servers can be updated in the configuration file, located at /etc/systemd/timesyncd.conf. Alternatively, an NTP service can be installed instead.

In this guide, I’ll show you the 2 main ways to handle this time synchronization on Raspberry Pi OS and most Linux distributions: timedatectl (the default method), and NTP (the traditional way). But let’s start with a bit of theory about this topic.

If you’re new to Raspberry Pi or Linux, I’ve got something that can help you right away!
Download my free Linux commands cheat sheet – it’s a quick reference guide with all the essential commands you’ll need to get things done on your Raspberry Pi. Click here to get it for free!

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 from another computer and the answer given to set the current time (it’s a little more complex than that, but remembering this is already great).

how NTP time synchronization works diagram
Network Time Protocol (source: Wikipedia)

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.

Something not working as expected?
You can get answers from real experts in minutes.
Get help with your setup

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 set up something more exact like 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

We’ll go over two methods to perform time synchronization. The first one is timedatectl, which is available on most modern Linux operating systems running systemd.

What is Timedatectl on Raspberry Pi?

Timedatectl is a tool to set up the date and time. Recent versions of Raspberry Pi OS 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 (only exists on Raspberry Pi 5 currently).
  • 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.

Recommended next step
Master Raspberry Pi in 30 Days

If you want a clearer path than jumping from tutorial to tutorial, my book gives you a step-by-step roadmap to really understand your Raspberry Pi.

Check the book here

Tip: Command lines can be a pain to memorize. I put the essential Linux commands on a printable cheat sheet so you don't have to keep googling them. You can grab the PDF here if you want to save some time.

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
timedatectl command showing timezones in america

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 > Localisation Options > Timezone.

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'

For example:

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.

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:

  • Open the configuration file:
    sudo nano /etc/systemd/timesyncd.conf
  • Comment out the line starting with FallbackNTP and replace default servers with your time servers (on the Internet or from the local network).
  • For example:
    [Time]
    #NTP=
    FallbackNTP=0.us.pool.ntp.org 1.us.pool.ntp.org
  • Save and exit.

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

The second and older way to do time synchronization on Linux is to use NTP.

NTP Introduction

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 I understand all the differences between both and why they’re 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 Installation and Configuration

As I mentioned, NTP is not available by default on Raspberry Pi OS. We’ll need an NTP client/server implementation to use it. For this example, I’ll be installing the ‘chrony’ package:

sudo apt install chrony

The configuration file for chrony is available here: /etc/chrony/chrony.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.).

Automatic Time Sync with NTP

NTP has fewer commands than timedatectl as everything is in the configuration file.
But you can use this one to manage the server daemon, ‘chrony’ in our example:

sudo service chrony status | start | stop | restart

When this NTP service is active, it will periodically check if your system is synchronized with the time servers you set above, and then automatically fix your system’s date-time for you.

The service is set-it-and-forget it, so you’re all done here.
Your date and time should be synced from here on out.

Manual Time Sync with NTP

If you’re not using the automatic service or don’t want to for some reason, you can enter a command to manually force time synchronization.

Check your current time delay compared to a server:

sudo chronyc tracking

In my case, I had a ~ 0.001s offset. It confirms that the NTP server is working fine.

Fix the delay now:
You’ll need to stop the NTP service to run a manual time synchronization.

sudo service chrony stop
sudo chronyd -q
sudo service chrony start

You’ll get something like this.

It will fix the time skew detected, and your system clock should now be completely synced.

Video Tutorial

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:

Summary in a Nutshell

Timedatectl Commands

CommandDescription
timedatectl statusShow the current configuration.
timedatectl list-timezonesList 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

CommandDescription
sudo apt install chronyInstall NTP daemon.
service chrony status | start | stop | restartManage the NTP service.
sudo chronyc trackingCheck the time delay compared to a server.
sudo chronyd -qForce a manual sync.
(NTP service should be turned off.)

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:


🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

Stuck on this project? Ask me or other Pi users in the RaspberryTips Community. We help each other out and you'll get answers quick. Join and fix it together.

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.

Not getting the same result?

Even when you follow every step, small differences in OS version, hardware or config can change the outcome. Instead of wasting time guessing, get help from people who have already fixed the same kind of issue.

Ask your question now
🔒 No risk. Cancel anytime.
  • Get help on your exact issue
  • Access step-by-step videos for tricky setups
  • Browse the website without ads

Similar Posts

19 Comments

  1. Thanks for the information, but seems like timedatectl is not independent as I thought…it is still depend on ntp to carry out its operations.

    1. Hi Jackson,

      I’m not sure to understand your comment
      If it requires ntp to use it, is this a problem?

  2. Good to know.

    I don’t know why, but I had to reboot after executing `sudo timedatectl set-ntp true` in order for the change to show up in `timedatectl status`. Not a bug or a complaint, just a datapoint.

  3. I found that there’s a typo on this line:
    sudo timedatectl set-time ‘A:M:J HH:mm:ss’ That should read
    sudo timedatectl set-time ‘A/M/J HH:mm:ss’
    Same for the next line/ instead of :
    I also found that there are many other issues on this article if you’re using Raspbian Buster like ‘ntp’ is no more installed even if you want as there are inconsistencies not met .

    1. Thanks for your comment Jacques

      And yes, this post is a little old, I need to try on Buster and update it if needed

  4. It is not necessary to reboot the pi after changing the timesyncd.conf. Just restart the service with the command:

    sudo systemctl restart systemd-timesyncd.service

    Afterwards you can check with “timedatectl show-timesync” if timesyncd uses the NTP servers, you have configured.

    I also use the line NTP for configuring my time servers, the FallbackNTP is for fallback. 🙂
    [Time]
    NTP=my-timeserver01.local my-timeserver02.local
    FallbackNTP=0.us.pool.ntp.org 1.us.pool.ntp.org

  5. I have several ESP8266 projects connected via wifi and all need to be synced to have the correct time. For testing I did not want all of them to go out and sync with an ntp server on the internet.

    I installed NTP because this enabled the Rpi to serve as an NTS server for local clients on my network. I.e. it then syncs itself with an NTP server on the internet AND it responds to NTP requests locally. At least this was suggested by some posts I found.

    I understand that timedatectl is clever enough to detect that ntp is installed so you don’t have to stop this service, as some posts elsewhere suggested.

    What I wonder: is the Rpi responding to NTP requests on the local network out of the box, thus without NTP installed? Anyone tested that?

    1. Hi Eric,

      I didn’t test it, but I think timedatectl is just a NTP client, not a server

      You need to install NTP to do this

      Patrick

  6. hi I installed raspbery pi 3 on ubuntu 18.04
    I can’t automatically synchronize the time every time I reboot
    the ntp is not used , i think because the time is long time ago,so it can’t work

    Vit

    1. Hi,
      Yes you probably need to stop NTP, synchronize “manually” (ntpdate) and restart NTP after to keep it synchronized

  7. I have a R-Pi 4B, Linux 5.4.51 (updated 2020-10-14).
    The sudo timedatect set-time ‘FMT’ commands do not seem to be recognized. OS says “Failed to recognize arguments.” What do I need to do to make this work?

    1. Hi Tim,
      There is a typo in your comment, make sure you are using the exact command on your Pi:
      sudo timedatectl set-time ’08:00:00′

  8. The correct format for setting the date and time is:

    sudo timedatectl set-time ‘Y-M-D hh:mm:ss’

  9. Hi, i have a problem that when i turn off electricity the raspberry time stops and doesn’t proceed and in my project there should be no internet connection to sync the time again.
    is there anything i can do so it sync the time with no internet?

    1. I’m using a rpi with opencpn on a sailing yacht without internet, there is GPS NMEA input on the rpi how can I get the rpi to use the GPS time received on the USB port?

Leave a Reply

Community members only
Comments are reserved for RaspberryTips Community members. Join the community to ask questions, get help, and interact with other Raspberry Pi users.

Join the community  or  log in