×


NTP Server and Client on Ubuntu - How to Configure it ?

NTP is a networking protocol used to synchronize all system clocks in a network.

This enables all the systems in a network to have the same time. 

NTP does so by contacting a number of other time servers on the internet. 

NTP uses UDP port 123 to communicate with clients and other NTP servers.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Ubuntu related tasks.

In this context, we shall look into how to configure the NTP server and client on the Ubuntu System.


How to Configure NTP Server and Client on Ubuntu?

Here, we will use two Ubuntu machines with the following details:

NTP host – Hostname: ntp-host, Static IP address: 192.168.72.150

NTP Client – Hostname: client, IP address: 192.168.72.151


Steps to Install and Configure NTP Server on Ubuntu Host Machine ?

To install and configure the NTP server on Ubuntu, you will need to perform all the steps that are outlined below.


1. Execute Apt Update

First, you will have to update the packages list in order to download the latest version of the NTP package. 

Open the Terminal and run the following command to do so:

$ sudo apt update


2. Install NTP on Host Server

To install the NTP server on the host machine, execute this command:

$ sudo apt install ntp

After running the above command, you may be asked if you want to continue the installation by providing you with the y/n (yes/no) option. Enter y to continue the installation process.

To verify if the NTP is installed and to view the version information, run the following command:

$ sntp --version


3. Configure NTP Server

To configure the NTP server, you are required to have two elements on your host machine: one is /etc/ntp.conf file and the other is ntpd daemon. 

i. First, we will configure the ntp.conf file and then we will restart the ntpd daemon.

ii. Edit the /etc/ntp.conf file using the following command:

$ sudo nano /etc/ntp.conf

When the NTP server is installed, it is by default configured to fetch the time from the following servers:

pool 0.debian.pool.ntp.org iburst
pool 1.debian.pool.ntp.org iburst
pool 2.debian.pool.ntp.org iburst
pool 3.debian.pool.ntp.org iburst

You can replace them with the time servers nearest to your location. Visit the ntp.org  website to find the NTP pool servers nearest to your location.


iii. To use any specific NTP pool server, you will need to add them to your ntp.conf file. 

We are going to add pools servers for the Asia region.

In the ntp.conf file, replace the default pool list with the following lines:

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

iv. Then save and close the ntp.conf file.


4. Restart NTP Daemon

Now you should restart the NTP daemon. Use the following command to do this:

$ sudo service ntp restart

Now, run the following command to check if the NTP server is running without any issues:

$ sudo service ntp status


5. Allow NTP through Firewall

If you are using a firewall, then you will need to configure it to allow UDP connections to NTP port 123. 

Run the following command to allow clients access to NTP server:

$ sudo ufw allow 123/udp

Now run the following command to verify if the rule has been successfully added:

$ sudo ufw status


How to Install and Configure NTP Client on Ubuntu Client Machine ?

To configure a machine as an NTP client, you will need to ensure that the offset (time difference between the local clock and NTP server) is not more than 1024 seconds. 

If the offset is greater than this value, the time source will be considered inaccurate. 

For this reason, we will first try to manually synchronize the NTP client with the NTP server using the ntpdate

If the offset value comes less than 1024 seconds, we will then configure ntpd.conf file for automatic synchronization.


Step 1: Sync NTP Client with NTP Server Manually Using ntpdate

Ntpdate is used to synchronize the time of the NTP client with the NTP server for once only. 

To install it on the client machine, use the following command:

$ sudo apt install ntpdate

After running the above command, you may be asked if you want to continue the installation by providing you with the y/n (yes/no) option. Enter y to continue the installation process.


Now use the following command to manually sync the NTP client with the NTP server:

$ sudo ntpdate <ntp-server-hostname/ip>

In our scenario, it would be:

$ sudo ntpdate 192.168.72.150

The output of the ntpdate command will show offset value (time difference between the local clock and NTP server). Therefore, we can now configure ntp.conf to automatically synchronize the time with our NTP server.


Step 2: Hosts File Configuration

Add the following hostname entry in the /etc/hosts file of the client machine. 

This step is required to resolve the NTP server through the hostname.

<ntp-server-hostname> <ntp-server-ip>

Make sure to replace ntp-server-hostname and ntp-server-ip with the hostname and IP address of your NTP respectively. 

In our scenario, it would be:

192.168.72.150 ntp-host


Step 3: Disable systemd timesyncd Daemon

As we are going to setup NTP, therefore disable the systemd timesyncd daemon. Use the following command to do so:

$ sudo timedatectl set-ntp off


Step 4: Sync NTP Client with NTP Server automatically Using NTP Configuration File

Now we will configure our client machine to sync with the NTP server automatically.

i. For this reason, we will install NTP using the following command on the client system:

$ sudo apt install ntp

After running the above command, you may be asked if you want to continue the installation by providing you with the y/n (yes/no) option. Enter y to continue the installation process.

ii. Once the installation is finished, configure ntp.conf file.

Run the following command to edit the ntp.conf file:

$ sudo nano /etc/ntp.conf

iii. Insert the following line in the file:

server <ntp-server-hostname/ip> prefer iburst

In our scenario, it would be:

server 192.168.72.150 prefer iburst

In the above line, prefer is used to mark the specified server as the preferred server for NTP synchronization. 

Where the iburst is used to send six packets for synchronization instead of the usual one (in case the NTP server is unreachable).

iv. Now restart the NTP daemon to apply the configuration changes:

$ sudo service ntp restart


How to Check NTP Synchronization Status ?

Now to check if NTP is working correctly, run the following command:

$ ntpq -p

In the output of the above command, remote is the NTP server hostname and refid is the top-level server to which our NTP server has itself connected for synchronization.


[Need urgent assistance to set up NTP server on Linux? We can help you. ]


Conclusion

This article will guide you on how to configure the #NTP server on Ubuntu. You will learn how to configure the NTP client on another #Ubuntu machine and synchronize it with the NTP server. By means of this, you can easily synchronize all system clocks in a network.

The Network Time Protocol (NTP) is a networking protocol for clock synchronization between computer systems over packet-switched, variable-latency data networks.

NTP is intended to synchronize all participating computers to within a few milliseconds of Coordinated Universal Time (UTC).


To Configure NTP server:

1. Install NTP Server. First, install NTP package on your server using the appropriate package management tool that is available on your Linux distro.

2. Setup Restrict values in ntp. conf.

3. Allow Only Specific Clients.

4. Add Local Clock as Backup.

5. Setup NTP Log Parameters.

6. Start the NTP Serrver.


To Configure NTP client:

1. To configure your Linux system as an NTP client, you will need to install the ntp daemon (ntpd).

2. The ntpd configuration file is located at /etc/ntp.conf.

3. This file contains the list of NTP servers that will be used for time synchronization.

4. Next, restart the NTP deamon with the sudo service ntp reload command