ACPI administration advocacy advocacy advocacy opinion alsa amarok apache apple apt aptitude archive audio audo authentication automount avi awk backup bash BIOS boot browser business bzip cache calendar calibre cdr cdrecord censorship commandline computerscience console convert cron cut database date debian degree design desktop development disk dpkg dvd economics education emacs email europe exim faad ffmpeg file files firefox firewall flash foss freedom ftp fun fuse git gnumeric graphics grep growisofs grub gtkpod gzip hardware hardware html icedove idiocy image imagemagick images installation ip iphone ipod iptables iso itunes ivman kde kernel keyboard knoppix lame laptop latex libreoffice linux locale lockin locking longlines lsof m4a microsoft mimetypes minitab mogrify mount mp3 mp4 mplayer multimedia music mysql network nfs nfs4 nmap openbox openfiles openoffice opinion opinion orgmode partition pdf pdftk perl php podcast politics pomodoro ports postgresql print printing privacy process programming rant remote rhythmbox rss rsync rxvt scp screengrab screenshot script scripting scsi security sed server services shell siteadmin sitenews sitesoftware skype skype slackware sound sox spam spreadsheet ssh statistics subversion sudo svk swap t23 t43 tar terminal tex text thinkpad thunderbird time timer timezone ubuntu udev upgrade usb usbmount users uuid versioncontrol vfat video vnc windows wine wordpress wordprocessing X40 xwindows xwindows youtube
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.
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.
shows the current time.
copies the time stored in the hardware clock to the system time and
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?
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.
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/ntpd.pid --exec /usr/sbin/ntpd -- -p /var/run/ntpd.pid -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:
server 0.europe.pool.ntp.org server 1.europe.pool.ntp.org server 2.europe.pool.ntp.org
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 184.108.40.206 2 u 337 512 377 49.496 -3.141 2.796 +80-28-46-78.ads 220.127.116.11 2 u 324 512 377 106.907 5.225 7.803 LOCAL(0) LOCAL(0) 13 l 29 64 377 0.000 0.000 0.004
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/hwclock.sh, 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: