Referencing: Unix time, UTC, TAI and all that
Your computer's internal clock is unlikely to keep perfect time. If you need it to be more accurate, the network time protocol daemon (ntpd) is the tool for that.
Historically, we've had ntpd and ntpdate. The ntpdate was for just quickly finding out the current time and updating the system with that. Typically, you'd run it at boot or perhaps once or twice a day from cron. Many systems still have this; you just point it at an appropriate server:
ntpdate clock2.redhat.com ntpdate time.apple.com
With modern ntpd, you could use -q for the same effect:
ntpd -q clock2.redhat.com
If you aren't always connected to the internet, or your desire for accuracy is fairly relaxed, running ntpdate once a day or so is probably enough to keep you happy and on time, but ntpd is the better choice.
The man page for ntpdate on my OS X says "After a suitable period of mourning, the ntpdate utility is to be retired from this distribution."
If your time keeping needs are more precise, you want to run ntpd all the time, and let it figure out how often it needs to adjust things. If you are being that concerned about accurate time, you'll want to carefully select your time server too, because arbitrarily picking a popular server as we did above could lead to inaccurate time just because of network delays. The "ntptrace" command tells you where things are:
# ntptrace time.apple.com time0.apple.com: stratum 2, offset -0.100057, synch distance 0.03278 188.8.131.52: stratum 1, offset -0.100085, synch distance 0.00548, refid 'GPS' # ntptrace clock2.redhat.com clock2.redhat.com: stratum 1, offset -0.100989, synch distance 0.00041, refid 'CDMA'
Notice a difference there? The "time.apple.com" is a Stratum 2 server, that talks to a Stratum 1 server to get its own time. In general, Stratum 1 servers aren't publicly available, or at least require some prearrangement. That's not always true, but if you look around the web you'll see that you are asked not to use Stratum 1 servers unless you really need to.
For many of us, one time server is enough. But people really worried about time will select multiple servers, so that a glitch at one won't throw them off. You put these servers into /etc/ntp.conf - here's mine from my iBook:
server time.apple.com minpoll 12 maxpoll 17
That's my entire file, but there are many more possible configuration parameters. Checking https://www.time.gov, I found my system was close enough that I couldn't tell any difference. That time isn't necessarily critically accurate either, but if your time is far off from that, something is probably wrong
On this machine, ntpd is started pointing at a "drift" file:
ntpd -f /var/run/ntp.drift -p /var/run/ntpd.pid
so that it knows how much this clock tended to drift from reality.when it last ran. Ordinarily, ntpd will adjust your time very slowly toward "reality" - this is so your programs don't suddenly gain or lose many seconds. You can tell it to just set the correct time instantly, and that is often done at first boot before settling down to small adjustments.
If ntpd is running on one server, you can point other network machines at it instead of at a public time server. This is more apt to keep everyone very much on the same page and of course cuts down on internet traffic too.
Unless you add restrictions to your /etc/ntp.conf file, or block ntpd at your firewall, ntpd will happily provide time information to anyone who asks - probably want to restrict this unless you intended to set up a public time server. By the way, "man ntp.conf" will tell you more about ntpd than "man ntpd" does.
Got something to add? Send me email.
More Articles by Tony Lawrence © 2010-10-27 Tony Lawrence