Most times you want all the servers and machines in the network to be synchronized to the same date and time. This has several advantages, mainly in network related operations. It is also important because many applications on the system uses the machine clock as well.
You can set time and date manually on your Linux system. This works most of the time as you need to set it only once and the clock maintains the correct date and time. But as with most clocks in real world, it is quite possible that your clock will run either slow or fast over a period of time, also known as the clock drift. This can cause the clock to get out of sync with the actual time.
What is NTP
Network Time Protocol or NTP is the network protocol used to effectively transmit and synchronize time across different computers and networks over the internet. It provides accurate time across networks in a consistent format, that can then be used by machines to set their own clocks.
There are several advantages in using NTP to set the date and time on your machine. It maintains the correct date and time without any user intervention. It allows you to maintain the same accurate date and time across several machines in the same network.
NTP works in a hierarchical client-server model. At the top of the hierarchy are Stratum 0 servers or Reference Clocks. Below these reference cloaks are Stratum 1 servers which synchronizes their time with these Reference Clocks. These are the best servers that are available to the public machines over the internet.
You can synchronize your machine with any of the time servers available on the internet, which can be in any stratum, 1 to 16.
You will need to install the NTP if it is not already installed. How you install will depend very much on your Linux distribution, and it should be available in your distribution repository under the name ntp. For Debian/Ubuntu distro variations, use the apt-get as follows
sudo apt-get install ntp
In the Redhat or CentOS variations, you can use yum to install ntp
yum install ntp
if you are on Gentoo, then use emerge as below
emerge -av net-misc/ntp
You will first need to edit the ntp configuration file. This file is located at /etc/ntp.conf on most distros. If you cannot find the file under /etc on your machine, then check the Linux distribution documentation for your machine.
The configuration file specifies the servers that will be contacted to which your machine clock will be synchronized. You can find specific machines for your region from the NTP website. For example, the users in North America can use the servers in the pool: 0.north-america.pool.ntp.org as mentioned in http://www.pool.ntp.org/zone/north-america. The machines in the Asia can point to 0.asia.pool.ntp.org (http://www.pool.ntp.org/zone/asia).
When using a pool, the list of servers that the pool points to changes randomly, usually every hour. This will make sure that you will hit a server that is working and also distribute the load much more evenly.
In addition to using the generic pool, you can use specific pools or servers as well. For example, gentoo users can use the 0.gentoo.pool.ntp.org or can you specify the IP address of a specific server or a machine in your network that supports NTP.
For example, the server setting in your configuration file could be like something shown below:
There is really no other settings that you need to modify out of the box. The default settings will work perfectly in most cases. Once you have installed the ntp, there are a couple of options you have to run it. You can run the ntp client as a cron job or as a service on your machine.
Install as a Cron Job
Instead of installing the ntpd service, you can run the ntpdate command as a cron job to update the time at defined intervals. In order to update the time every day, use the following entry in your cron tab file.
0 0 * * * root ntpdate -s us.pool.ntp.org
It is not a good idea to run it as a cron job for a couple of reasons. If your clock drifts too much between updates, then it causes the clock to jump around quite a bit every time it updates. It also causes unnecessary load on the time servers. Always run NTP as a service when possible.
Install as a Daemon/Service
After installing, you can now synchronize the time on the current machine either manually or run the NTP as a service to synchronize time at specific intervals. To get you up and running, make sure that the current time is set to a reasonable value, something that is close to the actual time.
You can set the date and time manually using the command line using the date command. You can also use the ntpdate command to perform this operation as well.
Running as a service has the added advantage that it uses a drift file. This is used to keep the clock reasonably accurate even when the servers cannot be accessed for a period of time. In order to update the time use the following command from the command prompt.
bash# ntpdate pool.ntp.org
You can now start (or restart) the NTP service to synchronize the clock. This may vary a little based on your operating system, but usually the following will work in most cases.
You can verify that your machine is synchronizing the time by using the ntpq command. It could take a little while to synchronize the first time around, so be patient. From the command line execute the ntpq command with the -p option, as below
bash# ntpq -p
This will print out a list of servers, something like what is shown in the example/screenshot below.
Now, in the list of servers verify that one of the remote servers start with an asterisk (*). That is the server that is currently being used to synchronize your clock. Your machine should now have the accurate date and time synchronized to the time servers.