Syncing time in Linux and Windows with NTP


There are plenty of reasons you should have your Linux and Windows servers set with the correct time. One of the most obvious (and annoying) is, without the correct time, your Linux machine will be unable to  connect to a Windows Domain. You can also get into trouble with the configuration of your mail and web servers when the time is not correct (sending email from the future is never a good idea). So how do you avoid this? Do you have to constantly be resetting the time on your machines? No. Instead of using a manual configuration, you should set up all of your servers to use NTP (Network Time Protocol) so that they always have the correct time.

Windows Server settings


There is a very simple way to set your Windows Server OS (2000 and later) to use an external time server. To do this simply click on this Fixit link and the registry entries necessary to be changed will be changed and your server will start updating time from an external source.

If you are more of the DIY Windows admin, you will want to know the registry edits that are made by clicking that Fix It link. Here they are (NOTE: The Windows registry is a tool that not all users are qualified to use. Make sure you do a backup of your registry before you make any changes.):

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Parameters\Type

Right-click Type and select Modify. Change the entry in the Value Data box to NTP and click OK.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config\AnnounceFlags

Right-click AnnounceFlags and select Modify. In the Edit D Word change the Value Data to 5 and click OK.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer

In the right pane, right-click Enabled and select Modify. In the Edit D Word change the Value Data to 1 and click OK.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Parameters

In the right pane, right-click NtpServer and select Modify. Change the Value Data to Peers and click OK.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient\SpecialPollInterval

In the right pane, right-click SpecialPollInterval and select Modify. Change the Value Data to Seconds (where Seconds is a number representing the amount of seconds between polls; 900 seconds is ideal) and click OK.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config\MaxPosPhaseCorrection

In the right pane right-click MaxPosPhaseCorrection and select Modify. Change the Value Data to Seconds (where Seconds is a number representing the amount of seconds used for positive corrections; this is used to correct for time zones and other issues).

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config\MaxNegPhaseCorrection

In the right pane, right-click MaxNegPhaseCorrection and select Modify. Change the Value Data to Seconds (where Seconds is a number representing the amount of seconds used for negative corrections; this is used to correct for time zones and other issues).

Once you have made the final registry edit, quit the registry editor and then click Start | Run and enter the following command:

net stop w32time && net start w32time
 
Your Windows machine will now start syncing time to an external server at the set intervals.

On to the Linux server


In order to get NTP up and running you first have to install the ntp daemon on the machine. This is very simple, as ntpd will be located in your default repositories. So, with that in mind, open up a terminal window and issue one of the following commands (dependent upon which distribution you are using). NOTE: If you are using a non-sudo distribution you will need to first su to the root user. Once you have administrative privileges issue one of the following:

  • sudo apt-get install ntp (for Debian-based systems).
  • yum install ntp (for Red Hat-based systems).
  • urpmi ntp (For Mandriva-based systems).
  • zypper ntp (For SUSE-based systems)

Upon installation, your NTP system should be pre-configured correctly to use an NTP server for time. But if you want to change the server you use, you would need to edit your /etc/ntp.conf file. In this file you want to add (or edit) a line to reflect your NTP needs. An entry looks like:

SERVER_ADDRESS [OPTIONS]
 
Where SERVER_ADDRESS is the address of the server you want to use and [OPTIONS] are the available options. Of the available options, there are two that might be of interest to you:

  • iburst: Use this option when the configured server is unreachable. This option will send out bursts of eight packets instead of the default one when trying to reconnect to the server.
  • dynamic: Use this option if the NTP server is currently unreachable (but will be reachable at some point).

By default, the /etc/ntp.conf file will look similar to this:
server 0.debian.pool.ntp.org iburst dynamic
 
server 1.debian.pool.ntp.org iburst dynamic
 
server 2.debian.pool.ntp.org iburst dynamic
 
server 3.debian.pool.ntp.org iburst dynamic

More than one server is used in order to assure a connection. Should one server not be available, another one will pick up the duty.

When you have everything set up correctly, enter the following command:

sudo /etc/init.d/ntp start (on Debian-based machines) OR /etc/rc.d/init.d/ntp start (on most other machines. NOTE: You will need to first su to the root user for this command to work).

Your machine should now start syncing its time with the NTP server configured.

Final thoughts

It may seem like a task that should be unnecessary, but in certain systems and configurations, the precise time is crucial. Whether you are serving up web pages, mail, or trying to connect to a Windows domain, keeping the correct time will make just about ever task either easier or simply correct.