User Tools

Site Tools


the_ntp_functions

NTP Server Functions

Mar 2017


If you just wanted a clock, then you don't have to worry about this section, however if you want your clock to act as an NTP (Network Time Protocol) server, so that all of the computers in your house are synchronised in time to the time of your Raspberry Pi Clock, then read on.

There are two parts for this:

Firstly we need our Raspberry Pi to get the correct time from an Internet Time Server, this will keep the time on our Raspberry Pi very accurate (to within a few 10s on milliseconds).

Secondly, we want our computers to speak to the Raspberry Pi to ask for the time, so everything on the network has the same time, but only one device (the Raspberry Pi) needs to check the Internet for the time (thus guaranteeing that all computers will be the same).


Install NTP


First we need to install the NPT software, this may already be installed, just run the command below.

  sudo apt-get install ntp



If NTP is not installed then the installation will start, if NTP is already installed you will the following message.

ntp is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.


This may already be installed, but just in case.
<sxh [txt][; options for SyntaxHighlighter]> Reading package lists… Done Building dependency tree Reading state information… Done Suggested packages:

ntp-doc

The following NEW packages will be installed:

ntp

0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 334 kB of archives. After this operation, 988 kB of additional disk space will be used. Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main ntp armhf 1:4.2.6.p5+dfsg-7+deb8u2 [334 kB] Fetched 334 kB in 5s (57.7 kB/s) Selecting previously unselected package ntp. (Reading database … 31977 files and directories currently installed.) Preparing to unpack …/ntp_1%3a4.2.6.p5+dfsg-7+deb8u2_armhf.deb … Unpacking ntp (1:4.2.6.p5+dfsg-7+deb8u2) … Processing triggers for man-db (2.7.0.2-5) … Processing triggers for systemd (215-17+deb8u6) … Setting up ntp (1:4.2.6.p5+dfsg-7+deb8u2) … update-rc.d: warning: start and stop actions are no longer supported; falling back to defaults </sxh>


Edit NTP Configuration File


Now that NTP is installed, we need to set it up, this is done via a configuration file called ntp.conf.

  sudo nano ntp.conf


Find the section that looks like the following:

# You do need to talk to an NTP server or two (or three).
#server ntp.your-provider.example

# pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
# pick a different set every time it starts up.  Please consider joining the
# pool: <http://www.pool.ntp.org/join.html>
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


And comment out ALL of the servers in this section so that it looks like the following:

# pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
# pick a different set every time it starts up.  Please consider joining the
# pool: <http://www.pool.ntp.org/join.html>
# 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


Now we can add our own NTP servers that we wish to use.

I am in the UK, so I wanted UK time servers, for a list of servers I just did a Google search for UK time servers. Using this Google search I found the (first result) was this site pool.ntp.org

The site lists the following four UK based time servers:

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


Paste these files in to the same section that you just commented out the original server list from.

# You do need to talk to an NTP server or two (or three).
#server ntp.your-provider.example

# pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
# pick a different set every time it starts up.  Please consider joining the
# pool: <http://www.pool.ntp.org/join.html>
# 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

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


Now save and close the file (Ctrl-X and then S to save)


Restart NTP


To get the Raspberry Pi to sync, we can restart the NTP service. Use the following to restart the NTP server.

  sudo service ntp restart



Now check the current time and date using the date command.

  date
  
  Fri 17 Mar 18:38:59 UTC 2017


If the time and date are correct, then you are good to go.


Check NTP Servers


You can test to see if NTP on the Raspberry Pi is actually using any external time servers by using the ntpq -pn command.

ntpq -pn


     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*130.159.196.118 193.62.22.90     2 u   78   64   36   61.470  -17.318  15.763
 78.129.190.21   85.199.214.102   2 u    9   64   37   13.979    0.712   0.680
 87.124.126.49   195.66.241.10    2 u    9   64   37   15.417    1.572   3.078
 192.146.137.13  82.148.230.254   2 u    8   64   37   42.823   -1.197   2.624


After running the command ntpq -pn you should see a list of time servers (by IP) that the NTP service on the Raspberry Pi has connected to. The IP Address with the asterisk * next to it is the last server the Raspberry Pi did a time sync with.


Synchronising Clients


LINUX

Synchronising Linux clients is pretty much the same as we have already done, except in the /etc/ntp.conf instead of putting internet time servers, you just enter the internal IP address of your NTP server.

WINDOWS

For Windows PCs we will have to set the NTP server in the time settings. To do this, first right click the clock in the bottom right hand side of the screen.



Select 'Adjust date/time'

When the next page opens, scroll down.



Select 'Additional date, time, & regional settings'

When the next page opens.



Select 'Set the time and date'

On the next page.



Select the 'Internet Time' tab.

On the next page.



Select the Change Settings button.

On the next page we will see a box that contains time.windows.com



Change the contents of this box to the IP Address of the Raspberry Pi running the NTP server.

Finally, Click the Update Button.



The time should now be updated, you will see confirmation in this page (I have to do this twice sometimes)

That's it.


Clients over 1000 Seconds out of time


“NTP won’t normally reset the clock if it’s off by more than 1000s. If you run:

ntpd -gq’

It ignores the clock being way adrift from ntp time and will force it to be correct then close. The old ntpdate program does the same thing.”

Credit: External Link


Raspberry Pi Dropping of Wireless Network

I found my raspberry Pi Zero, that is using the official Raspberry Pi Wireless Dongle drops off the wireless network after around a week. Only a reboot gets it back (as I have no keyb/mouse connected) so I figured just using Cron to reboot the thing every 24 hours will be a suitable work around.

To use cron (should already be installed):

  sudo crontab -e (reboots require super user rights)


Insert the following line:

  0 0   *   *   *    /sbin/shutdown -r


Explanation:

m      h    dom        mon   dow       command
minute hour dayOfMonth Month dayOfWeek commandToRun

so the line 
  0 0   *   *   *    /sbin/shutdown -r

would reboot your system every day at 00:00.


Use Ctrl+X, Y, Enter. This will save and exit crontab (if using nano)

the_ntp_functions.txt · Last modified: 2023/03/09 22:35 by 127.0.0.1