Syncing time is crucial for maintaining the integrity of your system, as it affects various aspects, such as security, authentication, and the proper functioning of applications and services. By keeping your system’s clock accurate, you minimize the risk of errors, discrepancies, and potential vulnerabilities.
As advanced as computer systems are, they are not perfect. They even get the time wrong! The computer time will sometimes show +/- 5 minutes or even more. What’s going on!?
Modern systems that use systemd already have a background daemon that synchronizes the system time through the Network Time Protocol (NTP). You can confirm whether the time synchronization is enabled by running the timedatectl command.
In these cases, if you see System clock synchronized: yes and NTP service: active, then you’re all set.
Note. RHEL-based distros have deprecated ntpdate in favor of chrony since the EL8 release.
Table of Contents
Why Use NTPDATE?
NTPDATE is a command-line tool that allows you to initiate time synchronization with an NTP server manually. It retrieves the current time from a specified NTP server and adjusts the local system clock accordingly.
While it is likely that your Linux systems have time sync already running through chrony, there are situations when you need NTPDATE.
- On non-systemd systems, you can use ntpdate to sync time in Linux.
- You have monitoring tools that do not support chrony and may be compatible with ntpdate.
- You require ntpdate features that are not available in chrony.
Install NTPDATE
The ntpdate command is not installed by default but is available in the default repositories. To install, run this command.
sudo apt install -y ntpdate
Once installed, you’ll have access to the ntpdate command. The ntpdate command supports various parameters or options that you can use to customize its behavior. Here are some commonly used options:
- b or -step: This option enables the “stepping” mode, which adjusts the time in one large step instead of gradually adjusting it. It can be useful when the time difference between the local clock and the NTP server is significant.
- q or -query: This option performs a one-time query of the NTP server’s time without adjusting the local system clock. It can be used for testing or checking the time synchronization status.
- u or -udp: This option forces NTPDATE to use the UDP transport protocol for communicating with the NTP server. By default, NTPDATE uses the TCP protocol for communication.
- v or -verbose: This option increases the verbosity level of the output, providing more detailed information about the time synchronization process.
- p <stratum> or -priority=<stratum>: This option sets the priority of the local clock. It is used when multiple NTP servers are specified and the local clock’s priority needs to be adjusted.
- t <timeout> or -timeout=<timeout>: This option sets the maximum time, in seconds, to wait for a response from the NTP server before timing out.
These are just a few examples of the options available in NTPDATE. To see the complete list of options supported by the version of NTPDATE installed on your system, you can refer to the command’s manual page by running man ntpdate in a terminal.
Related article. How to configure NTP time sync using Group Policy.
NTPDATE Usage Examples
Here are several examples showcasing the usage of the ntpdate command with different options:
Perform a one-time time query without adjusting the system clock
sudo ntpdate -q pool.ntp.org
The -q option performs a time query without modifying the system clock. In this example, the ntpdate command queries the pool.ntp.org server and displays the response, which includes the time information received from the server.
Offset is the time difference with a specified NTP server in seconds. In this example, time on the current Linux host is +0.023182 seconds ahead of that on the reference NTP servers.
Adjust the system clock in a single step
sudo ntpdate -b time.nist.gov
The -b option enables the “stepping” mode, which adjusts the system clock in one large step. In this example, the ntpdate command synchronizes the system clock with the time.nist.gov server using a single-step adjustment.
Increase the verbosity level for more detailed output:
sudo ntpdate -v pool.ntp.org
The -v option increases the verbosity level of the output, providing more detailed information about the time synchronization process. In this example, the ntpdate command queries the pool.ntp.org server and displays verbose output, including details about the current time, adjustment steps, and synchronization status.
Specify a custom timeout value
sudo ntpdate -t 5 pool.ntp.org
The -t option is used to specify a custom timeout value, which is the maximum time to wait for a response from the NTP server before timing out. In this example, the ntpdate command sets the timeout to 5 seconds when querying the pool.ntp.org server.
Specify the number of samples to acquire from each NTP server
sudo sudo ntpdate -q -v -p 3 time.cloudflare.com time.windows.com time.apple.com
The -p option allows you to specify the number of samples to be acquired from each NTP server. In this example, the ntpdate command acquires three samples from time.cloudflare.com, time.windows.com, and time.apple.com.
Force the use of unprivileged ports for outgoing packets
sudo ntpdate -u pool.ntp.org
The -u option in NTPDATE forces the utility to use unprivileged ports (ports higher than 1023) for outgoing packets. In this example, the ntpdate command uses unprivileged ports for communication with the pool.ntp.org server:
Remember to replace pool.ntp.org, time.nist.gov, ntp1.example.com, ntp2.example.com, or any other server names in the examples with the actual NTP server(s) you wish to use for time synchronization.
Schedule NTPUPDATE to Sync the Time
You can create schedules to run the ntpdate command to sync time automatically. First, run this command to open the crontab file in the text editor.
crontab -e
Append one of these example cron jobs (or both) to the end of the crontab file, depending on the schedule you want to implement.
# Run NTPDATE daily at 1:00 AM
00 1 * * * /usr/sbin/ntpdate 0.ca.pool.ntp.org
# Run NTPDATE after a reboot
@reboot /usr/sbin/ntpdate 0.ca.pool.ntp.org
Save the crontab and exit the editor.
NTPDATE Does Not Synchronize the Time
If the time synchronization fails with the NTP server and you are getting this error:
Ensure that port 123 is allowed in your firewall.
Another common ntpdate error looks like this: the NTP socket is in use, exiting.
This error means that UDP port 123 is busy with another program. By default, it is used by the NTP daemon.
As a workaround, specify the -u option so that ntpdate uses an unprivileged port instead of port 123.
You can also stop and disable the NTP daemon if you see fit:
sudo service ntp stop
sudo service ntp disable
Now you can synchronize time using ntpdate using port 123 by default.
Conclusion
NTPDATE is a powerful tool that empowers Ubuntu Linux users to sync their system’s clock perfectly. Incorporating it into your system administration routine can enhance security, improve performance, and prevent time-related issues.
So, leverage the capabilities of NTPDATE to maintain accurate time synchronization in your Ubuntu Linux environment.