Network Time Protocol (NTP) is an Internet protocol used for the transfer of accurate time, providing time information so that a precise time can be obtained and maintained on a network. NTP provides accurate, verified and synchronised time all over the Globe. This NTP Unix article will try to show you how to use Network Time Protocol to maintain and synchronize your system clock on your network devices easily and effortless.

NTP Unix and Client - Server model

NTP's organisation consists of a hierarchical client - server model. In the top of this hierarchy there are a small number of machines known as reference clocks (TimeServers, NtpServers, Stratum Sources). A reference clock is known as Stratum 0 and is typically a cesium clock or a GPS NTP that receives time from satellites. Attached to these machines there are the so-called stratum 1 servers (that is, stratum 0 clients), which are the top level time servers available to the Internet, that is, they are the best NTP servers available.

The NTP measure for synchronization distance is termed as stratum number: the number of steps that a system lies from a primary time source.

Stratum 1 and Hierararchical structure in NTP Unix

Following this hierarchy, the next level in the structure are the stratum 2 servers which in turn are the clients for stratum 1 servers. The lowest level of the hierarchy is made up by stratum 16 servers. Generally speaking, every server is synchronized with a stratum n server is termed as being at stratum n+1 level. So, there are a few stratum 1 servers which are referenced by stratum 2 servers, wich in turn are refenced by stratum 3 servers, which are referenced by stratum 4 and so on. This equation is pretty solved by buying GPS NTP server, which then acts as a direct Stratum 1 Source

Same stratum, peer basis

NTP servers operating in the same stratum may be associated with others in a peer to peer basis, so they may decide who has the higher quality of time and then can synchronise to the most accurate.

In addition to the client-server model and the peer to peer model, a server may broadcast time to a broadcast or multicast IP addresses and clients may be configured to synchronise to these broadcast time signals.

So, at this point we know that NTP clients can operate with NTP servers in three ways:

  1. in a client to server basis
  2. in a peer to peer mode
  3. sending the time using broadcast/multicast

NTP Howto linux, unix, bsd

Whenever ntpd starts it checks its configuration file /etc/ntp.conf to determine syncronization sources, authentication options, monitoring options, access control and other operating options. It also checks the frequency file /etc/ntp/drift that contains the latest estimate of clock frequency error. If specified, it will also look for a file containing the timeserver secure authentication keys /etc/ntp/keys.

The path and/or name of these configuration files may vary in your system. Check the ntp unix -c command line option.

Fire up NTP Daemon in Unix system

Once the NTP daemon is up and running, it will operate by exchanging packets (time and sanity check exchanges) with its configured servers at poll intervals and its behaviour will depend on the delay between the local time and its reference servers. Basically, the process starts when the NTP client sends a packet containing its timestamp to a server. When the server receives such a packet, it will in turn store its own timestamp and a transmit timestamp into the packet and send it back to the client. When the client receives the packet it will log its receipt time in order to estimate the travelling time of the packet.

NTP Server and NTP Daemon

The packet exchange takes place until a NTP server is accepted as a synchronization source, which take about five minutes. The NTP daemon tries to adjust the clock in small steps and will continue until the client gets the accurate time. If the delay between both the server and client is big enough the daemon will terminate and you will need to adjust the time manually and start the daemon again.

NTP Unix Client Workstation

The NTP client program ntpdate for synchronisation sets the system clock once. As real clocks drift, you need periodic corrections. Basically you can run ntpdate in a cron job hourly or daily, but your machine won't be an NTP server then.

Crontab in Unix shell

Crontab entry to update the system clock once a day:

0 2 * * * /usr/sbin/ntpdate -s -b -p 8 -u 129.132.2.21

Entry explanation of crontab input

-b

Force the time to be stepped using the settimeofday() system call, rather than slewed (default) using the adjtime() system call. This option should be used when called from a startup file at boot time.

-p samples

Specify the number of samples to be acquired from each server as the integer samples, with values from 1 to 8 inclusive. The default is 4.

-s

Divert logging output from the standard output (default) to the system syslog facility. This is designed primarily for convenience of cron scripts.

-u

Direct ntpdate to use an unprivileged port or outgoing packets. This is most useful when behind a firewall that blocks incoming traffic to privileged ports, and you want to synchronise with hosts beyond the firewall. Note that the -d option always uses unprivileged ports.

Unix Workstation as NTP Server

First of all you have to download the NTP sources from www.ntp.org. On RedHat Linux 7.0 / 7.1 the NTP server ntpd is already included in the distribution.

The NTP server ntpd will learn and remember the clock drift and it will correct it autonomously, even if there is no reachable server. Therefore large clock steps can be avoided while the machine is synchronized to some reference clock. In additionntpd will maintain error estimates and statistics, and finally it can offer NTP service for other machines.