ntpdate Command in Linux



The ntpdate command in Linux is used to manually set the date and time of your system by synchronizing it with an NTP server. It sets the local date and time by polling the specified Network Time Protocol (NTP) servers to determine the correct time. Read this tutorial to get a good understanding of the Linux ntpdate command and how to apply it in practice.

The ntpdate command obtains multiple samples from each specified server and applies a subset of NTP clock filter and selection algorithms to choose the best time. The accuracy and reliability of ntpdate depend on the number of servers polled, the number of samples taken, and the polling intervals.

Table of Contents

Here is a comprehensive guide to the options available with the ntpdate command −

Introduction of ntpdate Command

ntpdate can be included in the host's startup script to set the clock at boot time, which is useful before starting the NTP daemon (ntpd). Besides that, it can be scheduled via cron jobs, but it does not replace the NTP daemon's sophisticated algorithms that maximize accuracy and reliability while minimizing resource use. Running ntpdate regularly (e.g., hourly) can maintain reasonably accurate time without stepping the clock.

If the clock error exceeds 0.5 seconds, ntpdate steps the time using the settimeofday routine. For errors under 0.5 seconds, it slews the time using the adjtime routine, which is less disruptive and more accurate.

In addition, ntpdate will not set the date if an NTP server daemon (ntpd) is running on the same host. Unlike ntpd, ntpdate does not discipline the host clock frequency, limiting its accuracy. If compiled with NetInfo support, the server argument is optional if ntpdate can find a time server in the NetInfo configuration for ntpd.

Syntax of ntpdate Command

The general syntax for the ntpdate command is as follows −

ntpdate [options] server [ ... ]

ntpdate Command Options

The following table provides a summary of the different command line options available for the ntpdate command −

Tag Description
-4 Force DNS resolution to IPv4.
-6 Force DNS resolution to IPv6.
-a key Enable authentication and specify the key identifier.
-B Force time to be slewed using adjtime() even if offset > ±500 ms.
-b Force time to be stepped using settimeofday().
-d Enable debugging mode.
-e authdelay Specify the processing delay for authentication.
-k keyfile Specify the path for the authentication key file.
-o version Specify the NTP version for outgoing packets.
-p samples Specify the number of samples to acquire from each server (1-8).
-q Query only don't set the clock.
-s Divert logging output to the system syslog.
-t timeout Specify the maximum time waiting for a server response.
-u Use an unprivileged port for outgoing packets.
-v Enable verbose mode.
-U user_name Drop root privileges and change user ID to user_name.

Examples of ntpdate Command in Linux

The following examples can help you understand how to use the ntpdate command effectively −

Synchronize the System Clock with a Specific NTP Server

To synchronize your system clock with a specific NTP server, you can use the following command −

sudo ntpdate ntp.ubuntu.com

This command synchronizes the system clock with the NTP server ntp.ubuntu.com.

ntpdate Command in Linux1

Query an NTP Server without Setting the Date and Time

To query an NTP server without setting the date and time, use the "-q" flag −

sudo ntpdate -q ntp.ubuntu.com

This command queries the NTP server and displays the time differences without actually setting the system clock.

ntpdate Command in Linux2

Use an Unprivileged Port for Outgoing Packets

To use an unprivileged port for outgoing packets, you can simply run −

sudo ntpdate -u ntp.ubuntu.com

This command uses an unprivileged port for outgoing packets, useful when behind a firewall blocking privileged ports.

ntpdate Command in Linux3

Log the Result to the System Log Instead of Standard Output

To log the result to the system log instead of standard output, use the "-s" flag −

sudo ntpdate -s ntp.ubuntu.com

This command logs the synchronization result to the system log for later review.

ntpdate Command in Linux4

Force DNS Resolution to the IPv4 Namespace

To force DNS resolution to the IPv4 namespace, you can use the following command −

sudo ntpdate -4 ntp.ubuntu.com

This command ensures that the host names are resolved using IPv4 addresses.

ntpdate Command in Linux5

Enable Debugging Mode

To enable debugging mode, you can use the following command −

sudo ntpdate -d ntp.ubuntu.com

This command enables debugging mode, which goes through all steps without adjusting the local clock and prints useful debugging information.

ntpdate Command in Linux6

Force the Time to Be Stepped Using settimeofday

To force the time to be stepped using settimeofday, you can simply run −

sudo ntpdate -b ntp.ubuntu.com

This command forces the time to be stepped if the offset is greater than 0.5 seconds, using the settimeofday system call.

ntpdate Command in Linux7

Conclusion

The ntpdate command is an essential tool for synchronizing your system's clock with an NTP server. It offers flexibility through various options, allowing users to tailor synchronization to their needs. While ntpdate is efficient for manual synchronization, it is not a replacement for the more robust ntpd daemon, which ensures continuous time discipline.

Understanding how and when to use ntpdate can help maintain accurate system time, which is crucial for various network-related tasks and applications.

Advertisements