Back to main page

The Quick-N-Dirty Guide to ntpd

1.) Do you have ntpd

At a command prompt type
which ntpd

Hopefully, you will get back an answer like /usr/sbin/ntpd. Note that as it's a system command, you might have to be root or do an su - to find it. (Note that it is an su - with a hyphen after the su. This gives you root's $PATH). If you have ntpd go to step 3, otherwise, go to step 2.

2.) Installing

For help with installing take a look at our QND guide to installing software. If your distribution isn't covered, then you may have to install from source. Fear not, we have a QND guide for installing from source as well.

3.) Configuring ntpd

This can and almost certainly will vary with distribution. In FreeBSD 5.x, one does the following as root or with root privilege
cd /etc
vi ntp.conf

We are about to create a configuration file. In FreeBSD 5.x this is /etc/ntp.conf. It might, in other distributions, be called ntpd.conf. (I don't recollect seeing it anywhere, but I'm not sure. Note that in some distributions, the file might already be there--it might be in /etc or sometimes in an /etc/ntp directory) To check the names of the necessary files for your distribution, check man ntpd. My ntp.conf reads

server 0.pool.ntp.org
server 1.pool.ntp.org
server 2.pool.ntp.org
driftfile /var/db/ntpd.drift
Some Linux distros as well as OpenBSD use the one line
servers pool.ntp.org

I believe the servers, as opposed to server, keyword is for openntpd, which is from OpenBSD and used by many Linux distributions. FreeBSD's base ntpd doesn't seem to recognize the servers keyword, requiring its ntp.conf to be as I have shown above, with server, rather than servers, and listing multiple servers from the ntp.org pool.

Whether one uses openntpd with the servers keyword, or the older version, with server and listing multiple servers, this configuration file tells the ntpd daemon to check the time with any of the servers in the group at pool.ntp.org. To get an idea of the servers being used, you can use
ntpq -p

If you wish to keep any other boxes on your network (or off it) from accessing your ntp daemon add

restrict default ignore

Add ntpd to services to begin at startup. This will vary with distribution. We have a guide to some common ones here.

In most distributions, you can now start it by simply typing

ntpd

at a command prompt.

In FreeBSD 5.x at least, it is started from /etc/rc.conf. One adds the line

ntpd_enable="YES"

(In FreeBSD at least you can leave the driftfile line out of ntp.conf and it will create one by default in /var/db. With some Linux distros, you might have to specify a driftfile. The /var/db/ntpd.drift is the FreeBSD default, it might be different in your Linux distribution. Another common location is /etc/ntpd/ntpd.drift)

4.) Testing ntpd

You want to see that ntpd is listening for udp packets on port 123. So, in FreeBSD I do
netstat -an | grep 123

If I get back something like

udp4    0       0 *.123         *.*

I know that it's working.

In FreeBSD, one usually sets ntpd from rc.conf. If for some reason your computer's clock is really off (dying cmos battery or whatever) and the time difference is too great, ntpd might not work. One solution is to run ntpdate once at startup. If you're going to do this, it will look for the server in /etc/ntp.conf, but you would want to add the -b flag. So, one could have these lines in /etc/rc.conf

ntdate_enable="YES"
ntpdate_flags="-b"
ntpd_enable="YES"

Note that the ntpdate line is above the ntpd line. If you call ntpdate and ntpd is running it will die with an error message.

The other solution is using (on most operating systems) the -s flag. This will cause ntpd to set the time, even if it is completely off. On OpenBSD for example, one can add the line
ntpd_flags="-s"
In FreeBSD, however, the -s flag does something different. However, if you look at /etc/defaults/rc.conf you'll see that they have the ntpd_sync_on_start option, which will sync ntpd even if it has a high offset. In FreeBSD's /etc/rc.conf one would have

ntpd_enable="YES" ntpd_sync_on_start="YES"

Congratulations you're done.

References

man ntpd
man 5 ntp.conf (or ntpd.conf)
The bsdguides.org ntpd guide.
(Note that this guide does a few things differently, such as specifying the driftfile after creating it. file with the touch command.)

Back to main page