by ComputerBob
May 25, 2008
Currently, Debian users have two main choices of software to install to synchronize their PCs' clocks to online NTP servers — Ntpdate and NTP. I tried to use Ntpdate several different times in both Debian Etch and Lenny, but it didn't work for me. I also spent a lot of time trying to configure NTP to work with the specific time servers that I wanted it to use.
If you do a search for information about how to get your Debian PC to use NTP, you'll find lots of good information, but it will be scattered between many different web sites. And you'll also find a lot of out-of-date and inaccurate information. How will you know which is which? I know from experience that it's a very confusing and time-consuming, trial-and-error process to sift through everything that's out there to gather all of the good information together so that you can use it.
That's because, since this past December, I've done a lot of trial and error with NTP time-synchronization. In fact, I've probably spent more than 30 hours getting automatic time synchronization to work on my PC several different times. If you've read My Debian Adventure, then you know what I'm talking about. Anyway, yesterday, I installed Ntpdate on my Debian Lenny PC, to automatically synchronize its clock to online time servers each time I boot it up. But this morning, after I booted my PC, I checked /var/log/syslog and discovered that Ntpdate wasn't working correctly. I could open up a root console and type a command to manually make Ntpdate synchronize my PC's clock, but Ntpdate couldn't find my specified time server when I booted my PC — and that's the only time that Ntpdate automatically tries to synchronize. In contrast, NTP uses a daemon that runs at boot and then continues to run all of the time, to continuously keep a PC synchronized to the right time.
So yesterday, when Ntpdate didn't work for me, I didn't spend a lot of time trying to figure out why. Instead, I uninstalled Ntpdate, installed NTP, and then spent a few hours getting NTP to work correctly. After I got it to work the way I wanted, I decided to sit down and write myself a step-by-step guide for future reference, so that in the future, I won't have to go looking through my 30 or 40 NTP browser bookmarks any more. Once I finished writing that one-page cheat sheet for myself, I expanded it into this guide for all Debian Linux users, to save them the trouble of having to figure out how to get NTP to work on their PCs. I'm guessing that the steps that I describe here might also work for people who use Ubuntu, Mepis, and other Debian-based Linux distros.
This guide is meant for Debian newbies and NTP newbies — I'm sure that Debian veterans already know all of this stuff.
Install NTP on your PC, using Synaptic, apt-get, aptitude or your preferred package manager. Don't install Ntpdate.
The NTP service should start working as soon as you install it. So, before you make any configuration changes, first confirm that NTP is working. If it isn't working, you'll know that you didn't cause the problem by somehow messing up NTP's configuration.
Run the following command as root:
ntpq -p
When you install NTP, it comes configured to connect to a pool of NTP time servers that may be located far away from you. That's the pool of servers that NTP connected to when you ran the ntpq -p command above, to make sure that it was working. But to get the most accurate time synchronizations, you'll want to configure NTP to use the pool of time servers that is closest to you. Ideally, you want to use a NTP pool that is located in your country or in a nearby country.
1. If you live in the U.S., change the NTP server lines in /etc/ntp.conf to:
Instead of deleting the lines that contain the names of the default servers, you may want to put a # at the beginning of each of the default server lines to turn them into comments, and then type your server lines underneath them. That will allow you to use the default server lines as examples of how to type your server lines. Don't forget the single space between the word "server" and the server address on each line.
(If you don't live in the U.S., find the NTP server pool that is closest to you by visiting http://www.pool.ntp.org/)
(If you have access to a local NTP server at your company or ISP, you may use its name or IP address instead.)
2. If you want to be able to confirm that NTP is continuing to automatically work after you reboot your PC and it has been running for awhile, uncomment the line in /etc/ntp.conf to enable NTP to log its synchronizations. Then reboot your PC and check the peerstats file, as described below.
3. Restart NTP to enable your changes by running the following command as root:
/etc/init.d/ntp restart
Run the following command as root:
ntpq -pAfter you reboot your PC, Debian logs the first few time sync events in /var/log/syslog but only for a few minutes. If you enabled NTP logging (see Step #2 above), you can confirm that NTP is working at any time by looking at the date and time of the peerstats file:
/var/log/ntpstats/peerstats
The peerstats file contains a record of each time synchronization, but it doesn't say when each time sync was done. But if NTP is working, the date/time of the peerstats file itself will be within 17 minutes of whatever time it is when you check its date/time. That's because NTP time synchronization is a continuous process — it starts out by syncing every 64 seconds, but it intelligently increases the time between syncs as your PC's clock needs fewer and smaller adjustments — and the maximum time between syncs is 1024 seconds (17 minutes). If you leave your file browser window open to the ntpstats folder, don't forget to manually refresh that view in order to see the most recent date/time of the peerstats file.
Make sure that your firewall is configured to allow incoming connections from NTP time servers. Redo the steps of this guide, to make sure that you didn't forget anything. Check ntp.conf for typos. Do a Google search for whatever specific NTP error message(s) that you find in /var/log/syslog. NOTE: In Debian Etch, my PC was unable to connect to any NTP servers when I rebooted, and it would only start doing automatic time synchronizations if I restarted NTP from a root prompt. That problem was caused by the NTP daemon trying to synchronize my PC before my network card had been fully configured during the boot process. I worked around that problem by changing my network card from using DHCP to using manual IP configuration, as I described in My Debian Adventure. I'm very happy to report that I haven't experienced that problem with Debian Lenny — using DHCP, Lenny connects to an NTP server and synchronizes my PC's clock during each boot, and then it continues to synchronize my PC continuously without me having to run any commands or configure my IP address manually.
I hope that you found this guide to be helpful. Please let me know if you have any corrections or suggestions to make it clearer for others to understand.![]()