Having the correct time on your server is important for both you and your clients. The problem is that the locks on most servers tend to drift a bit. This can cause problems with your clients sites (forum posts, for example can get out of order if the time drifts a lot) or if you need to compare / share server logs with anyone else.
The ‘old school’ method to keep the server time in sync was using the rdate command. While this method works, rdate was typically run via cron (once per day) and the clock can drift a lot in 24 hours.
We suggest using the ntp daemon to keep your time in sync. ntp stands for network time protocal. The difference between ntp and rdate is that ntp constantly micro-adjusts the clock keeping it totally accurate. ntpd does this by communicating with different ntp servers.
Installing ntp is quite easy.
On CentOS/Fedora servers:
yum install ntp
on Debian/Ubuntu servers
apt-get install ntp
The configuration file is located at /etc/ntp.conf. You don’t really need to change anything in the configuration file as the default configuration has several ntp servers listed. If you do want to use different ntp servers you simply change the “server” lines. The default CetnOS configuration looks like this:
# Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). server 0.centos.pool.ntp.org server 1.centos.pool.ntp.org server 2.centos.pool.ntp.org
Changes would look something like this:
# Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). server my.ntp.server1 server my.ntp.server2
You can list as many servers as you like, but it’s suggested to have a minimum of two. ntp will automatically switch between servers if it can’t contact the server that it’s currently using.
The last thing we need to do is start ntp and set it to start on boot. In Debian and Ubuntu this is done automatically by apt-get. In CentOS and Fedeora we have to do it ourselves, but it’s quite easy:
service ntpd start chkconfig ntpd on
The ntpd daemon will now keep your server clock constantly synchronized and you’ll no longer have to deal with the clock drifting.
Thanks for the very straightforward tutorial. I do have a question. How can I check that ntp is actually working?
The best way to check it is using the ntpq command. You’ve just given me an idea for another ‘how to’.