How to Install NTP Server and Client(s) on Ubuntu 22.04 LTS

NTP or Network Time Protocol is a protocol that is used to synchronize all system clocks in a network to use the same time. When we use the term NTP, we are referring to 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 to sync with NTP, 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 messages (transaction). These transactions use port number 123 of your system.

In this article, we will describe a step-by-step procedure on how to:

  • Install and configure the NTP server on a Ubuntu machine.
  • Configure the NTP Client to be time synced with the server.

We have run the commands and procedures mentioned in this article on a Ubuntu 22.04 LTS system.

Install and configure NTP Server on the host computer

Follow these steps to install the NTP server on your Ubuntu server machine:

Note: We are using the Ubuntu command line, the Terminal, to install and configure NTP. You can open the Terminal application either through the application launcher search or the Ctrl+Alt+T shortcut.

Step 1: Update the package repository index

To install the latest software versions from the Internet repositories, your local repository index must align with them. Run the following command as sudo in order to update your local repository index:

$ sudo apt update

Updating Ubuntu package lists

Step 2: Install NTP Server with apt-get

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

$ sudo apt install ntp

Please note that only an authorized user can add, remove and configure software on Ubuntu.

Install NTP Server on Ubuntu

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

Step 3: 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

Check NTP version

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

When you install the NTP server, it is mostly configured to fetch the 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 the following command:

$ sudo nano /etc/ntp.conf

Configure nearest NTP servers

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:

Choose NTP Pool server

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

This is how my file looks after adding the above lines to it:

Add pool servers in ntp.conf file

Quit the file by hitting Ctrl+X and entering y to save changes.

Step 5: Restart the NTP server

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

$ sudo service ntp restart

Step 6: Verify that the NTP Server is running

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

$ sudo service ntp status

Check NTP-Server status

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

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

Finally, it is time to configure your system’s UFW firewall so 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

Configure UFW Firewall for NTP

Your Ubuntu host machine is now configured 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 1: 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

Install ntpdate program

Step 2: 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 3: Check if the client machine’s time is synchronized with an 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 4: 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:

Disable timesyncd

Step 5: 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 6: 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:

Add NTP server to ntp.conf file

Hit Ctrl+x to close the file and enter y to save the changes.

Step 7: 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 to do so:

$ sudo service ntp restart

Step 8: 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, your machines will be synced in no time.