Ntpdate is a console utility that can be used as an NTP client in various Linux distros to synchronize the local time of a host with the global time on an NTP server.
Installing ntpdate Utility in Linux
The ntpdate command is not installed by default in most Linux distributions. To install it, run the command below:
#apt-get install ntpdate
#yum install ntpdate
On Fedora-like distors:
#dnf install ntpdate
You can check the local host time with the NTP server with the command:
#/usr/sbin/ntpdate -q 0.de.pool.ntp.org
The command will return something like this:
server x.x.x.x, stratum 2, offset 54.126055, delay 0.03915
server x.x.x.x, stratum 2, offset 54.129242, delay 0.02861
server x.x.x.x, stratum 2, offset 54.126370, delay 0.02370
server x.x.x.x, stratum 2, offset 54.126370, delay 0.02185
10 Jul 12:00:01 ntpdate: adjust time server x.x.x.x offset 54.126370 sec
Offset is the time difference with specified NTP server in seconds. In our example, time on the current Linux host is 54 seconds ahead than on the reference NTP servers.
Stop the ntp service:
#service ntp stop
To synchronize time on a host with an NTP server once, run the following command:
#/usr/sbin/ntpdate –bs 0.de.pool.ntp.org
10 Jul 12:00:41 ntpdate: adjust time server [126.96.36.199] offset 54.231123 sec
For time synchronization, it is advisable to use the closest available NTP server from your region. A list of public NTP server pools for different regions is available here.
The ntpdate utility performed synchronization, as a result, 54 seconds were added to the local system time.
Now you can start the NTP service:
#service ntp start
To synchronize the time automatically once a day, you can create a scheduled task:
#crontab -e #00 1 * * * /usr/sbin/ntpdate 0.ca.pool.ntp.org
To automatically synchronize on Linux boot, use the command:
#crontab -e #@reboot /usr/sbin/ntpdate 0.ca.pool.ntp.org
Ntpdate Does Not Synchronize Time
If after the time synchronization with the NTP server command (for example, #ntpdate 0.ca.pool.ntp.org), you are getting the error:
11 Jul 16:05:44 ntpdate: no server suitable for synchronization found
This means that the NTP server is unavailable (although you can access Internet from the host). It is possible that UDP port 123 is blocked (check if the incoming and outgoing traffic for port UDP/123 is allowed on your Internet access gateway). In this case, you can try to synchronize the time with the help of the unprivileged port (–u key):
#ntpdate -u 0.ca.pool.ntp.org
11 Jul 16:20:14 ntpdate: step time server [188.8.131.52] offset 14.195031 sec
Another common ntpdate error looks like this:
11 Jul 16:41:02 ntpdate: the NTP socket is in use, exiting
This error means that UDP port 123 is busy by another program. As a rule, it is used by the ntp daemon.
You can stop the ntp service in Linux like this:
service ntpd stop
Now you can synchronize time using ntpdate.