How to force a clock update using ntp?

I am running Ubuntu on an ARM based embedded system that lacks a battery backed RTC. The wake-up time is somewhere during 1970. Thus, I use the NTP service to update the time to the current time.

I added the following line to /etc/rc.local file:

sudo ntpdate -s time.nist.gov

However, after startup, it still takes a couple of minutes until the time is updated, during which period I cannot work effectively with tar and make.

How can I force a clock update at any given time?


UPDATE 1: The following (thanks to Eric and Stephan) works fine from command line, but fails to update the clock when put in /etc/rc.local:

$ date ; sudo service ntp stop ; sudo ntpdate -s time.nist.gov ; sudo service ntp start ; date
Thu Jan  1 00:00:58 UTC 1970
 * Stopping NTP server ntpd     [ OK ] 
 * Starting NTP server          [ OK ] 
Thu Feb 14 18:52:21 UTC 2013

What am I doing wrong?


UPDATE 2: I tried following the few suggestions that came in response to the 1st update, but nothing seems to actually do the job as required. Here's what I tried:

  1. Replace the server to us.pool.ntp.org
  2. Use explicit paths to the programs
  3. Remove the ntp service altogether and leave just sudo ntpdate ... in rc.local
  4. Remove the sudo from the above command in rc.local

Using the above, the machine still starts at 1970. However, when doing this from command line once logged in (via ssh), the clock gets updated as soon as I invoke ntpdate.

Last thing I did was to remove that from rc.local and place a call to ntpdate in my .bashrc file. This does update the clock as expected, and I get the true current time once the command prompt is available.

However, this means that if the machine is turned on and no user is logged in, then the time never gets updates. I can, of course, reinstall the ntp service so at least the clock is updated within a few minutes from startup, but then we're back at square 1.

So, is there a reason why placing the ntpdate command in rc.local does not perform the required task, while doing so in .bashrc works fine?


Solution 1:

Instead of ntpdate (which is deprecated), use ntpd:

sudo service ntp stop
sudo ntpd -gq
sudo service ntp start

The -gq tells the ntp daemon to correct the time regardless of the offset (g) and exit immediately (q) after setting the time.

Solution 2:

Probably the ntp service is running, that's why ntpdate can't open the socket (port 123 UDP) and connect to ntp server.

Try from command line:

sudo service ntp stop
sudo ntpdate -s time.nist.gov
sudo service ntp start

If you want to put this in /etc/rc.local use the following:

( /etc/init.d/ntp stop
until ping -nq -c3 8.8.8.8; do
   echo "Waiting for network..."
done
ntpdate -s time.nist.gov
/etc/init.d/ntp start )&

Solution 3:

Use sntp to set the time immediately. Some examples from its man page:

USAGE
     sntp ntpserver.somewhere
             is the simplest use of this program and can be run as an unprivileged command to check the current time and error in the local clock.

     sntp -Ss -M 128 ntpserver.somewhere
             With suitable privilege, run as a command or from a cron(8) job, sntp -Ss -M 128 ntpserver.somewhere will request the time from the server, and if that server reports that it is
             synchronized then if the offset adjustment is less than 128 milliseconds the correction will be slewed, and if the correction is more than 128 milliseconds the correction  will
             be stepped.

     sntp -S ntpserver.somewhere
             With suitable privilege, run as a command or from a cron(8) job, sntp -S ntpserver.somewhere will set (step) the local clock from a synchronized specified server, like the (dep‐
             recated) ntpdate(8), or rdate(8) commands.

It does work with any ntp time server. A convenient list of servers can be found on ntppool.org.

You need sudo privileges, for example:

sudo sntp -Ss -M 128 0.de.pool.ntp.org

Solution 4:

Use timedatectl (systemd service unit) to set the time. ntp is deprecated.

sudo systemctl restart systemd-timesyncd.service

You can check the time was updated reading the logs with journalctl -xe | tail.

Status with timedatectl status and config in /etc/systemd/timesyncd.conf.

Reference

  • Ubuntu Server documentation
  • Freedesktop
  • man timesyncd.conf