Skip to content

Instantly share code, notes, and snippets.

@Paraphraser
Last active January 18, 2020 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Paraphraser/e1129880015203c29f5e1376c6ca4a08 to your computer and use it in GitHub Desktop.
Save Paraphraser/e1129880015203c29f5e1376c6ca4a08 to your computer and use it in GitHub Desktop.
Configuring Raspbian to use local time-servers

Configuring Raspbian to use local time-servers

The case for using local time-servers

Out of the box, Raspbian gets its time from NTP servers in the debian.pool.ntp.org domain. You can confirm this via:

$ timedatectl show-timesync
...
ServerName=0.debian.pool.ntp.org
...

Depending on your geographic location, this may not be appropriate. To use a concrete example, query the DNS to obtain some IP addresses for that pool:

$ dig @8.8.8.8 0.debian.pool.ntp.org +short
81.166.75.50
176.9.241.107
193.141.27.6
178.17.161.12

That returns a randomized list. Ping the first entry:

$ ping -c 10 81.166.75.50
PING 81.166.75.50 (81.166.75.50): 56 data bytes
64 bytes from 81.166.75.50: icmp_seq=0 ttl=50 time=316.846 ms
64 bytes from 81.166.75.50: icmp_seq=1 ttl=50 time=316.796 ms
64 bytes from 81.166.75.50: icmp_seq=2 ttl=50 time=317.082 ms
64 bytes from 81.166.75.50: icmp_seq=3 ttl=50 time=316.832 ms
64 bytes from 81.166.75.50: icmp_seq=4 ttl=50 time=316.409 ms
64 bytes from 81.166.75.50: icmp_seq=5 ttl=50 time=317.470 ms
64 bytes from 81.166.75.50: icmp_seq=6 ttl=50 time=316.857 ms
64 bytes from 81.166.75.50: icmp_seq=7 ttl=50 time=316.545 ms
64 bytes from 81.166.75.50: icmp_seq=8 ttl=50 time=316.554 ms
64 bytes from 81.166.75.50: icmp_seq=9 ttl=50 time=317.419 ms

--- 81.166.75.50 ping statistics ---
10 packets transmitted, 10 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 316.409/316.881/317.470/0.336 ms

Very little variance (good) but a 317ms round-trip-time is a fair distance away. Now, let's run the same test for servers which should be closer to my location in Australia:

$ dig @8.8.8.8 0.au.pool.ntp.org +short
129.250.35.251
162.159.200.123
129.250.35.250
27.124.125.252

$ ping -c 10 129.250.35.251
PING 129.250.35.251 (129.250.35.251): 56 data bytes
64 bytes from 129.250.35.251: icmp_seq=0 ttl=55 time=17.930 ms
64 bytes from 129.250.35.251: icmp_seq=1 ttl=55 time=17.549 ms
64 bytes from 129.250.35.251: icmp_seq=2 ttl=55 time=17.335 ms
64 bytes from 129.250.35.251: icmp_seq=3 ttl=55 time=17.958 ms
64 bytes from 129.250.35.251: icmp_seq=4 ttl=55 time=17.666 ms
64 bytes from 129.250.35.251: icmp_seq=5 ttl=55 time=17.434 ms
64 bytes from 129.250.35.251: icmp_seq=6 ttl=55 time=17.058 ms
64 bytes from 129.250.35.251: icmp_seq=7 ttl=55 time=17.788 ms
64 bytes from 129.250.35.251: icmp_seq=8 ttl=55 time=17.279 ms
64 bytes from 129.250.35.251: icmp_seq=9 ttl=55 time=16.912 ms

--- 129.250.35.251 ping statistics ---
10 packets transmitted, 10 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 16.912/17.491/17.958/0.336 ms

317ms vs 17ms round-trip times? No contest, really.

Finding your NTP pool servers

Unless you have good reasons to do otherwise, "your NTP pool servers" generally means "servers in your own country".

Countries usually have several pools and the simplest way to find out what they are is to use Google to conduct a restricted search on your country name like this:

«your country name here» site:.pool.ntp.org

For Australia, the answer is:

server 0.au.pool.ntp.org
server 1.au.pool.ntp.org
server 2.au.pool.ntp.org
server 3.au.pool.ntp.org

Once you have identified the pool servers for your country, arrange them in space-separated form like this:

0.au.pool.ntp.org 1.au.pool.ntp.org 2.au.pool.ntp.org 3.au.pool.ntp.org

Configuring Raspbian

Login to your Raspbian device. Change your working directory and make a backup of the baseline timesyncd.conf file:

$ cd /etc/systemd/
$ sudo cp timesyncd.conf timesyncd.conf.bak

Use (sudo) the text editor of your choice to edit timesyncd.conf. Find the line beginning with:

#NTP=

In its commented-out form, this string means Raspbian is using default servers (eg 0.debian.pool.ntp.org).

Change the string to remove the leading # and append the space-separated list of pool servers you prepared earlier. The result should look like this:

NTP=0.au.pool.ntp.org 1.au.pool.ntp.org 2.au.pool.ntp.org 3.au.pool.ntp.org

Save the file, then execute these commands:

$ sudo timedatectl set-ntp false
$ sudo timedatectl set-ntp true
$ timedatectl show-timesync

The confirmation you are looking for in the output from the third command is a line like this:

SystemNTPServers=0.au.pool.ntp.org 1.au.pool.ntp.org 2.au.pool.ntp.org 3.au.pool.ntp.org

Job done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment