Wednesday, July 15, 2015

Configuring NTP between two linux machines

I have two virtual boxes at home. One that maintains system time accurately and another that always shows the time of some other country. I let the one with the correct time be considered the server and the other one be the client which polls the server using NTP (which is installed by default in Linux).

On the server side, use "su" to become the root user.

vi /etc/ntp.conf

Set these values in the ntp.conf file:
fudge stratum 10

Save and exit the vi editor.

service ntpd start

The basically refers to the same computer. Something like the localhost URL.
Stratum is the level of confidence you have in the time of your server. Stratum 0 would mean you have the highest confidence in it. So here we set it to 10, just-like-that. After you configure the client system, you'll see that the stratum of the client will be 11. i.e. lower than the trust you have on the server time.

On the client side:
Get into root mode.

vi /etc/ntp.conf

Comment out all other server address lines and add your server systems address.


Save and exit vi.

service ntpd restart

ntpq -p will show you a table of the network jitter, polling time, server address and other useful data. If you see an asterisk just before the server address, it means that the server is synchronized with your client machine. The offset value shown is the difference in time between the client and server. To convert the value to seconds, shift the decimal thrice to the left.

You can also use ntpdate -d to debug and see what is going on. This command will also show you the time difference between client and server.

Configuring polling time

Well frankly, don't try to configure it.
NTP will automatically choose between a polling value of 64 seconds to 1024 seconds. So don't be surprised if it takes 17 minutes for time to be synchronised. As time elapses, the clock time will drift away from the correct time and NTP will set it right. You have to allow a few days for NTP to select the right polling value based on network jitter and other parameters. NTP does not immediately trust the time it receives from the server. If you set a low polling time using the minpoll command, the scope for erroneous synchronization increases. So the best thing to do is to just ensure your configuration settings have synchronized the client and server and leave it at that. NTP will handle the polling automatically and optimally.

No comments: