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 time.google.com
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$ timedatectl status Local time: Fri 2017-06-16 15:10:39 BST Universal time: Fri 2017-06-16 14:10:39 UTC RTC time: n/a Time zone: Europe/London (BST, +0100) NTP enabled: yes NTP synchronized: yes RTC in local TZ: no DST active: yes Last DST change: DST began at Sun 2017-03-26 00:59:59 GMT Sun 2017-03-26 02:00:00 BST Next DST change: DST ends (the clock jumps one hour backwards) at Sun 2017-10-29 01:59:59 BST Sun 2017-10-29 01:00:00 GMT |
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; http://www.pool.ntp.org/en/use.html . To do so you need to edit the file sudo nano /etc/systemd/timesyncd.conf so that your Time section looks something like this;
1 2 |
[Time] Servers=0.uk.pool.ntp.org 1.uk.pool.ntp.org 2.uk.pool.ntp.org 3.uk.pool.ntp.org |
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 🙂
HTH
by
Thanks. This fixed my time-drift issues. With the Raspberry having no RTC, this become important