Productive Linux


 Subscribe via Feedburner in a reader

Enter your email address:

Delivered by FeedBurner


Don't have an account?
Sign up to
Forgot your password?

Adjusting the Time - (or How to Be a Time Lord)
18 March 2006 @ 13:33 GMT
by Paul

I was sick of having my laptop on one time, my desktop on another, my file server on another and my web server on yet another. It's no wonder I was always late for everything. Time drift on the laptop was particularly annoying and was the cause of often turning up late for appointments. That's my excuse anyway.

So, what is time, really?

Linux systems have two sorts of times. One is the time that is kept in the hardware clock. This keeps track of time when the computer is off. The second is the 'system time' that is kept by the kernel when the computer is on. The system time is normally more accurate than the hardware clock.

Occasionally, these two types of times need to be synchronised. This is done using the 'hwclock' utility.

hwclock --show

shows the current time.

hwclock --hctosys

copies the time stored in the hardware clock to the system time and

hwclock --systohc

copies the system time into the hardware clock.

That seems simple, but it's not. Because the time in the hardware clock drifts the 'hwclock' program has to check how much the hardware clock is drifting away from the system time. How it does this is complicated, but it involves the file '/etc/adjtime'.

All this leads to interesting philosophical problems, such as what is time really? is it all relative? is it possible to measure time objectively? and so on. Fortunately, further discussion of this is beyond the scope of this post!

But back to the original problem, how do I get my system time to bear some relation to the actual time that everyone else is living their lives by?

Misadventures with ntpdate

I first tried ntpdate but the way that this is set up on Debian is that it sets the system clock at startup by reference to an internet time server (an ntp server) and then it exits. This presents problems. What if, on startup I'm not connected to the internet? (The program waits for ages and then exits). What if I then later connect to the internet? Some people run ntpdate using cron. But its brute force method seems to cause problems that I didn't previously have.

A solution with ntpd

Because of this I quickly got fed up with ntpdate. Besides, it is deprecated. I turned my attention to the ntp server, ntpd, by installing Debian's ntp-simple package. Now, this works out of the box, as ntp polls a time server and then adjusts the system time accordingly. Generally, if the system time needs adjusting it slews it gradually so that your computer doesn't get any shocks if time is abruptly shifted. There are some limitations though. If your clock is out by 1000 seconds, ntpd will panic and exit. You can stop this behaviour by setting the 'g' option. This must be done, as far as I can see, in the startup file, /etc/init.d/ntp-server. So the revelant line of mine now reads:

start-stop-daemon --start --quiet --pidfile /var/run/
--exec /usr/sbin/ntpd -- -p /var/run/ -u $UGID -g

You can also make ntpd immitate the operation of ntpdate (i.e. set the system time and then quit) by setting the 'q' option. However, if you are connected to the internet I don't see why you'd want to.

To get ntpd to work properly you need to adjust the configuration file, /etc/ntp.conf. It is best if you set ntpd to poll one of the ntp server pools, details of that are here. Mine are different on different machines, but a typical entry is:


Once you've started your local ntp server, you can check that it is working by doing ntpq -p. You'll get a print out that is something like this:

     remote           refid      st t when poll reach   delay   offset  jitter
*Time1.Stupi.SE  .PPS.            1 u  376  512  377   54.483    0.497   1.930
+ipx10540.ipxser   2 u  337  512  377   49.496   -3.141   2.796    2 u  324  512  377  106.907    5.225   7.803
 LOCAL(0)        LOCAL(0)        13 l   29   64  377    0.000    0.000

The important thing is that one of the servers has an asterix next to its name, which means that it is the system peer. What this printout means is described here.

One thing that worried me was whether after all this whether my far more accurate time would be lost when the system was shutdown. Luckily, it isn't. On system shutdown, a script, /etc/init.d/, is run which writes the system time to the hardware clock ensuring that the time is preserved for the next boot up. Hopefully, it doesn't drift too much while we're shut down.

Another thing that concerns me is the effect of putting a laptop to sleep. Apparently, ntpd doesn't understand about 'sleep' mode and I wonder whether this will confuse it. So far there have been no ill effects, but perhaps we should stop the ntp server just before system suspend and restart it on wake up. I don't know whether the this is necessary or not.

Finally, here are some resources on time setting:

Ntp protocol and more
Ntp server options
A discussion thread and getting ntpd to emulate ntpdate

Comments disabled