Keeping time on a Pi

Again, this blog fall into the category of a ‘I need to remember how I did this’ post.  I actually started writing this months ago but never finished it.  Good job as some of what I originally said was wrong and we have moved on from Jessie to Stretch.

This started when I accidentally discovered that the date/time on one of my Pi’s was significantly out.  I won’t bore you with the things that I tried and failed, rather just tell you how to fix it.

You can check the date&time your own system by simply typing date and seeing if it is right or not.

Finding out how to get the right time set is remarkably difficult.  In the end I discovered that you can simply use this command (reference);

sudo sntp -s

NTP is the protocol that synchronises time across the internet.  There are a number of free to use servers that you can query to get the current date & time.

One important fact is that if your date & time are significantly out, you cannot use ntp to set the time correctly.  Set it manually so about the right time then sync.

On more recent versions/distributions of Linux,  timedatectl  may be used to query and change the system clock and its settings.  Note this is not a daemon in it’s own right.  On Linux there are 2 daemons you can use, ntpd  or systemd-timesyncd . However you must not have both enabled at the same time. ntpd is the older daemon. systemd-timesyncd is the newer one and is a builtin service.

Now have a look at the daemon that controls synchronising your system with an ntp server.

The best explanation of this daemon I found was here, with another guide here.

As you can see from the output above, NTP is enabled and synchronised and my timezone is set correctly, it is likely that in the response you see, they are not.  Firstly, if your timezone is not right I’d suggest using sudo raspi-config to set it correctly.

In order to use ntp to keep your clock right, you first need to point the daemon to a time server.  The recommendation is to use a suitable one out of the ntp pool project; . To do so you need to edit the file  sudo nano /etc/systemd/timesyncd.conf  so that your Time section looks something like this;

Interestingly, the guide above suggested the line should start ‘NTP’ but as all I did to the standard line was uncomment it and change debian to uk.

To now switch on the synchronising use the command sudo timedatectl set-ntp true

One final thing, if you are using UFW you may find you need to open the port with  sudo ufw allow 123/udp

You may also need to restart the services  sudo systemctl restart systemd-timesyncd.service

Your system should now keep time 🙂


Facebooktwitterby feather
This entry was posted in Technical - General. Bookmark the permalink.

1 Response to Keeping time on a Pi

  1. Graham says:

    Thanks. This fixed my time-drift issues. With the Raspberry having no RTC, this become important

Leave a Reply