Team LiB
Previous Section Next Section

Hack 71. Update Your Clock via the Internet

Staying in time is easier when you let someone else set your clocks.

There is little doubt that time is an important part of your daily life. Everything from watching the latest episode of your favorite TV program to having a meeting with your boss is based around time, and your computer is no different. Inside your computer, hardware and software rely on time to help keep you up-to-date. If our computers didn't know the time, our calendar, email, and personal-information management would be a mess. Each of these tools relies heavily on having an accurate clock, and the challenge is keeping this clock accurate.

This hack explores a little tool called the Network Time Protocol (NTP) that can solve your inaccurate-clock problems. The NTP software synchronizes your computer's clock with a central server on the Internet with impressive accuracy, and if you set this synchronization to occur when your system boots (or at regular intervals), your clock will never be inaccurate again.

9.3.1. Getting NTP

The NTP software is available for download from http://www.ntp.org, but it is probably best to use your distributions package-management program to obtain the software.

With the software installed, you need to find a server with which to synchronize your clock. Many ISPs provide NTP services to their subscribers; contact your ISP to see if it has a server to synchronize with. If your ISP does not provide a server, a number of public NTP servers are available for different regions of the world. The easiest way to find these servers is to search on Google for your location/country and the words public NTP server. For quick synchronizations which lead to a more accurate clock, choose a server as physically close to your geographic location as possible. Also, because you are an end user, you should not synchronize to a stratum 1 server. These servers are reserved for use only when you are setting up your own NTP server that will service thousands of clients.

9.3.2. Synchronizing Your Clock

Within the NTP software is a tool called ntpdate that is used to synchronize your clock with an NTP server. With the server address you just obtained and ntpdate, you can easily synchronize your clock with this command:

foo@bar:~$ ntpdate 
ntp.yourserver.com

When you run this command, ntpdate will connect to the server and perform the synchronization. For an even easier method of running NTP, create a configuration file, /etc/ntp.conf, with your servers included. Use this format to add your server:

server ntp.yourserver.com

With the file created, you need to use the ntpd daemon to read the file and synchronize your clock. You can start it manually with this command:

foo@bar:~$ ntpd

You should use your distribution's startup services software manager to load ntpd when the system boots.

9.3.3. Automate NTP Synchronization

Apart from setting your clock, one of the best uses of NTP is to synchronize your clock regularly to compensate for the natural drift that occurs with a hardware clock. This drift is caused by various issues such as power fluctuations and hardware problems, but if you synchronize with NTP as often as possible, you can reduce this drift.

To schedule synchronization at regular intervals, use a cronjob to automate these NTP updates. [Hack #70] fully explains the uses of cron. You can create your cronjob by opening the cron file with:

foo@bar:~$ crontab -e

Then add a line to define the cronjob. As an example, to synchronize your clock once a day at 3:00 a.m., use this line:

00 03 * * * ntpdate ntp.yourserver.com > /dev/null

When you save and quit the editor, your cronjob will be enabled and your clock will be updated regularly.

    Team LiB
    Previous Section Next Section