/ Linux

NTP FYI

NTP, Network Time Protocol is the theme of today's post! This isn't the be all and end all, font of all knowledge blog post. Rather an aide memoir.

What is NTP?

Network Time Protocol (NTP) is a networking protocol for clock synchronization between computer systems over packet-switched, variable-latency data networks.

Thanks Wikipedia

NTP is used to ensure that our computers are synchronised...but why?

Well if the time on our computer is incorrect then if we visit a website that uses SSL/TLS or another security certificate our computer may reject the certificate and claim that the site is insecure. The same is true for SSH connections.

Using NTP we can keep our clock set correctly and typically this is handled automatically via the operating system.

But what if you need to tinker with your NTP settings? Well you can and for this project I used a spare Raspberry Pi 3 as a test subject (No Pis were harmed in the creation of this post.)

On the Pi

Starting off I powered up the Pi and then opened a terminal. In the terminal I updated the repositories and then installed ntpd

sudo apt update && sudo apt install ntp

With that installed I then checked the status of the NTP service, running behind the scenes of the operating system.

sudo service ntp status

And I saw

● ntp.service - LSB: Start NTP daemon
   Loaded: loaded (/etc/init.d/ntp; generated; vendor preset: enabled)
   Active: active (running) since Thu 2018-01-25 10:35:14 GMT; 17min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 18485 ExecStop=/etc/init.d/ntp stop (code=exited, status=0/SUCCESS)
  Process: 18670 ExecStart=/etc/init.d/ntp start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/ntp.service
           └─18681 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 111:116

Jan 25 10:35:14 raspberrypi ntpd[18681]: Listening on routing socket on fd #22 for interface updates
Jan 25 10:35:15 raspberrypi ntpd[18681]: Soliciting pool server 195.219.205.9
Jan 25 10:35:16 raspberrypi ntpd[18681]: Soliciting pool server 85.199.214.99
Jan 25 10:35:17 raspberrypi ntpd[18681]: Soliciting pool server 139.59.180.23
Jan 25 10:35:17 raspberrypi ntpd[18681]: Soliciting pool server 129.250.35.250
Jan 25 10:35:18 raspberrypi ntpd[18681]: Soliciting pool server 194.1.151.226
Jan 25 10:35:18 raspberrypi ntpd[18681]: Soliciting pool server 217.114.59.3
Jan 25 10:35:19 raspberrypi ntpd[18681]: Soliciting pool server 213.251.52.107
Jan 25 10:35:19 raspberrypi ntpd[18681]: Soliciting pool server 178.62.24.228
Jan 25 10:36:21 raspberrypi ntpd[18681]: Soliciting pool server 217.114.59.66

So in this case my NTP service was working and checking the time. So lets turn it off.

sudo service ntp stop

Then we can manually ask NTP to update the time from the NTP servers.

sudo ntpd -qg

The -q switch is used to automatically close the ntpd tool once the date / time is set.

The -g switch stops the ntp tool from panicing if the update is particularly large (say changing the date from 2013 to 2018)

Now we can restart the NTP service.

sudo service ntp start

The check the date / time from the terminal.

date

Which looks like

[email protected]:/etc $ date
Thu 25 Jan 11:03:54 GMT 2018

So there we go, a quick whistlestop tour of NTP, and how to fix the most common issue.