Learn how to configure NTP (Network Time Protocol) on Linux machines to sync time with the NTP server over the network. Also, learn how to manually sync time on NTP.
Nowadays NTP (Network Time Protocol) is one of the essential things in any IT infrastructure. Apart from production even development or test environments also backed with NTP to ensure smooth operations. Let’s see what is NTP and how to configure it in the Linux machine.
What is NTP
NTP is the protocol used to sync the time of machines with the NTP server (can be an appliance or another Linux machine) over the network. It aims at keeping all the machines clock in sync so that there will be no delays between any two machines in a network. This is very crucial in production environments running finance data. Network time protocol runs on UDP port 123. This should be open between the time server and client in both directions.
What is NTP server
NTP server can be another machine with NTP server-side configuration running or it can be a dedicated NTP appliance. NTP appliance is a small rackmount server looking device that has an antenna attached to it. An antenna can be extended to building rooftops to ensure better signal receiving. These appliances receive signals and hence synchronize their own time with satellites in space. Now their own system time will be set a benchmark to sync other machines with it over the network. The appliance comes with its own configuration which can be done on front display buttons or by connecting to its console. Each vendor has a different set of configs and different methods to set them.
Configure NTP client
Let’s assume we already have NTP appliance name
ntpappliance1.xyz.com with IP
10.10.1.2 in our infra. Now we will see step by step configuring Linux server to sync time with this appliance over the network.
1. Make sure you have
ntp package installed. If not install it using steps defined here.
# rpm -q ntp
/etc/ntp.conf file to add appliance or NTP server name into it. Add IP/hostname in the end of the file. If you are supplying hostname here then make a sure relevant entry is present in /etc/hosts file. I have shown both entries in the below example IP and hostname.
# cat /etc/ntp.conf
------- output clipped ------
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
server ntpappliance1.xyz.com prefer
In the above output, 3-time servers are defined and the one with
prefer option will always have a preference while syncing time. If it’s not reachable then any one of the remaining servers is chosen by the daemon to sync time with.
3. That’s it! Start your
ntp daemon and make sure it’s running.
# /etc/init.d/ntpd start
# service ntpd start
# service ntpd status
ntpd (pid 2261) is running...
4. Check time sync status using command :
# ntpq -p
remote refid st t when poll reach delay offset jitter
+ntpappliance2.xyz.com 10.10.1.3 3 u 40 64 377 180.764 0.719 0.458
*ntpappliance1.xyz.com 10.10.1.2 3 u 50 64 377 180.851 -0.272 0.149
Here different fields are :
remote: Remote time server hostname/IP
refid: Association ID
t: u: unicast, b: broadcast, l: local
when: sec/min/hr since last received packet
poll: poll interval (log2 s)
reach: reach shift register (octal)
delay: roundtrip delay
offset: offset from server time
jitter: Jitter (noise)
Also, the very first value displayed is state i.e.
* sign. These values can be :
+mean Good connectivity and preferred remote server
*means currently selected time server for sync
-means do not use to sync due to out of tolerance (cluster algorithm)
xmeans do not use to sync due to out of tolerance (intersection algorithm)
#means good connectivity but not used to sync yet.
Manually sync time with the server
NTP daemon runs in background and sync time according to polling configuration. But if you want to manually sync time right away with time server then you can do it with below command :
# ntpdate -u ntpappliance2.xyz.com
10 Dec 13:20:05 ntpdate: adjust time server 10.10.1.3 offset -0.000437 sec
It will update time with a given time server (as an argument in command) right away.
If you are having issues with timeserver connectivity then first troubleshoot at OS and firewall level. You can also view your syslog and grep for the NTP keyword and you will see all NTP related messages logged in Syslog which may help you in troubleshooting.