Never Ending Security

It starts all here

How To Setup and Configure a NTP Server on Linux

How to: Linux NTP Server

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.

Install NTP Server

First, install NTP package on your server using the appropriate package management tool that is available on your Linux distro.

apt-get install ntp

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”

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 mask nomodify notrap

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


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 # local clock
fudge 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.

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.

Start the NTP Serrver

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

/etc/init.d/ntpd start

Configure NTP Client to Synchronize with NTP Server

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 iburst 
server iburst 
server iburst 
server 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 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.

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.

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
*server1       3 u  300 1024  377    1.225   -0.071   4.606

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

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:
system peer mode:     client
leap indicator:       00
stratum:              4
precision:            -23
root distance:        0.00279 s
root dispersion:      0.06271 s
reference ID:         []
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

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s