Skip to content


Keeping track of time with NTP

When running a network, one really useful(tm) but oft-misunderstood service to run is Network Time Protocol, or NTP. NTP is a service that simply synchronizes the time across any systems that query it. If you have any Windows systems with XP or 2000, chances are that they at least attempt to synchronize time with time.windows.com. Whilst there’s no harm in running the odd NTP query here and there, it certainly isn’t going to hurt to keep a local server. In this article, I discuss how to set up an NTP server on Linux, and synchronize various Operating Systems with it.The first thing you need to do is to install NTP. I mainly use a mix of Windows, BSD and Linux-based systems on NTP deployments as the hardware requirements are low and the hardware compatibility is high. I set NTP up on a NetBSD Sparcstation some time back, but since its quite loud, it was felt that it was time to migrate it to a nice quiet compaq deskpro running Debian with a few CPU cycles to spare.

NTP servers are handled by the ntp-server package under debian. A quick search came up with the following:

debian:~# apt-cache search ntp | grep ^ntp
ntp – Network Time Protocol: network utilities
ntp-doc – Network Time Protocol: documentation
ntp-refclock – Network Time Protocol: daemon for reference clocks
ntp-server – Network Time Protocol: common server tools
ntp-simple – Network Time Protocol: daemon for simple systems
ntpdate – The ntpdate client for setting system time from NTP servers

A quick apt-get install ntp-server later and we’re almost ready to go. The next step is to edit /etc/ntp.conf. My ntp.conf looks roughly like this:

# /etc/ntp.conf, configuration for ntpd

# ntpd will use syslog() if logfile is not defined
#logfile /var/log/ntpd

driftfile /var/lib/ntp/ntp.drift
statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
# pool.ntp.org maps to more than 100 low-stratum NTP servers.
# Your server will pick a different set every time it starts up.
# *** Please consider joining the pool! ***
# *** ***
server pool.ntp.org
server pool.ntp.org
## uncomment for extra reliability

# … and use the local system clock as a reference if all else fails
# NOTE: in a local network, set the local stratum of *one* stable server
# to 10; otherwise your clocks will drift apart if you lose connectivity.
server 127.127.1.0
fudge 127.127.1.0 stratum 10

restrict default noquery
restrict 127.0.0.1
restrict 192.168.1.0 mask 255.255.0.0 nomodify

I set two server definitions for pool.ntp.org – this automatically maps to public ntp servers, so if your upstream ntp server goes down then service won’t be interrupted. The other item of note is the restrict setting for 192.168.1.0 with a class ‘B’ subnet mask. This may seem a bit strange but the simple explanation is that it suits my subnetting scheme. Change the IP and subnet mask to suit yours and you’re almost ready to go.

Start the ntp service, use this on debian:

/etc/init.d/ntp-server start

You can check to see if it works by doing the following (apologies for the bad formatting):

debian:~# ntpq -p 127.0.0.1
remote refid st t when poll reach delay offset jitter
==============================================================================
tool.snarl.nl 193.79.237.14 2 u 7 64 7 294.505 -164.65 148.632
clock3.redhat.c .CDMA. 1 u 4 64 7 487.682 -208.49 250.008
LOCAL(0) LOCAL(0) 13 l 5 64 7 0.000 0.000 0.001

Now to get the other hosts talking. I have another Debian box that I want to synchronize. For this box I just apt-get install ntpdate. I now have to edit /etc/default/ntpdate so that it knows where to look. I set the value of the NTPSERVERS variable to 192.168.1.3 (my test host) and saved the change. Now all I need to do is:

/etc/init.d/ntpdate start

It should tell me that it’s running ntpdate to synchronize the clock. If I encounter any problems I can run ntpq 192.168.1.3 which should give me the same results as running it locally on the ntp server.

I also have a Sony VAIO running Gentoo Linux that I want to synchronize. In gentoo, it’s simply a case of running emerge ntp and waiting for a while for it to build. Again, I need to a variable to reflect my NTP server but this time it lives in /etc/conf.d/ntp-client and is called NTPCLIENT_OPTS. Now all I need to do is /etc/init.d/ntp-client start and it should work fine. Once again, ntp -q should help resolve issues.

On Windows its a different matter (of course). I have an XP Professional system at home, and I wanted it to synchronize with the NTP server. In the date and time capplet on the control panel you’ll find a network time tab. It should be set to automatically update with time.windows.com. Simply change this to your NTP server’s IP address and hit update now. If everything goes smoothly, hit ok and it should update every hour.

Different distros and Operating Systems handle time synchronization differently. I wanted my Gentoo laptop to synchronize on boot only, and only if the IP address was one of the ones it normally gets on my home networks. I wrote a simple startup script to grep ifconfig for known IP addresses and run /etc/init.d/ntp-client start if it works. With debian, depending on your setup it may or may not synchronize. I set ntp to synchronize by placing an entry in /etc/cron.d/cron.daily – I’d find any more regular than that overkill for what I have in mind.

BSDs use different startup methods, and the common ones come with NTP client functionality by default. On FreeBSD its simply enough to configure /etc/ntp.conf as shown above and set ntpdate_enable=”YES” in /etc/rc.conf.

If you’ve followed this guide and reached here, then by now you should have a working NTP service up and running, with time now synchronized across your hosts.

Share

Posted in Guides, OpenSource, Security.