Using NTP to Sync Your Clock on Linux

ntp-clock There is one thing that irritates me the most with computers: the clock. When you think about it, we know how to connect to machines across seas, how to express our feelings to the whole world, and yet, for a long time, my computer’s clock was off by a few minutes. I’ll admit that it isn’t a matter of the utmost importance, but it’s still very frustrating. However, if you use some out-of-the-box distributions like Ubuntu, you’ve probably never experienced this problem, simply because the clock is already configured to synchronize with what is called a time server. The frustrated people are in fact the ones who use distributions that require a bit of configuration at the beginning, like Archlinux. In that case, the synchronization has to be set up manually, using NTP (Network Time Protocol).

First of all, you will need the “ntp” package on your computer. On most distributions, it is installed by default, but you may want to check that you have it or its equivalent. To check, try the command

ntpq

And if it is not found, you know that you don’t have the appropriate package.

Now that this is done, the entire configuration is going to be made with the file “/etc/ntp.conf.” All you have to do for an instant result is to modify these lines which are the default ones:

 server 0.pool.ntp.org
 server 1.pool.ntp.org
 server 2.pool.ntp.org
 server 3.pool.ntp.org

Instead, there are two things you need to do:
Find the addresses of the time servers closest to you on pool.ntp.org and copy-paste them instead of the default ones. As an example, the addresses for the U.S. are

server 0.us.pool.ntp.org
server 1.us.pool.ntp.org
server 2.us.pool.ntp.org
server 3.us.pool.ntp.org

Add “iburst” at the end of each server lines. This option will send a “burst” of packets in case the initial connection with the server fails.

So, in the end, if you are in the U.S., your addresses should look like that:

server 0.us.pool.ntp.org iburst
server 1.us.pool.ntp.org iburst
server 2.us.pool.ntp.org iburst
server 3.us.pool.ntp.org iburst

Now that the servers are correctly setup, you can launch the synchronization process. You may want to first test that the connections are working and that the servers are up. For that, use the command

 ntpq -p

If you see something like this, then you’re fine. 

ntp-testing

Then, the easiest way to synchronize is to add ntp as a daemon. Edit your “/etc/rc.conf” file (or the equivalent, depending on your distribution) to add “ntpd” after your Internet connection daemon. In this case, you will have to blacklist the hwclock daemon. So your rc.conf file should contain something like:

 DAEMONS=(...[Internet connection like wicd]...!hwclock ntpd...)

An alternative if you don’t want it as a daemon is to launch the command

 sudo ntpd -qg

And then update your system clock with

 sudo hwclock -w

Note that in some circumstances, your clock should already be broadly at the right time, but off by a few minutes. The synchronization may not work if your time differs by a couple of hours from the servers’ time.

With this, your clock should always indicate the right time. Overall, I find the configuration and the synchronization to be pretty straightforward. It’s still more complex than it is with Ubuntu, but it is the price that those who want more control must pay. Alternatively, systems like Chrony and OpenNTPD offer the same service, and work generally in the same way.

Do you use ntpd? Or do you prefer some other service? And which method do you use for your synchronization? Please let us know in the comments.