NTP — Network Time Protocol Configuration Example

Quintas
3 min readAug 11, 2022

Introduction

NTP or Network Time Protocol is a protocol that, together with the client and server programs, synchronizes all the system clocks in a local network within a single millisecond of each other.

This is extremely useful in robotics when you need to perform operations, and for some reason, the robot doesn’t have access to the internet. NTP will then synchronize the time of your robot(client) with your machine(server).

Photo by Milad Fakurian on Unsplash

To successfully configure NTP, you will need to:

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

Notes:

  • Must use Ubuntu command line | Terminal (Ctrl+Alt+t)
  • Run all commands in Terminal, you can simply copy and past them

Install and configure NTP Server on the host computer

Step 1: Install NTP Server with apt-get
sudo apt-get install ntp -y

Step 2: Verify NTP installation and version number (optional)
sntp --version

Step 3: Switch to an NTP server pool closest to your location
Here you need to edit the ntp.conf with nano or vim:
sudo vim /etc/ntp.conf

As indicated in the ntp.cong file:

# Specify one or more NTP servers.# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html

You can use the page https://www.ntppool.org/en/ to choose a pool of time servers closest to your location.

In my case, I want a pool list from servers in Europe. So edit/replace the following:

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

with:

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

For more information about the ntp.conf file, please see this link.

The system always tries to find the closest available servers for you.

Step 4: Restart the NTP server for switching the servers
sudo service ntp restart

Step 5: Verify that the NTP Server is running
sudo service ntp status

Step 6: Configure Firewall so that client(s) can access the NTP server
Since the NTP server works at UDP Port number 123, you should open the port for incoming traffic:
sudo ufw allow from any to any port 123 proto udp

Configure NTP Client to be Time Synced with the NTP Server

Step 1: Install ntpdate and NTP
sudo apt-get install ntpdate ntp -y

Step 2: Specify the IP and hostname of the NTP server in the host's file
Edit the /etc/hosts with nano or vim:
sudo vim /etc/hosts

Add your NTP server’s IP and specify a hostname to the file:
192.168.x.xx NTP-server-host

Step 3: Check if the client machine’s time is synchronized with the NTP server
sudo ntpdate NTP-server-host

Step 4: Disable the systemd timesyncd service on the client
To properly sync time with the NTP server, you must disable the timesyncd service on the client side. For that, use the following command:
sudo timedatectl set-ntp off

Step 5: Switch the NTP pool to your server machine
Here you need to edit the ntp.conf with nano or vim:
sudo vim /etc/ntp.conf

Editing/replacing the following:

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

with:

server NTP-server-host prefer iburst

Step 6: Restart the NTP server
sudo service ntp restart

Step 7: View the Time Synchronization Queue
Congrats, your client and server machines are hopefully able to synchronize. To view the time synchronization queue run:
ntpq -ps

References

source 1
source 2
source 3

--

--