NTP which is short for the Network Time Protocol is a protocol that is used to synchronize all the date and time of system clocks in a network to use the same time. The term NTP stands for the protocol itself and also the client and server programs running on the networked computers. NTP belongs to the traditional TCP/IP protocol suite and can easily be classified as one of its oldest parts.

When you are initially setting up the clock, it takes six exchanges within 5 to 10 minutes before the clock is set up. Once the clocks in a network are synchronized, the client(s) update their clocks with the server once every 10 minutes. This is usually done through a single exchange of message(transaction). These transactions use port number 123 of your system.

In this article, we will be going over a step-by-step process on how to install and configure the NTP server on an Ubuntu 19.04 machine as well as configuring the NTP Client to be time synced with the server.

Update Ubuntu Packages

In order to install the latest available version of software packages from the Internet repositories, your local repository index needs to be in line with them. Run the following command as sudo in order to update your local repository index:

$ sudo apt-get update

Step 1: Install NTP Server with apt-get

Please run the following command as sudo in order to install NTP server daemon from the APT repositories:

$ sudo apt-get install ntp

The system might ask you the password for sudo and also provide you with a Y/n option to continue the installation. Enter Y and then hit enter; NTP server will then be installed on your system. The process may, however, take some time depending on your Internet speed.

Step 2: Verify installation (optional)

You can verify your NTP installation and also check the version number by running the following command in your Terminal:

$ sntp --version

Step 3: Switch to an NTP server pool closest to your location

When you install the NTP server, it is mostly configured to fetch proper time. However, you can switch the server pool to the ones closest to your location. This includes making some changes in the /etc/ntp.conf file.

Open the file in the nano editor as sudo by running this following command:

$ sudo nano /etc/ntp.conf

In this file, you will be able to see a pool list. We have highlighted this list in the above image. The task here is to replace this pool list by a pool of time servers closest to your location. The pol.ntp.org project provides reliable NTP service from a big cluster of time servers. To choose a pool list according to your location, visit the following page:

https://support.ntp.org/bin/view/Servers/NTPPoolServers

We have searched for a pool list for the US:

The page tells us to add the following lines to the ntp.conf file:

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

Step 4: Restart the NTP server

In order for the above changes to take effect, you need to restart the NTP server. Run the following command as sudo in order to do so:

$ sudo service ntp restart

Step 5: Verify that the NTP Server is running

Now, check the status of the NTP service through the following command:

$ sudo service ntp status

The Active status verifies that your NTP server is up and running.

Step 6: Configure Firewall so that client(s) can access NTP server

Finally, it is time to configure your system’s UFW firewall so that incoming connections can access the NTP server at UDP Port number 123.

Run the following command as sudo to open port 123 for incoming traffic:

$ sudo ufw allow from any to any port 123 proto udp

Your Ubuntu host machine is now configured to be used as an NTP server.

Configure NTP Client to be Time Synced with the NTP Server

Let us now configure our Ubuntu client machine to be time synchronized with the NTP server.

Step 7: Install ntpdate

The ntpdate command will let you manually check your connection configuration with the NTP-server. Open the Terminal application on the client machine and enter the following command as sudo:

$ sudo apt-get install ntpdate

Step 8: Specify IP and hostname of the NTP server in the hosts file

For your NTP server to be resolved by a hostname in your client machine, you need to configure your /etc/hosts file.

Open the hosts file as sudo in the nano editor by entering the following command:

$ sudo nano /etc/hosts

Now add your NTP server’s IP and specify a hostname as follows in this file:

Quit the file by hitting Ctrl+X and then save it by entering y.

Step 9: Check if the client machine’s time is synchronized with NTP server

The following ntpdate command will let you manually check if time is synchronized between the client and server systems:

$ sudo ntpdate NTP-server-host

The output should ideally show a time offset between the two systems.

Step 10: Disable the systemd timesyncd service on the client

Because we want our client to sync time with the NTP server, let us disable the timesyncd service on the client machine.

Enter the following command to do so:

Step 11: Install NTP on your client

Run the following command as sudo in order to install NTP on your client machine:

$ sudo apt-get install ntp

Step 12: Configure the /etc/ntp.conf file to add your NTP server as the new time server

Now we want our client machine to use our own NTP host server to be used as the default time server. For this, we need to edit the /etc/ntp.conf file on the client machine.

Run the following command as sudo in order to open the file in the Nano editor:

$ sudo nano /etc/ntp.conf

Then, add the following line in the file, where NTP-server-host is the hostname you specified for your NTP server:

server NTP-server-host prefer iburst

This is how my file looks like after I have specified the time server:

Step 13: Restart the NTP server

In order for the above changes to take effect, you need to restart the NTP service. Run the following command as sudo in order to do so:

$ sudo service ntp restart

Step 14: View the Time Synchronization Queue

Now your client and server machines are configured to be time-synced. You can view the time synchronization queue by running the following command:

$ ntpq -ps

You should be able to see NTP-server-host as the time synchronization host/source in the queue. So this was all you needed to know about installing and configuring NTP to synchronize time on your networked Ubuntu machines.

The process may seem a little cumbersome but if you follow all of the above steps carefully, one-by-one, your machines will be synced in no time.