How to install & configure NTP server and NTP client in Linux?

We might have heard this word NTP server & client many times with minimal idea about it.

However, I will tell you clearly in this article about NTP Server and NTP Client setup

Let us see about Chrony NTP Client setup later.

What is NTP server?

NTP stands for Network Time Protocol.

It is a networking protocol that synchronize the clock between computer systems over the network.

In other hand we can say, It will keep the same time to all the systems which are connected to NTP server through NTP or Chrony client with more accuracy.

NTP can usually maintain time within tens of milliseconds over the public Internet, and can achieve better than one millisecond accuracy in local area networks under ideal conditions.

It uses “User Datagram Protocol” (UDP) on port number 123 for send and receive timestamps. It is a client/server application which send and receive timestamps using the User Datagram Protocol (UDP).

What is NTP Client?

NTP client will synchronize its clock to the network time server.

What is Chrony Client?

Chrony is replacement of NTP client. It can synchronize the system clock faster with better time accuracy and it can be particularly useful for the systems which are not online all the time.

Why we need NTP Server?

To keep all the servers in your organization in-sync with an accurate time to perform time based jobs.

To clarify this, I will tell you a scenario. Let us say  we have two servers (Server1 and Server2). The server1 usually complete the batch jobs at 10:55 and the server2 needs to run another job at 11:00 based on the server1 job completion report.

If both the system is having different time then we can’t perform the task as we planned . To achieve this, we should setup NTP. Hope it cleared the necessity of NTP.

In this article, we are going to use the following setup to test this.

  • NTP Server: HostName: CentOS7.2daygeek.com, IP:192.168.1.8, OS:CentOS 7
  • NTP Client: HostName: Ubuntu18.2daygeek.com, IP:192.168.1.5, OS:Ubuntu 18.04

NTP SERVER SIDE: How to install NTP Server in Linux?

There is no different packages for NTP server and NTP client since it’s a client/server model. The NTP package is available in distribution official repository so, use the distribution package manger to install it.

For Fedora system, use DNF Command to install ntp.

$ sudo dnf install ntp

For Debian/Ubuntu systems, use APT-GET Command or APT Command to install ntp.

$ sudo apt install ntp

For Arch Linux based systems, use Pacman Command to install ntp.

$ sudo pacman -S ntp

For RHEL/CentOS systems, use YUM Command to install ntp.

$ sudo yum install ntp

For openSUSE Leap system, use Zypper Command to install ntp.

$ sudo zypper install ntp

How to configure the NTP server in Linux?

Once you have installed the NTP package, make sure you have to uncomment the following configuration in the /etc/ntp.conf file on server side.

By default the NTP server configuration relies on X.distribution_name.pool.ntp.org. If you want you can use the default configuration or you can change it as per your location (country specific) by visiting https://www.ntppool.org/zone/@ site.

Say for example. If you are in India then your NTP server will be 0.in.pool.ntp.org and it will work for most of the countries.

# vi /etc/ntp.conf

restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 3.asia.pool.ntp.org
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys

We have allowed only 192.168.1.0/24 subnet clients to access the NTP server.

Since firewall is enabled by default on RHEL 7 based distributions so, allow the ntp server/service.

# firewall-cmd --add-service=ntp --permanent
# firewall-cmd --reload

Bounce the service once you update the configuration.

For sysvinit systems and Debian based system we need to run ntp instead of ntpd.

# service ntpd restart

# chkconfig ntpd on

For systemctl systems. For Debian based system we need to run ntp instead of ntpd.

# systemctl restart ntpd

# systemctl enable ntpd

NTP CLIENT SIDE: How to install NTP client on Linux?

As I mentioned earlier in this article. There is no specific package for NTP server and client. So, we can install the same package on client also.

For Fedora system, use DNF Command to install ntp.

$ sudo dnf install ntp

For Debian/Ubuntu systems, use APT-GET Command or APT Command to install ntp.

$ sudo apt install ntp

For Arch Linux based systems, use Pacman Command to install ntp.

$ sudo pacman -S ntp

For RHEL/CentOS systems, use YUM Command to install ntp.

$ sudo yum install ntp

For openSUSE Leap system, use Zypper Command to install ntp.

$ sudo zypper install ntp

I have installed and configured the NTP server on CentOS7.2daygeek.com so, append the same into all the client machines.

# vi /etc/ntp.conf

restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
server CentOS7.2daygeek.com prefer iburst
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys

Bounce the service once you update the configuration.

For sysvinit systems. For Debian based system we need to run ntp instead of ntpd.

# service ntpd restart

# chkconfig ntpd on

For systemctl systems. For Debian based system we need to run ntp instead of ntpd.

# systemctl restart ntpd

# systemctl enable ntpd

Wait for few minutes post restart of the NTP service to get synchronize time from the NTP server.

Run the following commands to verify the NTP server synchronization status on Linux.

# ntpq –p
Or
# ntpq -pn

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*CentOS7.2daygee 133.243.238.163  2 u   14   64   37    0.686    0.151  16.432

Run the following command to get the current status of ntpd.

# ntpstat
synchronised to NTP server (192.168.1.8) at stratum 3
   time correct to within 508 ms
   polling server every 64 s   

Finally run the date command.

# date
Tue Mar 26 23:17:05 CDT 2019

If you are observing a significant offset in the NTP output. Run the following command to sync clock manually from the NTP server. Make sure that your NTP client should be in active state when you perform the command.

# ntpdate –uv CentOS7.2daygeek.com

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...