How to Install and Configure Linux NTP Server and Client

by Karthikeyan Sadhasivam on June 25, 2014

NTP stands for Network Time Protocol.

It is used to synchronize the time on your Linux system with a centralized NTP server.

A local NTP server on the network can be synchronized with an external timing source to keep all the servers in your organization in-sync with an accurate time.

I. 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.

For example, on RedHat or CentOS, use yum to install ntp as shown below:

yum install ntp

2. Setup Restrict values in ntp.conf

Modify the /etc/ntp.conf file to make sure it has the following two restrict lines.

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery

The first restrict line allows other clients to query your time server. This restrict line has the following parameters

  • noquery prevents dumping status data from ntpd.
  • notrap prevents control message trap service.
  • nomodify prevents all ntpq queries that attempts to modify the server.
  • nopeer prevents all packets that attempts to establish a peer association.
  • Kod – Kiss-o-death packet is to be sent to reduce unwanted queries

The value -6 in the second line allows forces the DNS resolution to the IPV6 address resolution. For more information on the access parameters list, Please refer to documentation on “man ntp_acc”

3. Allow Only Specific Clients

To only allow machines on your own network to synchronize with your NTP server, add the following restrict line to your /etc/ntp.conf file:

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

If the localhost needs to have the full access to query or modify, add the following line to /etc/ntp.conf

restrict 127.0.0.1

4. Add Local Clock as Backup

Add the local clock to the ntp.conf file so that if the NTP server is disconnected from the internet, NTP server provides time from its local system clock.

server  127.127.1.0 # local clock
fudge   127.127.1.0 stratum 10

In the above line, Stratum is used to synchronize the time with the server based on distance. A stratum-1 time server acts as a primary network time standard. A stratum-2 server is connected to the stratum-1 server over the network. Thus, a stratum-2 server gets its time via NTP packet requests from a stratum-1 server. A stratum-3 server gets its time via NTP packet requests from a stratum-2 server, and so on.

Also stratum 0 devices are always used as reference clock.

5. Setup NTP Log Parameters

Specify the drift file and the log file location in your ntp.conf file

driftfile /var/lib/ntp/ntp.drift
logfile /var/log/ntp.log

driftfile is used to log how far your clock is from what it should be, and slowly ntp should lower this value as time progress.

6. Start the NTP Serrver

After setting up appropriate values in the ntp.conf file, start the ntp service:

service ntpd start

II. Configure NTP Client to Synchronize with NTP Server

7. Modify ntp.conf on NTP Client

This setup should be done on your NTP Client (Not on NTP-server)

To synchronize the time of your local Linux client machine with NTP server, edit the /etc/ntp.conf file on the client side. Here is an example of how the sample entries looks like. In the following example, you are specifying multiple servers to act as time server, which is helpful when one of the timeservers fails.

server 0.rhel.pool.ntp.org iburst 
server 1.rhel.pool.ntp.org iburst 
server 2.rhel.pool.ntp.org iburst 
server 3.rhel.pool.ntp.org iburst

iburst: After every poll, a burst of eight packets is sent instead of one. When the server is not responding, packets are sent 16s interval. When the server responds, packets are sent every 2s.

Edit your NTP.conf to reflect appropriate entries for your own NTP server.

server 19.168.1.1 prefer

prefer: If this option is specified that server is preferred over other servers. A response from the preferred server will be discarded if it differs significantly different from other server’s responses.

8. Start the NTP Daemon

Once the ntp.conf is configured with correct settings, start the ntp daemon.

/etc/init.d/ntp start

You will see the NTP will slowly start to synchronize the time of your linux machine with the NTP Server.

9. Check the NTP Status

Check the status of NTP using the ntpq command. If you get any connection refused errors then the time server is not responding or the NTP daemon/port is not started or listening.

# ntpq -p
     remote           	refid      st t when poll reach   delay   offset  jitter
==============================================================================
*elserver1 	19.168.1.1       3 u  300 1024  377    1.225   -0.071   4.606

10. Set Local Date and Time

The ntpdate command can be used to set the local date and time by polling the NTP server. Typically, you’ll have to do this only one time.

Your jitter value should be low, else check the drift from the clock in the driftfile. You may also need to change to some other NTP server based on the difference. This command synchronizes the time with your NTP server manually.

ntpdate –u 19.168.1.1

After this initial sync, NTP client will talk to the NTP server on an on-going basis to make sure the local time reflects the accurate time.

You can also use the following command to get the current status of ntpd.

# ntpdc -c sysinfo
system peer:          thegeekstuff.com
system peer mode:     client
leap indicator:       00
stratum:              4
precision:            -23
root distance:        0.00279 s
root dispersion:      0.06271 s
reference ID:         [19.168.1.1]
reference time:       d70bd07b.f4b5cf2b  Wed, Apr 30 2014 15:41:47.955
system flags:         auth monitor ntp kernel stats
jitter:               0.000000 s
stability:            0.000 ppm
broadcastdelay:       0.000000 s
authdelay:            0.000000 s

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 4 comments… read them below or add one }

1 sacarde June 26, 2014 at 1:45 am

hi,
which are differences between “offset” and “delay” ?

thank you

2 Jalal Hajigholamali June 27, 2014 at 11:08 pm

Hi,

Very nice and useful article…

3 LnxGnome July 10, 2014 at 7:31 am

You should highlight “if the NTP server is disconnected from the internet”, in step “4. Add Local Clock as Backup”, as the “local clock” should not be used when upstream NTP servers are available.

4 Pouriya Ebrahimzadeh November 26, 2014 at 5:18 am

Hi,
Thank you.

Leave a Comment

Previous post:

Next post: