Setting Up a Basic NTP Server on Linux

Time synchronization is a vital component of some networks where knowing the exact time of events must be known, or when timestamps on packets of information must be exact to ensure the integrity of the data.

There are some time synchronization protocols available that use IP networks for transport, major ones being NTP (Network Time Protocol) which is good enough for day to day use and PTP (Precision Time Protocol) that is much more precise than NTP and could be used in TDD-LTE and 5G networks for time-slot synchronization.

In this post I’ll be explaining how to set up a basic NTP server using Chrony as the service. So let’s begin…

Wait, what is Chrony? Are there other packages available? The answer is that there are three popular packages available for NTP service, Chrony, ntp, and openntpd. However, I have chosen Chrony for two reasons. One is it has a relatively small footprint on the server (the binary daemon is just 278 KB) and two is that it can synchronize time much faster than ntp. I’m not going to compare these two packages with openntpd because it lacks a lot of features they have. For further comparison I would recommend reading here.

Installation

Depending on the Linux distribution you use, the installation is different:

On Ubuntu:

# apt install chrony

on RHEL or CentOS:

# yum install chrony

After installing the required packages, you need to edit the configuration file to meet your environment’s needs. The location of the configuration file on Ubuntu is /etc/chrony/chrony.conf and on RHEL or CentOS is /etc/chrony.conf.

The first part should be edited is the upstream server you would like to use as your reference. It is recommended to use well-known trusted servers close to your server’s location for better performance, so if I want to configure my server for example, I would use ir.pool.ntp.org as my upstream server. But it is worth noting that this address has more than one server behind it for redundancy purposes. So, in the chrony.conf file edit the line that starts with the word server and set it up accordingly.

server NTP_SERVER iburst

Now the only configuration needed is to allow clients to connect and synchronize their clocks with your new NTP server. For this part you need to add the following line to the configuration file. If necessary, replace 10.0.0.0/24 with your subnet(s).

allow 10.0.0.0/24

After saving the file, all you need to do is to restart your Chrony daemon by typing in the command(s) below.

For Ubuntu:

# service chrony restart

For RHEL or CentOS:

# systemctl enable chronyd.service
# systemctl start chronyd.service

To check if your server is synchronizing with the upstream servers, type in the command on the server to get the status of the service.

# chronyc sources
210 Number of sources = 4
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^+ 77.104.70.70                  3  10   377   196  +4432us[+3978us] +/-   93ms
^+ ntp5.mobinnet.net             2  10   377   796    +93us[ -507us] +/-   78ms
^* ns3.shabdiznet.com            2   6   377    27  +3758us[+3304us] +/-   42ms
^+ ntp.iranet.ir                 2  10   377   734  -2646us[-3247us] +/-   76ms
root@ntp-01:~#

One Reply to “Setting Up a Basic NTP Server on Linux”

Leave a Reply

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