NTP Server
An NTP server is a good way to synchronise the time across all the machines on a network. This is useful for enterprise level software with many servers, so that anything date and time related will be the same. The idea is that a computer can ask the NTP server what the time is, then set it’s own clock accordingly.
For reference, I’m using this post as a guide to getting the NTP stuff setup. However, it was made two years ago and in tech terms, things change. The change from Rasbian Wheezy, to Jessie may be enough to make the reference that I’m following out of date.
Configuring NTP on the Raspberry Pi
NTP is already included in Raspbian, so that saves some effort, it’s not so much a case of installing it. This is simply a job to configure the system to make it available.
First things first. We need to find some time servers that we can use to set our NTP Server. The Raspberry PI doesn’t have it’s own real time clock, so we cannot assume that the timer is right when it starts up. Actually, on boot the PI is already accessing a pool of time servers, but these are likely to be geographically a long away from you, which adds a delay due to the ping time. This will mean that your PI is likely to be showing the wrong time. It’s less than a second, but even so, if you can get it as accurate as possible for little effort, it’s best to spend the few minutes to get that done. So, how do we do it. ntp.org provides pools of time servers around the world. Browse to this link and you will see a table with the continents. Follow the links there to drill down to as close to you geographical location as you can. For me, I can get to a list for the UK, with provides me access to a pool of about 186 time servers. This is great as it means that there’s 186 servers that I can sync up too. As these are all in the same pool of servers they will be syncronised, and in turn running from the same master source.
At the top of the page there is a series of servers that can be added to your ntp.conf. let’s do that now. For this post, I’m going to assume that you are running as root. (sudo -s if you haven’t done so already)
nano /etc/ntp.conf
Scroll down until you see this…
server 0.debian.pool.ntp.org iburst server 1.debian.pool.ntp.org iburst server 2.debian.pool.ntp.org iburst server 3.debian.pool.ntp.org iburst
comment out those lines with a # then add the new servers from your local pool. In my case it’s the UK Servers. (See the image on the left to see what I’ve done)
server 0.uk.pool.ntp.org iburst server 1.uk.pool.ntp.org iburst server 2.uk.pool.ntp.org iburst server 3.uk.pool.ntp.org iburst
Now Ctrl-X, Y then Enter to save.
The post that I’m using as a guide says that there’s a problem. NTP isn’t actually using this file as as it’s taking the information from another file. I decided to check out if this is true, so I rebooted my Pi to see what happens. The post says that the configuration is read from /var/lib/ntp/ntp.conf.dhcp instead. I’ve checked for this file on my new Rasbian Jessie, and it appears to no longer exist, so looks like it’s switched over.
I’ve done a few reboots and have seen that it’s picking four different servers every time. Looking at the output, the delay is the communication delay in miliseconds. So here I can see that these times are less than 20 miliseconds for the time to be downloaded, that shows that they’re local enough for my needs.
Checking against my Pi 3, which is still using the debian servers, I can see delays of 60 miliseconds. Looks like it could be using servers in Europe judging from the ping times.
As a final check, use this command to show that your PI is showing the right time.
date '+%A %W %Y %X'
I’ll worry about configuring all my devices to make use of my new time server later on, for now. I’m happy that it’s setup and working.
One final tweek to make your system run even better.
timedatectl set-timezone Europe/London
This command changes the timezone that your PI is in, by default it is set to UTC, which means that the PI doesn’t know anything about timezones or daylight savings time. This means that your PI will get the time wrong when the the clocks go forward or back. To make things even more complicated, it doesn’t how how to interpret a time properly. So by making the small adjustment above your PI will now know what to do when the clocks change. It also knows the difference between local time and UTC.
As a finishing touch, let’s make sure that iptables is setup to support udp port 123 which is used for NTP Servers.
nano /usr/local/bin/firewall.sh
Add this to the file.
iptables -A OUTPUT -p udp –dport 123 -j ACCEPT
iptables -A INPUT -p udp –sport 123 -j ACCEPT
iptables -A INPUT -p udp –dport 123 -j ACCEPT
iptables -A OUTPUT -p udp –sport 123 -j ACCEPT
Now, let’s make that firewall stuff survive a reboot.
nano /etc/rc.local
Once the script is open, add the following before the line that reads “exit 0”
/usr/local/bin/firewall.sh
See the image on the left to see how the edited file should look.
Now Ctrl-X then Y and enter.