How to Synchronize Time with NTP in Linux?


Synchronizing time is a key aspect of computer systems, as it ensures that clocks are accurate and compatible with other systems on a network. incorrect time can cause several issues, such as inconsistent data, incorrect timestamps on files, and security risks that are not worth taking. Fortunately, Linux offers various methods for synchronizing time with network time servers, and one such method is through the Network Time Protocol (NTP). NTP is an open-source protocol that provides precise time synchronization between computer systems.

This article will walk you through the process of synchronizing time using NTP on a Linux machine. this will cover everything from installing the NTP package to configuring time sources and checking synchronization status. Whether you're a Linux expert or a system administrator, this article will provide you with a concise guide on how to synchronize time using NTP on Linux systems.

Step 1: Install NTP

To synchronize time using the Network Time Protocol (NTP) on Linux, the initial step is to install the NTP package in your Linux system. NTP is a free and open-source protocol that provides time synchronization between computer systems within a network. The functionality of NTP is based on the exchange of time-stamped packets between servers and clients.

Your system's package manager can be used to install NTP. For example, the apt-get package manager, which is widely used on Ubuntu and Debian systems. we will use that in this example.

The first command,

sudo apt-get update 

This command will update the package list on your system to ensure that you have the latest version of the NTP package available. The second command,

sudo apt-get install ntp 

This command will install the NTP package on your system.

When NTP is installed, it will sync time automatically with the main NTP server mentioned in the /etc/ntp.conf file. This file holds crucial settings for NTP, like which NTP servers to use for time syncing. Most Linux systems set pool.ntp.org as the default NTP server, which is a trusted server used widely in the industry.

To install chrony an alternative to NTP, we can run the following command −

sudo apt-get install chrony

After the installation process is complete, Chrony will automatically start synchronizing time with the default time servers configured in the /etc/chrony/chrony.conf file.

One of the remarkable advantages of Chrony over NTP is that it can handle network jitter and delay much more efficiently. Chrony maintains a local clock with a high-resolution timestamp and uses that to estimate the offset between the local clock and the time source. It then adjusts the local clock to minimize the offset.

To check the synchronization status of Chrony, we can use the chronyc command with the tracking option 

sudo chronyc tracking

The output of this command will show you the current time difference between your system clock and the time sources configured in the /etc/chrony/chrony.conf file. If the time difference is significant, Chrony will gradually adjust the system clock to synchronize with the time sources.

Step 2: Configure NTP

Once NTP is installed on your Linux system, you need to configure it to use the correct time servers. By default, NTP uses the time servers specified in the /etc/ntp.conf configuration file.

To configure NTP to use specific time servers, you will need to edit the /etc/ntp.conf file using a text editor of your choice. For example, you can use nano or vim 

sudo nano /etc/ntp.conf

Inside the configuration file, you will see a list of servers and their associated settings. You can add or remove servers to meet your requirements.

To use NTP Pool Project's time servers, you can add the following lines to the /etc/ntp.conf file 

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

These are the primary NTP Pool Project servers, and they are highly reliable and accessible from anywhere in the world.

Once you have added the servers that you want to use for NTP time synchronization, save the changes to the /etc/ntp.conf file and exit the text editor.

It is important to note that NTP uses a hierarchical approach to time synchronization, where time servers higher in the hierarchy are considered more reliable than those lower down. The hierarchy is typically organized into four tiers, with tier 1 servers being the most reliable and authoritative, and tier 4 servers being the least reliable.

After you have edited the configuration file, you need to restart the NTP service to apply the changes −

sudo service ntp restart

This will restart the NTP service with the new configuration and start synchronizing your system time with the new time servers.

Step 3: Start NTP

When we start the NTP service, it will automatically connect to the time servers that we have specified in the configuration file and begin synchronizing the system time. The NTP service will periodically query the time servers and adjust the system time to keep it in sync with the time servers.

To start the NTP service, we can use the following command 

sudo service ntp start

This command will start the NTP service on our Linux machine. If the service is already running, the command will simply output a message saying that the service is already running.

Step 4: Check the NTP Status

We can use the ntpq command with the -p option to do this. The ntpq command is used to query NTP servers, and the -p option stands for "peer status." This option displays a table of peers along with their associated statistics and reachability information.

Here is the command −

ntpq -p

The output of the command will look like this 

	     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+0.pool.ntp.org  .POOL.          16 p    -   64    0    0.000   -0.020   0.015
*1.pool.ntp.org  .POOL.          16 p    -   64    0    0.000   -0.021   0.015
+2.pool.ntp.org  .POOL.          16 p    -   64    0    0.000   -0.020   0.015
+3.pool.ntp.org  .POOL.          16 p    -   64    0    0.000   -0.022   0.015

The output example shows the successful synchronization of the NTP service with four-time servers from the NTP Pool Project specified in the /etc/ntp.conf configuration file. The presence of a plus sign (+) next to certain server names denotes their current usage for time synchronization.

Each time server is represented in the "refid" column by a unique four-character reference ID that identifies the source of time. The "st" column displays the stratum level of each server, indicating a direct connection to the local machine with a value of 16.

Furthermore, the "delay," "offset," and "jitter" columns provide insights into various time synchronization statistics. The "delay" column showcases the round-trip delay time for each server, while the "offset" column displays the time difference between the local system clock and each time server. Lastly, the "jitter" column exhibits the variation in time offset over time.

Conclusion

In summary, NTP is an important service that helps maintain accurate time on Linux machines. By syncing with external time servers, NTP ensures that the system clock stays accurate, which is crucial for various applications, from network communication to system security.

In the above article, we discussed the steps for synchronizing time with NTP on a Linux machine. We installed the NTP service, updated the configuration file, and checked the synchronization status using the ntpq -p command. By following these steps and regularly monitoring the synchronization status, we can ensure that the system clock remains accurate and reliable.

Having accurate timekeeping is required for modern computing, and NTP plays an important role in achieving this accuracy on Linux machines.

Updated on: 27-Jul-2023

593 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements