install ntp server & client in ubuntu

How to Setup NTP Server & Client in Ubuntu

Network Time Protocol (NTP) is responsible for synchronizing the clocks of systems in a network. It is available as a server-client setup that uses the same protocol. In this article, we will learn how to setup NTP server & client in Ubuntu.


How to Setup NTP Server & Client in Ubuntu

Here are the steps to setup NTP server & client in Ubuntu.


1. Update System

Open terminal and run the following command to update system packages.

$ sudo apt update -y


2. Install NTP Server

Next, install NTP server package with the following command. Please run these commands on the system where you want to install NTP server. Once installed, the different NTP clients on your network will regularly message it for clock synchronization.

$ sudo apt install ntp 

If you see any intermediate prompts, enter y, or yes.

After installation, you can verify NTP server’s version with the following command.

$ sntp --version


3. Configure NTP Server Pools

NTP Server comes with a pre-configured configuration file /etc/ntp.conf that contains specifications for 4 NTP pool servers

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

pool ntp.ubuntu.com

These work fine but sometimes you may want to connect to a pool server closer to your server’s location. Here is a link to get list of all available pool servers depending on location.

To change the pool servers, open the configuration file in a text editor.

$ sudo vi /etc/ntp.conf

Change the above pool configuration lines as per your requirement. Save and close the file.

Restart the server to apply changes.

$ sudo systemctl restart ntp


4. Update Firewall

Next, we need to enable firewall rules for NTP. Run the following command to allow inbound connections to NTP server.

$ sudo ufw allow ntp 
OR
$ sudo ufw allow 123/udp 

Reload the firewall to apply changes.

$ sudo ufw reload

At this point, we have installed and configured NTP server in Ubuntu.


Install NTP Client in Ubuntu

Next, we need to install NTP client in Ubuntu. Please note, you need to run the following commands on the system where you want to install the client. Once installed, the NTP client will communicate with NTP server and regularly synchronize its clock.


1. Update System Packages

First, open terminal and run the following command to update system packages.

$ sudo apt install ntpdate


2. Add NTP Server to /etc/hosts

Next, we need to add the NTP server’s IP address (e.g. 54.43.32.21) to /etc/hosts file on each NTP client system. Open it in a text editor.

$ sudo vi /etc/hosts

Add the following line.

54.43.32.21 bionic

Save and close the file.


3. Verify Time Sync

Next, we need to check if the time sync is happening correctly between NTP server & client. Here is the syntax for the command for verifying time sync.

$ sudo ntpdate NTP-server-hostname

Since our server hostname is bionic, we will run the following command.

$ sudo ntpdate bionic

It will display the time offset between NTP server & client.

To sync client-server clocks, run the following command to turn off timesynchd on client system.

$ sudo timedatectl set-ntp off


4. Install NTP client

Run the following command to install NTP client.

$ sudo apt install ntp

Enter y whenever prompted during installation.


5. Configure NTP client

Open NTP client configuration file in a text editor.

$ sudo vi /etc/ntp.conf

Add the following line to it. Replace bionic with your NTP server’s name.

server bionic prefer iburst

Save and close the file. Restart NTP client to apply changes.

$ sudo systemctl restart ntp


6. Verify NTP Time Synchronization

To view time sync details, run the following command.

$ ntpq -p

You will see a similar output.

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
  bionic          71.76.79.71      2 u    6   54  371    0.525   -0.242   0.053

That’s it. In this article, we have learnt how to install & configure NTP Server & Client on Ubuntu.

Also read:

How to Add User to Sudoers in Linux
How to Install Brotli for NGINX in Ubuntu
How to Uninstall Docker in Ubuntu
How to Install Brotli for Apache in Ubuntu
How to Uninstall Asterisk in Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *