Network Time Protocol (NTP).

Valid HTML 4.01!


According to the ntp home page, "NTP is a protocol designed to synchronize the clocks of computers over a network." It is different to and more accurate that the time and daytime services referred to in /etc/inetd.conf. You will of course need to have the ntp package installed, under Slackware 9.x & 10.x it is not installed by default, but can be selected as a package under expert installation. Or just install it with "installpkg <path/to/package.tgz>".
 

ntp.conf


The operation of ntpd is controlled by the configuration file /etc/ntpd.conf. Most of this is well documented under the documentation section of the www.ntp.org web site, so I will not repeat it here. Two points that I have had trouble with are:-
  • restrict directive, in order to control who may talk to the ntp server a restrict directive is available, this would be more intuitive if it was called allow as it is a list of ipaddresses who may connect. I got caught out by putting more than one ipaddress on each line, it seems that you have to add a second restrict statement for the second address. If you don't ntp does not seem to create an error message anywhere, it just doesn't work.
  • Using a local time source. If you want to run ntp on an isolated pc or network, there is a special ip address to use as the reference server. (server 127.127.1.0) This causes ntp to use the local hardware clock, not to be recommended as the quality of most pc clocks is not too good, I wanted to use it for testing purposes.
Also for doing a one off, step change in system time use:-
lithium:/# ntpdate -b ntp.your.fav.ntpserver
But using ntpdate more than once (eg. in a cron job) is a bad idea, get ntpd to work properly. Also remember to sync the hardware clock (hwclock -w).
 

ntp hierachy


The illustration to the right shows our logical configuration. Tick & tock are directly connected to the internet, all other devices are behind nat and cannot see the internet. Tick & tock are clients of public stratum 2 servers and also peer with each other. Internally, the network devices on our 20 site wan are clients of the core routers and switch (Cisco 2610, 3750 & 2950) which peer and themselves are clients of tick & tock and some client accessible servers. All user workstations are clients of the internal servers, either using ntp for linux computers or using ncp for windows computers logging in to Novell Netware 6.5 servers (set station time=on).

Testing ntp


Testing ntp is important because it verifies all is working correctly. Part of the ntp package are two utilities for doing this:-
  • ntpq, this can be run as an interactive program (just use ntpq from the command line) or as a command line program (use ntpq -p for example). I have produced a web page generated from a cgi perl script based on this as a quick and easy test of time status. You can download it from here and see it in action here. An ssi perl script is here which prints the current stratum the host is (using ntptrace). You can also use ntpq to interrogate a remote server if you have access permission. The online demo here on tick.mediahub.co.uk uses this to get the time status of tock.mediahub.co.uk.
  • ntptrace is a command line program to trace the time hierarchy, similar to what traceroute does for ip. A sample output is here:-
    lithium:/#ntptrace
    localhost: 		stratum 4, offset 0.000013, synch distance 0.19173
    phobos.mediahub.co.uk:	stratum 3, offset 0.037836, synch distance 0.07823
    ntp0.cis.strath.ac.uk: 	stratum 2, offset 0.033060, synch distance 0.03949
    chronos.cru.fr: 	stratum 1, offset 0.036797, synch distance 0.00000, refid 'GPS'
    lithium:/#
    
 

Sample ntp.conf

server ntp0.cis.strath.ac.uk
server phobos.mediahub.co.uk
server deimos.mediahub.co.uk
driftfile /etc/ntp/drift
# server 127.127.1.0
server tick.mediahub.ac.uk 	prefer	# prefer this server
peer 172.1.28.14			# local peer
peer 172.1.28.1				# local peer
# local clock
# fudge 127.127.1.0 stratum 10
multicastclient			 	# listen on default 224.0.1.1
broadcastdelay	0.008
# Trust ourselves.  :-)
restrict 127.0.0.1
restrict tick.mediahub.ac.uk
restrict 172.1.28.14
This may not be the best ntp.conf file, if you have any suggestions for improvements, please email me on "suggestions at rainsbrook dot co dot uk".
Below is a sample startup script for Slackware, by default if you install ntp, no mechanism is provided to start it automatically.

Sample rc.ntpd

#!/bin/sh
# Start/stop/restart ntpd time server:

if [ "$1" = "stop" ]; then
  echo "Stopping ntpd..."
  killall ntpd
elif [ "$1" = "restart" ]; then
  echo "Restarting ntpd..."
  killall ntpd
  sleep 1
  /usr/sbin/ntpd
else # assume $1 = start:
  echo "Starting ntpd:  /usr/sbin/ntpd"
  /usr/sbin/ntpdate 192.168.1.21
  /usr/sbin/ntpd
fi

Download rc.ntpd.
Having proceeded this far, you actually need to run rc.ntpd, the best way is to put a line
/etc/rc.d/rc.ntpd start
in /etc/rc.local, of course you can run it from the command line as well. If it doesn't want to run check that you have x permission (chmod o+x rc.ntpd).