Learn how to configure NTP (Network Time Protocol) on Linux machines to sync time with ntp server over network. Also learn how to manually sync time on ntp.
Now a days NTP (Network Time Protocol) is one of the essential thing in any IT infrastructure. Apart from production even development or test environments also backed with NTP to ensure smooth operations. Lets see what is NTP and how to configure it in Linux machine.
What is NTP
NTP is protocol used to sync time of machines with NTP server (can be appliance or another Linux machine) over network. It aims at keeping all 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 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 rack mount server looking device which has a antenna attached to it. Antenna can be extended to building rooftops to ensure better signal receiving. These appliances receives 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 network. Appliance comes with its own configuration which can be done on front display buttons or by connecting to its console. Each vendor has different set of configs and different methods to set them.
Configure NTP client
Lets assume we already have a NTP appliance names 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 network.
1. Make sure you have ntp package installed. If not install it with ‘rpm install ntp‘
# rpm -q ntp
2. Edit /etc/ntp.conf file to add appliance or ntp server name into it. Add IP/hostname in the end of file. If you are supplying hostname here then make sure relevant entry is present in /etc/hosts file. I have shown both entries in 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 above output, 3 time servers are defined and the one with prefer option will always have preference while syncing time. If its not reachable then any one of remaining server is chose by daemon to sync time with.
3. Thats it! Start your ntp daemon and make sure its running.
# /etc/init.d/ntpd start
# service ntpd start
# service ntpd status
ntpd (pid 2261) is running...
4. Check time sync status using below 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
- st : stratum
- 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, very first value displayed is state i.e. + and * 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)
- x means 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 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 give time server (as a 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 ntp keyword and you will see all ntp related messages logged in syslog which may help you in troubleshooting.