Monday, September 15, 2008

Sync clocks using ntp on Linux

I have often faced difficulty in using ntp to sync the clocks of 2 machines. One easy way (i never found it easy though!) is to use a common ntp server on both the machines to sync the clock. But this never worked for me for reasons unknown!

So below is an easy way to synchronize the clock on 2 local machines.

Assume that we have 2 machines - M1 and M2. Now synchronizing the clocks of these 2 machines takes just 3 simple steps!

Step 1: Install ntp on both the machines.
Step 2: Now make M1 as the NTP server which is synced to its local clock.
Step 3: Use 'ntpdate' command to synchronize the clock on M2 machine with M1 machine.

Lets go through these 3 steps in detail:
Step 1: I wont go in much detail about installing ntp. Get the latest package for your OS and install it.

Step 2: To make M1 as local NTP server, you need to have 2 important values in the etc/ntp.conf file. [if ntp.conf file is not present, just create on with following values]

server 127.127.1.0 # Local clock IP
peer 10.10.17.10 # M2 machine IP

You can comment out rest of the lines in the ntp.conf file. At the end of this blog I am attaching a simple ntp.conf file. You can use the same one.

Please note the two values given above. The first line "server 127.127.1.0" represents the local system clock IP. Dont confuse this with 127.0.0.1 - the loopback ip. 127.127.1.0 is the special IP address used to represent local clock.
So with this line we are saying to the ntpd demon that use the local clock itself as the reference.

The next line "peer 10.10.17.10" tells the ntp server that this IP is the peer machine. You need to give M2 IP address here.

With this configuration, now start ntpd demon on M1 machine:

#ntpd -c /etc/ntp.conf

Note that ntpd by default reads the config file from /etc/ntp.conf. In case you want to override that with your own config file, you can do that by using -c option.

Once ntpd is started, you need to wait for around 10-15 seconds for the server the sync its time with the local clock. You can verify this by using "ntpq -p" command, as below:

root@10.10.254.140:~/asp_3.0/etc/init.d# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *LOCAL(0) 73.78.73.84 5 l 62 64 377 0.000 0.000 0.001 10.10.17.10 .INIT. 16 u 18h 1024 0 0.000 0.000 4000.00
Note the above output. Observe the first line where it says "LOCAL(0)" and it has a * next to it. Initially when you start the ntpd you wont see the '*'. You need to wait until the * appears next to the LOCAL(0). - Dont worry about the other values.

Once this is done, your ntp server is UP and ready to be used by other peers.

Step 3: On M2 machine, all you need to do is issue 'ntpdate' command with the M1 IP address:

root@10.10.254.137:~/asp_3.0/etc/init.d# ntpdate 10.10.17.20 2
Jun 16:48:14 ntpdate[2541]: step time server 10.10.17.20 offset 7689.396852 sec

Note that ntpdate detected the offset value and adjusted to the local clock.


Verifying clock difference between 2 machines:

Once you are done with synchronizing the clock between 2 machines, you can verify the clock difference using 'clockdiff' utility.

Just issue 'clockdiff ' on the command line, as shown below:

root@10.10.254.137:~/asp_3.0/etc/init.d# clockdiff 10.10.17.20
..
host=10.10.17.20 rtt=562(280)ms/0ms delta=0ms/0ms Mon Jun 2 17:27:07 2008

If you are running clockdiff on M1, pass the IP address of M2 machine to get the difference.
Note the 'delta' value which shows that the difference is 0. There might be a delta of up to 3-5 milliseconds, which is acceptable.

Installing clockdiff:
clockdiff is part of iputils package. You can download this package from http://www.skbuff.net/iputils/iputils-current.tar.bz2

You need to untar the above file and then do a "make". After that clockdiff binary is created. Just copy this binary to /usr/sbin or /usr/bin.


Did you find this post useful? Please leave a comment! Thanks.