How to use the command 'ntpdate' (with examples)
- Linux
- December 17, 2024
The ntpdate
command is a utility for setting and synchronizing the date and time of a computer system to match that of a reliable NTP (Network Time Protocol) server. By ensuring the system clock is accurate, ntpdate
helps maintain system logs, schedule tasks, and ensure time-sensitive operations run effectively. Below are several use cases demonstrating how to effectively utilize the ntpdate
command.
Use case 1: Synchronize and set the date and time
Code:
sudo ntpdate host
Motivation:
Synchronizing a computer’s internal clock is crucial in maintaining synchronization across server clusters, databases, and logging services. When systems operate on different times, anomalies may occur, leading to data corruption or erroneous log entries. Using sudo ntpdate host
, an administrator can ensure a computer’s clock adheres to a globally accepted time standard provided by a specific ‘host’ or NTP server.
Explanation:
sudo
: This command is executed with elevated (admin) privileges to allow changes to the system clock, which is a protected action.ntpdate
: This command invokes the utility responsible for modifying the system date and time.host
: This argument specifies the NTP server address or hostname from which the time will be synchronized.
Example output:
12 Oct 13:23:45 ntpdate[1234]: step time server 192.168.1.1 offset 0.357623 sec
This output indicates that the system time has been adjusted by approximately 0.36 seconds to be in line with the time from server ‘192.168.1.1’.
Use case 2: Query the host without setting the time
Code:
ntpdate -q host
Motivation:
Sometimes, before making any adjustments, system administrators might want to check how much a system’s clock deviates from an authoritative source. This approach helps in assessing whether the system’s internal clock is running accurately. In such cases, querying the ntpdate
server using the -q
option can help ascertain the time difference without affecting the system clock.
Explanation:
ntpdate
: This utility is invoked to query time servers.-q
: Short for query-only, this argument instructsntpdate
to request the current time from the server without adjusting the system time.host
: Designates the NTP server to gather time information from.
Example output:
server 192.168.1.1, stratum 2, offset 0.357623, delay 0.02510
12 Oct 13:25:45 ntpdate[1234]: poll interval 16 s
The time fetched from the server indicates a small offset from the local time, yet no changes to the system clock are performed.
Use case 3: Use an unprivileged port if a firewall is blocking privileged ports
Code:
sudo ntpdate -u host
Motivation:
In networked environments where firewalls enforce strict rules about which ports are accessible, utilising unprivileged ports for time synchronization might be necessary. The default NTP service port (123) might be blocked, leading to connectivity issues. By using the -u
flag, users can ensure that ntpdate
communicates through non-standard ports that firewalls typically allow more freely.
Explanation:
sudo
: Required to grant permission for port number changes, which might otherwise be restricted.ntpdate
: The tool used for server time synchronization and potential adjustments.-u
: This flag instructsntpdate
to use an unprivileged port, thus bypassing potential firewall-imposed restrictions.host
: Identifies the target NTP server to request or set the desired time.
Example output:
12 Oct 13:26:00 ntpdate[1234]: adjust time server 192.168.1.1 offset 0.357623 sec
The output signifies successful time adjustment and demonstrates that the ntpdate
command has utilized an alternative, unblocked port.
Use case 4: Force time to be stepped using settimeofday
instead of slewed
Code:
sudo ntpdate -b host
Motivation:
Under certain conditions, it might be necessary to enforce an instantaneous time adjustment rather than gradual syncing. When time discrepancies are large, using the -b
flag permits the clock to be stepped — meaning changed all at once — instead of being slewed slowly over a period of time, providing immediate time accuracy.
Explanation:
sudo
: Grants root privilege, required for modifying the system’s date and time settings.ntpdate
: Calls upon the NTP utility to engage with external time sources.-b
: This option commands the utility to adjust time by stepping instead of slewing, facilitating an immediate jump in time settings.host
: Supplies the chosen NTP server for acquiring the accurate time.
Example output:
12 Oct 13:26:40 ntpdate[1234]: set time server 192.168.1.1 offset 5.678912 sec
The output communicates a successful and abrupt time adjustment, showing adjustment by a substantial difference of nearly 5.68 seconds.
Conclusion:
ntpdate
is a valuable tool for maintaining time coherence and accuracy across systems and networks, with several command-line options available to account for diverse network configurations and requirements. By understanding these options, system administrators can ensure their infrastructure reliably reflects the correct time, which is fundamental for synchronized operations.