In this article, I’ll explain how we may use ntpdate and ntptrace commands while working o debugging or troubleshooting NTP related issues. We already showed how to install and configure NTP in Linux Redhat/Centos and Ubuntu/Debian.
What is a ntpupdate command in Linux?
The ntpdate command is used to manually sync time with an NTP server when you don’t have NTP agent running.
Note: ntpdate will only sync to the time server if the ntpd daemon is not running.
Install ntpupdate command in Linux
This package is installed on the system, but if it isn’t we can easily install it via yum as shown below:
[root@linuxnix ~]# yum install ntpdate -y Loaded plugins: fastestmirror, presto Setting up Install Process Determining fastest mirrors * base: mirror.nus.edu.sg * extras: mirror.nus.edu.sg * updates: mirror.nus.edu.sg base | 3.7 kB 00:00 centos-sclo-rh | 2.9 kB 00:00 centos-sclo-rh/primary_db | 2.6 MB 00:04 extras | 3.4 kB 00:00 extras/primary_db | 29 kB 00:00 updates | 3.4 kB 00:00 updates/primary_db | 4.7 MB 00:00 Resolving Dependencies --> Running transaction check ---> Package ntpdate.x86_64 0:4.2.6p5-10.el6.centos.2 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================================================================== Package Arch Version Repository Size ====================================================================================================================================== Installing: ntpdate x86_64 4.2.6p5-10.el6.centos.2 base 78 k Transaction Summary ====================================================================================================================================== Install 1 Package(s) Total download size: 78 k Installed size: 123 k Downloading Packages: Setting up and reading Presto delta metadata Processing delta metadata Package(s) data still to download: 78 k ntpdate-4.2.6p5-10.el6.centos.2.x86_64.rpm | 78 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : ntpdate-4.2.6p5-10.el6.centos.2.x86_64 1/1 Verifying : ntpdate-4.2.6p5-10.el6.centos.2.x86_64 1/1 Installed: ntpdate.x86_64 0:4.2.6p5-10.el6.centos.2 Complete!
Let’s query the application and verify the installation.
[root@linuxnix ~]# rpm -qi ntpdate Name : ntpdate Relocations: (not relocatable) Version : 4.2.6p5 Vendor: CentOS Release : 10.el6.centos.2 Build Date: Mon 06 Feb 2017 07:22:52 AM UTC Install Date: Thu 19 Oct 2017 04:29:48 PM UTC Build Host: c1bm.rdu2.centos.org Group : Applications/System Source RPM: ntp-4.2.6p5-10.el6.centos.2.src.rpm Size : 125487 License: (MIT and BSD and BSD with advertising) and GPLv2 Signature : RSA/SHA1, Mon 06 Feb 2017 11:25:05 AM UTC, Key ID 0946fca2c105b9de Packager : CentOS BuildSystem <http://bugs.centos.org> URL : http://www.ntp.org Summary : Utility to set the date and time via NTP Description : ntpdate is a program for retrieving the date and time from NTP servers.
Update system time with ntpdate command
[root@linuxnix ~]# ntpdate -uv 203.82.48.83 13 Oct 20:55:01 ntpdate[43081]: ntpdate 4.2.6p5@1.2349-o Wed May 11 20:04:33 UTC 2016 (1) 13 Oct 20:55:05 ntpdate[43081]: step time server 203.82.48.83 offset 0.614673 sec [root@linuxnix ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *dns1.synet.edu. 202.118.1.47 2 u 81 64 2 364.110 345.760 87.509 +send.mx.cdnetwo 204.123.2.5 2 u 89 64 16 342.758 315.714 197.871 -ns1.connect2b.n 203.82.48.83 3 u 21 64 17 674.288 639.773 410.967 +202.65.114.202 218.100.41.254 2 u 23 64 7 268.936 493.577 229.8.54
The –v option turns on verbose output and –u option uses an unprivileged port.
The unprivileged port is useful when the server is behind a firewall and traffic to privileged ports is blocked.
If you get the below message while trying to sync the system clock via ntpdate, then it means that the ntpd daemon is running and the server is syncing its clock through the ntpd daemon.
[root@linuxnix ~]# ntpdate -v 195.43.74.123 13 Oct 21:28:43 ntpdate[43343]: ntpdate 4.2.6p5@1.2349-o Mon Feb 6 07:22:46 UTC 2017 (1) 13 Oct 21:28:43 ntpdate[43343]: the NTP socket is in use, exiting
We could also use ntpdate in debug mode using the –d flag. In debug, mode ntpdate goes through the steps involved in syncing the system clock but does not go ahead with the sync. It’s a dry run of sorts.
[root@linuxnix ~]# ntpdate -d 195.43.74.123 13 Oct 22:09:16 ntpdate[2541]: ntpdate 4.2.6p5@1.2349-o Mon Feb 6 07:22:46 UTC 2017 (1) Looking for host 195.43.74.123 and service ntp host found : ntp.amnic.net transmit(195.43.74.123) transmit(195.43.74.123) receive(195.43.74.123) transmit(195.43.74.123) receive(195.43.74.123) 13 Oct 22:09:20 ntpdate[2541]: 195.43.74.123 rate limit response from server. server 195.43.74.123, port 123 stratum 1, precision -23, leap 00, trust 000 refid [GPS], delay 0.34071, dispersion 56.00000 transmitted 3, in filter 2 reference time: dd8b6ab4.dfb47e98 Fri, Oct 13 2017 22:09:16.873 originate timestamp: dd8b6ab7.cb9f09af Fri, Oct 13 2017 22:09:19.795 transmit timestamp: dd8b6ab7.d96515b2 Fri, Oct 13 2017 22:09:19.849 filter delay: 0.00000 0.34071 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 filter offset: 0.000000 0.103771 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 delay 0.34071, dispersion 56.00000 offset 0.103771
Troubleshooting tip: If you are observing a significant offset in ntpq –p output or connection timed out the error, then stop the ntpd service, sync the system clock manually with ntpdate and start the ntpd service again. This might resolve the problem if there are no significant underlying network problems.
After ntpdate next, we come across ntptrace. This is a Perl script which uses the ntpq utility in the background to display the primary time source from which the system clock is being synced. It is part of the ntp-perl package and is not installed on the system by default.
Install ntp-perl package using yum
[root@linuxnix ~]# yum install ntp-perl –y Loaded plugins: fastestmirror, presto Setting up Install Process Loading mirror speeds from cached hostfile * base: mirror.nus.edu.sg * extras: mirror.nus.edu.sg * updates: mirror.nus.edu.sg Resolving Dependencies --> Running transaction check ---> Package ntp-perl.x86_64 0:4.2.6p5-10.el6.centos.2 will be installed --> Processing Dependency: ntp = 4.2.6p5-10.el6.centos.2 for package: ntp-perl-4.2.6p5-10.el6.centos.2.x86_64 --> Running transaction check ---> Package ntp.x86_64 0:4.2.6p5-10.el6.centos.2 will be installed --> Finished Dependency Resolution Dependencies Resolved ====================================================================================================================================== Package Arch Version Repository Size ====================================================================================================================================== Installing: ntp-perl x86_64 4.2.6p5-10.el6.centos.2 base 30 k Installing for dependencies: ntp x86_64 4.2.6p5-10.el6.centos.2 base 599 k Transaction Summary ====================================================================================================================================== Install 2 Package(s) Total download size: 629 k Installed size: 1.6 M Downloading Packages: Setting up and reading Presto delta metadata Processing delta metadata Package(s) data still to download: 629 k (1/2): ntp-4.2.6p5-10.el6.centos.2.x86_64.rpm | 599 kB 00:00 (2/2): ntp-perl-4.2.6p5-10.el6.centos.2.x86_64.rpm | 30 kB 00:00 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 3.4 MB/s | 629 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : ntp-4.2.6p5-10.el6.centos.2.x86_64 1/2 Installing : ntp-perl-4.2.6p5-10.el6.centos.2.x86_64 2/2 Verifying : ntp-perl-4.2.6p5-10.el6.centos.2.x86_64 1/2 Verifying : ntp-4.2.6p5-10.el6.centos.2.x86_64 2/2 Installed: ntp-perl.x86_64 0:4.2.6p5-10.el6.centos.2 Dependency Installed: ntp.x86_64 0:4.2.6p5-10.el6.centos.2 Complete!
Let’s query the rpm to retrieve some information about it.
root@linuxnix ~]# rpm -qi ntp-perl Name : ntp-perl Relocations: (not relocatable) Version : 4.2.6p5 Vendor: CentOS Release : 10.el6.centos.2 Build Date: Mon 06 Feb 2017 12:52:52 PM IST Install Date: Fri 13 Oct 2017 09:13:43 PM IST Build Host: c1bm.rdu2.centos.org Group : Applications/System Source RPM: ntp-4.2.6p5-10.el6.centos.2.src.rpm Size : 4802 License: (MIT and BSD and BSD with advertising) and GPLv2 Signature : RSA/SHA1, Mon 06 Feb 2017 04:55:06 PM IST, Key ID 0946fca2c105b9de Packager : CentOS BuildSystem <http://bugs.centos.org> URL : http://www.ntp.org Summary : NTP utilities written in perl Description : This package contains perl scripts ntp-wait and ntptrace.
As we’ll observe in the following examples, the stratum value for localhost should be 2. If it’s 3 or 16, then it indicates a problem.
root@linuxnix ~]# ntptrace localhost: stratum 16, offset 0.000000, synch distance 0.000000 [root@linuxnix ~]# ntptrace localhost: stratum 3, offset 0.000000, synch distance 0.237577 150.95.148.140: timed out, nothing received ***Request timed out
The favorable output is similar to what we see below:
[root@linuxnix ~]# ntptrace localhost: stratum 2, offset 0.000000, synch distance 0.159431 ntp.nic.kz: stratum 1, offset 0.000000, synch distance 0.000000, refid 'PPS'
If you continue to get a request timed out now and then, it could indicate a problem with domain name resolution or general network connectivity with your NTP server.
Throughout this article, we’ve talked about ntpdate and ntptrace, but I’d also like so briefly discuss the options used with the ntpd daemon when it is running on the server.
To view the options for the currently running ntpd daemon, search for it in ps –ef output with grep.
[root@linuxnix ~]# ps -ef | grep ^ntp ntp 1885 1 0 19:40 ? 00:00:00 ntpd -u ntp:ntp -p /var/run/ntpd.pid –g
To view the default options with which ntpd will start, see the /etc/sysconfig /ntpd file
[root@linuxnix sysconfig]# cat ntpd # Drop root to id 'ntp:ntp' by default. OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
The options used are described below:
-u: The user and group with which ntpd should run provided the OS permit daemons to run with non-root privileges.
-p: The path of the ntpd pid file.
-g: If we do not use this option then ntpd will exit if it sees an offset of greater than 1000 s. This option allows the time to be set to any value without restriction; however, this can happen only once. If the threshold is exceeded after that, ntpd will exit with a message to the system log.
Other examples for different ntp based commands
- #ntpdate -u (to force synchronized with it’s NTP servers)
- #ntpq -p (will check either your NTP Server is synchronized or not.)
- #ntpdate -u “ntp server ip address” (will synchronize your client to NTP Server)
- #ntpdate pool.ntp.org (will synchronize the system clock with pool.ntp.org server)
- #ntpdate -s server1 [server2…] ( will direct it’s output to the system logging facility.)
- #ntpdate -sb server1 [server2…] (will reset the clock at boot time.)
I hope this article helps you the next time you need to debug an NTP related issue.
Sahil Suri
Latest posts by Sahil Suri (see all)
- Google Cloud basics: Activate Cloud Shell - May 19, 2021
- Create persistent swap partition on Azure Linux VM - May 18, 2021
- DNF, YUM and RPM package manager comparison - May 17, 2021
- Introduction to the aptitude package manager for Ubuntu - March 26, 2021
- zypper package management tool examples for managing packages on SUSE Linux - March 26, 2021