Cyberithub

10 Simple and Useful ntpdate command examples in Linux(RHEL/CentOS 7/8)

Advertisements

In this article, we will look into 10 Simple and Useful ntpdate command examples in Linux(RHEL/CentOS 7/8). If you are following up on my articles then last time we have seen the Steps to install and configure NTP Server on Linux so in this article we will look into the usages of all the utilities used with the NTP Server. ntpdate is a free and open source utility used in Linux Based Servers to synchronize time with the NTP Server.

There are other ntp utilities like ntpq, ntpstat which are used along with ntpdate to check and synchronizes the local server time with the NTP Server. Here we will keep our focus on ntpdate command and will see the usage of this command with examples.

SYNOPSIS

ntpdate [ -46bBdqsuv ] [ -a key ] [ -e authdelay ] [ -k keyfile ] [ -o version ] [ -p samples ] [ -t timeout ] [ -U user_name ] server [ ... ]

10 Simple and Useful ntpdate command examples in Linux(RHEL/CentOS 7/8)

ntpdate command examples in Linux(RHEL/CentOS 7/8)

Also Read: 10 Practical and Useful ntpq command examples in Linux(RHEL/CentOS 7/8)

Example 1: How to Check If Client machine is sync with NTP Server

You can simply run ntpdate command in your Linux Command Line interface and check whether your client machine is getting synced with any of the NTP Server. As you can see from the output as of now there is no NTP Server configured.

[root@localhost ~]# ntpdate
9 Jan 15:17:28 ntpdate[2083]: no servers can be used, exiting

NOTE:

Please note that here I am using root user to run all the below commands. You can use any user with sudo access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to the User.

Example 2: How to Test Connection to a NTP Server

If you want to test connection to a NTP Server then you need to use -q option with ntpdate command as shown below. In this example we are testing connection with pool.ntp.org NTP Server using ntpdate -q pool.ntp.org.

[root@localhost ~]# ntpdate -q pool.ntp.org
server 162.159.200.1, stratum 3, offset -0.000668, delay 0.06847
server 162.159.200.123, stratum 3, offset -0.000199, delay 0.06737
server 95.216.144.226, stratum 2, offset -0.000413, delay 0.17609
server 157.119.108.165, stratum 4, offset -0.007028, delay 0.04279
9 Jan 15:20:01 ntpdate[2100]: adjust time server 95.216.144.226 offset -0.000413 sec

-q : Query only - don't set the clock. More on ntpdate command Man Page.

stratum : It denotes the level in timing hierarchy. Stratum 1 denotes the first level which synchronize its time with external NTP Server. Stratum 2 denotes the second level which synchronizes its time with Stratum 1 servers and so on.

offset : It is the difference in time between reference or configured NTP Server and the local server. Greater value indicates lesser synchronization with the source.

delay : It denotes the round trip time or latency with the NTP Server.

Example 3: How to divert Connection output to the System Logs

If you are ever running ntpdate command in cron scripts or in any other bash script then you can always use -s option to divert the command output to Syslog.

[root@localhost ~]# ntpdate -s pool.ntp.org

-s : Divert logging output from the standard output (default) to the system syslog facility.

Example 4: How to Synchronize Connection to a NTP Server

If you want to synchronize your local server with NTP Server then you need to simply use ntpdate <ntp_server_ip> command. In this example we are synchronizing our time with pool.ntp.org NTP Server using ntpdate pool.ntp.org command as shown below.

[root@localhost ~]# ntpdate pool.ntp.org
9 Jan 15:34:18 ntpdate[2260]: adjust time server 103.134.252.11 offset 0.009016 sec

Example 5: How to Synchronize with Hosts beyond the Firewall

If you want to synchronize your time with some hosts lying behind a firewall then you need to use -u option to always use the unprivileged ports. In this example we are trying to sync time with pool.ntp.org using ntpdate -u pool.ntp.org command as shown below.

[root@localhost ~]# ntpdate -u pool.ntp.org
9 Jan 15:56:16 ntpdate[2498]: adjust time server 103.134.252.11 offset 0.018811 sec

-u : Direct ntpdate to use an unprivileged port for outgoing packets.

Example 6: How to Enable Debugging Mode

To enable debugging mode you need to use -d option with ntpdate command as shown in below example. In this example we are trying to debug the time sync with NTP Server pool.ntp.org using ntpdate -d pool.ntp.org command as shown below.

[root@localhost ~]# ntpdate -d pool.ntp.org
9 Jan 15:58:41 ntpdate[2532]: ntpdate 4.2.6p5@1.2349-o Tue Jun 23 15:38:19 UTC 2020 (1)
Looking for host pool.ntp.org and service ntp
host found : mail.deva-ayurveda.eu
transmit(5.189.141.35)
receive(5.189.141.35)
transmit(162.159.200.1)
receive(162.159.200.1)
transmit(162.159.200.123)
receive(162.159.200.123)
transmit(95.216.144.226)
receive(95.216.144.226)
transmit(5.189.141.35)
receive(5.189.141.35)
transmit(162.159.200.1)
receive(162.159.200.1)
transmit(162.159.200.123)
receive(162.159.200.123)
transmit(95.216.144.226)
receive(95.216.144.226)
transmit(5.189.141.35)
receive(5.189.141.35)
transmit(162.159.200.1)
receive(162.159.200.1)
transmit(162.159.200.123)
server 5.189.141.35, port 123
stratum 2, precision -25, leap 00, trust 000
refid [5.189.141.35], delay 0.16208, dispersion 0.01099
transmitted 4, in filter 4
reference time: e3a49704.9b529d42 Sat, Jan 9 2021 15:56:36.606
originate timestamp: e3a49787.7ac51f62 Sat, Jan 9 2021 15:58:47.479
transmit timestamp: e3a49787.6d5b4212 Sat, Jan 9 2021 15:58:47.427
filter delay: 0.21945 0.16505 0.21362 0.16208
0.00000 0.00000 0.00000 0.00000
filter offset: -0.04609 -0.01673 -0.04312 -0.01589
0.000000 0.000000 0.000000 0.000000
delay 0.16208, dispersion 0.01099
offset -0.015895

server 162.159.200.1, port 123
stratum 3, precision -24, leap 00, trust 000
refid [162.159.200.1], delay 0.07127, dispersion 0.00047
transmitted 4, in filter 4
reference time: e3a4974d.f62f9b41 Sat, Jan 9 2021 15:57:49.961
originate timestamp: e3a49787.a1d5dede Sat, Jan 9 2021 15:58:47.632
transmit timestamp: e3a49787.a0718dee Sat, Jan 9 2021 15:58:47.626
filter delay: 0.07127 0.07179 0.07469 0.07323
0.00000 0.00000 0.00000 0.00000
filter offset: -0.01762 -0.01782 -0.01921 -0.01839
0.000000 0.000000 0.000000 0.000000
delay 0.07127, dispersion 0.00047
offset -0.017625

9 Jan 15:58:48 ntpdate[2532]: adjust time server 5.189.141.35 offset -0.015895 sec

-d : Enable the debugging mode. More on ntpdate command Man Page.

Example 7: How to Use Verbose Mode with ntpdate command in Linux

To enable the verbose mode with ntpdate command you need to use -v option as shown below. This option will make all ntpdate tasks visible on console output.

[root@localhost ~]# ntpdate -v pool.ntp.org
9 Jan 16:03:03 ntpdate[2613]: ntpdate 4.2.6p5@1.2349-o Tue Jun 23 15:38:19 UTC 2020 (1)
9 Jan 16:03:11 ntpdate[2613]: adjust time server 5.103.139.163 offset 0.024725 sec

-v : Be verbose. More on ntpdate command Man Page.

Example 8: How to Force Step up the time rather than adjust Using ntpdate command

If you want to stepped up the time sync then you need to use -b option as shown below. In this example we have stepped the time sync with pool.ntp.org Server by using ntpdate -vb pool.ntp.org command.

[root@localhost ~]# ntpdate -vb pool.ntp.org
9 Jan 16:05:23 ntpdate[2649]: ntpdate 4.2.6p5@1.2349-o Tue Jun 23 15:38:19 UTC 2020 (1)
9 Jan 16:05:30 ntpdate[2649]: step time server 5.103.139.163 offset 0.019299 sec

-b : Force the time to be stepped using the settimeofday() system call, rather than slewed (default) using the adjtime() system call.

Example 9: How to Force the Time Sync to always be slewed

If you want sync to always be slewed then you need to use -B option as shown below. In this example we have slewed the time sync with pool.ntp.org Server using ntpdate -vB pool.ntp.org command.

[root@localhost ~]# ntpdate -vB pool.ntp.org
9 Jan 16:05:35 ntpdate[2650]: ntpdate 4.2.6p5@1.2349-o Tue Jun 23 15:38:19 UTC 2020 (1)
9 Jan 16:05:42 ntpdate[2650]: adjust time server 95.216.144.226 offset -0.015836 sec

-B: Force the time to always be slewed using the adjtime() system call, even if the measured offset is greater than +-500 ms.

Example 10: How to Check Man Page of ntpdate command in Linux

If you want to check the man page of ntpdate command then you need to use man ntpdate command as shown below.

[root@localhost ~]# man ntpdate
ntpdate(8) System Manager's Manual ntpdate(8)

NAME
ntpdate - set the date and time via NTP

Disclaimer: The functionality of this program is now available in the ntpd program. See the -q command line option in the ntpd - Network Time Protocol (NTP)
daemon page. After a suitable period of mourning, the ntpdate program is to be retired from this distribution

SYNOPSIS
ntpdate [ -46bBdqsuv ] [ -a key ] [ -e authdelay ] [ -k keyfile ] [ -o version ] [ -p samples ] [ -t timeout ] [ -U user_name ] server [ ... ]

DESCRIPTION
ntpdate sets the local date and time by polling the Network Time Protocol (NTP) server(s) given as the server arguments to determine the correct time. It
must be run as root on the local host. A number of samples are obtained from each of the servers specified and a subset of the NTP clock filter and selec‐
tion algorithms are applied to select the best of these. Note that the accuracy and reliability of ntpdate depends on the number of servers, the number of
polls each time it is run and the interval between runs.

ntpdate can be run manually as necessary to set the host clock, or it can be run from the host startup script to set the clock at boot time. This is useful
in some cases to set the clock initially before starting the NTP daemon ntpd. It is also possible to run ntpdate from a cron script. However, it is impor‐
tant to note that ntpdate with contrived cron scripts is no substitute for the NTP daemon, which uses sophisticated algorithms to maximize accuracy and
reliability while minimizing resource use. Finally, since ntpdate does not discipline the host clock frequency as does ntpd, the accuracy using ntpdate is
limited.

 

Leave a Comment