LinuxReviws.org --get your your Linux knowledge
> Linux Reviews > Howtos >

ntp - makes the clock show the correct time

How to get started with ntpdate, the tool for constantly adjusting the system clock to the internet


  1. Installing ntpd
  2. Configuration files for running ntp as daemon
  3. Setting the clock at boot or using cron


ntp is the Network Time Protocol suite.

1. Installing ntpd

First, make sure ntp/ntpdate are installed. The package is by default installed on Mandrake, Linux and Redhat. If you do not have ntp installed you will find packages on your distributions install-cd(s).

On Gentoo Linux, install by typing

  emerge ntp

2. Configuration files for running ntp as daemon

ntpd manpage

2.1. Client Setup

Now, edit the following files:

  /etc/ntp.conf

The only important thing is that the line contains server some.thing.org.

Example:

  server ntp.uio.no
  driftfile /etc/ntp/drift
  broadcastdelay  0.008
  authenticate no

Make sure you add ntpd as a boot (init) service.

Gentoo Linux: rc-update add ntpd default , /etc/init.d/ntpd start

2.2. (LAN) Server setup

To run a ntp server to sync the clocks on your lan, edit

  /etc/ntp.conf

Example:

  server fartein.ifi.uio.no prefer
  server ntp.uio.no
  server ntp.eunet.no
  
  fudge 10.0.0.50 stratum 10
  
  driftfile /etc/ntp/drift
  broadcastdelay  0.008
  
  authenticate no

fudge tells the server to pass on the correct time to the given network interface. stratum is the level of time servers, 10 being the lowest. You only need a higher number if you will be acting as a public internet timeserver with fast bandwidth.

It makes sence to add a few servers. Select the best as prefer

Make sure you add ntpd as a boot (init) service.

Gentoo Linux: rc-update add ntpd default , /etc/init.d/ntpd start

2.3. Firewalls

ntp uses port 123, both UDP and TCP. Make sure this port is not blocked by a firewall.

If you are using iptables, you can open this port with

  $IPTABLES -A INPUT -j ACCEPT -p tcp --dport 123

2.3.1. Known valid and working ntp servers

server adress Location
ntp.ipv6.viagenie.qc.ca IPV6 ONLY
clock.via.net
server fartein.ifi.uio.no Norway
server ntp.uio.no Norway
server ntp.eunet.no Norway
ntp.demon.co.uk UK
ntp.nasa.gov USA
bigben.cac.washington.edu USA
time-b.nist.gov USA
montpelier.ilan.caltech.edu USA
nist1.aol-ca.truetime.com USA
nist1.datum.com USA
time-a.timefreq.bldrdoc.gov USA
time-b.timefreq.bldrdoc.gov USA
time-c.timefreq.bldrdoc.gov USA
time.nist.gov USA
utcnist.colorado.edu USA
tick.usno.navy.mil USA
tock.usno.navy.mil USA
mizbeaver.udel.edu USA

ntpservers.txt

2.3.2. Server List

3. Setting the clock at boot or using cron

I good idea is _not_ to run the ntpd daemon on clients as it uses about 1,7 MB memory, unless you are a server or have a box with a broken clock. Many boxes clocks do drift along on their own, a check every hour or day is generally a good idea.

3.1. rdate

rdate is a simple 3 kB tool for syncing the system clock to a server. It's options are -p to print the date on the given server, -s to set the system clock according to it (must be done as root) and -u to use the UDP protocol. Example:

rdate -p sntp.lth.se

rdate manual page

3.2. clockspeed

clockspeed is a very small tool for setting the clock and is, for clients, a much better alternative than installing the 4+ MB ntp package.

3.3. cron & ntp

You can simply add ntpdate as a cron job every now and then:

  echo 'ntpdate -b server.dot.com' > /etc/cron.hourly/time.cron
  chmod a+x /etc/cron.hourly/time.cron

Most distributions come with a cron. dcron and Vixie cron are nice.

3.4. Gentoo & ntp-client

You can set the clock using ntp at boot (without starting the daemon) on Gentoo Linux by editing:

  /etc/conf.d/ntp-client 

The file should contain:

  NTPDATE_CMD="ntpdate"
  NTPDATE_OPTS="-b your.server.here"

Then add ntp-client to the services started at boot:

  rc-update add ntp-client default

You can check what services are started at boot with rc-status.

Meet new people