How to use the command 'ntpdate' (with examples)
- Linux
- December 25, 2023
The ’ntpdate’ command is used to synchronize and set the date and time via NTP (Network Time Protocol). It allows you to query and synchronize time with an NTP server, ensuring accurate timekeeping on your system. In this article, we will explore different use cases of the ’ntpdate’ command.
Use case 1: Synchronize and set date and time
Code:
sudo ntpdate host
Motivation: In a scenario where the system clock is not synchronized with an NTP server, this command can be used to fetch the accurate time and set it on your system.
Explanation:
sudo
: This command is typically run with root privileges in order to set the system time.ntpdate
: The command itself, used to synchronize and set the system time.host
: The hostname or IP address of the NTP server you want to synchronize with.
Example output:
29 Dec 14:30:42 ntpdate[2385]: adjust time server 203.0.113.1 offset 0.010317 sec
Use case 2: Query the host without setting the time
Code:
ntpdate -q host
Motivation: Sometimes, you may want to check the time offset between your system and the NTP server without actually modifying the system time. This command allows you to query the NTP server for time information without making any changes.
Explanation:
ntpdate
: The command itself, used to query the NTP server without setting the time.-q
: The option to perform a query without modifying the system time.host
: The hostname or IP address of the NTP server you want to query.
Example output:
server 203.0.113.1, stratum 3, offset -0.010317, delay 0.02542
Use case 3: Use an unprivileged port in case a firewall is blocking privileged ports
Code:
sudo ntpdate -u host
Motivation: In some situations, your system’s firewall may be blocking privileged ports (ports below 1024). By using the -u
option, you can instruct ’ntpdate’ to use an unprivileged port (port higher than 1024) for communication with the NTP server, bypassing the firewall restriction.
Explanation:
sudo
: This command is typically run with root privileges in order to set the system time.ntpdate
: The command itself, used to synchronize and set the system time.-u
: The option to use an unprivileged port for communication with the NTP server.host
: The hostname or IP address of the NTP server you want to synchronize with.
Example output:
29 Dec 14:30:42 ntpdate[2385]: adjust time server 203.0.113.1 offset 0.010317 sec
Use case 4: Force time to be stepped using ‘settimeofday’ instead of ‘slewed’
Code:
sudo ntpdate -b host
Motivation: By default, ’ntpdate’ will adjust the system time smoothly by slewing, which means gradually changing the time to reach the accurate time. However, in some scenarios, you may want to forcefully step the system time, making an abrupt change. This can be achieved using the -b
option.
Explanation:
sudo
: This command is typically run with root privileges in order to set the system time.ntpdate
: The command itself, used to synchronize and set the system time.-b
: The option to force the system time to step using ‘settimeofday’ instead of slewing.host
: The hostname or IP address of the NTP server you want to synchronize with.
Example output:
29 Dec 14:30:42 ntpdate[2385]: adjust time server 203.0.113.1 offset 0.010317 sec
Conclusion:
The ’ntpdate’ command is a powerful tool for synchronizing and setting the date and time on your system using NTP. By exploring the different use cases provided in this article, you can make the most out of this command in various scenarios. Whether you need to synchronize the time, query an NTP server, bypass a firewall restriction, or force a time step, ’ntpdate’ has got you covered. Keep in mind the different options available and choose the one that suits your specific requirements.