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:
- Replace the server to
us.pool.ntp.org
- Use explicit paths to the programs
- Remove the
ntp
service altogether and leave justsudo ntpdate ...
inrc.local
- Remove the
sudo
from the above command inrc.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?
Instead of ntpdate (which is deprecated), use
ntpd
:The
-gq
tells the ntp daemon to correct the time regardless of the offset (g
) and exit immediately (q
) after setting the time.Probably the
ntp
service is running, that's whyntpdate
can't open the socket (port 123 UDP) and connect to ntp server.Try from command line:
If you want to put this in
/etc/rc.local
use the following:Use sntp to set the time immediately. Some examples from its man page:
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:
Use
timedatectl
(systemd service unit) to set the time.ntp
is deprecated.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
man timesyncd.conf
As others have pointed out the best solution is to instruct ntpd to ignore the panic threshold, which is 1000 seconds by default. You can configure the panic threshold in one of two ways:
/etc/default/ntp
and ensure that the -g option is present.tinker panic 0
at the topSo far this is essentially what others have recommended however there is one more step I think you should take. Install the fake-hwclock program:
With fake-hwclock installed your machine will not start up thinking it is 1970 all over again. When your machine boots up it will set its clock to the timestamp fake-hwclock wrote during the last reboot/shutdown. This means you can have a somewhat correct clock in case there are network issues when you boot up.
ntpdate is a program different from the net dameon. NTPDate is probably erroring out on boot because ntpd is running on that socket.
From the command line, run
You could also uninstall ntpd all together (apt-get remove ntp) and add a cron script to use ntpdate every hour or so.
UPDATE
ntp service probably won't have meaningful value for you on this system, so remove that first.
Now add the command:
to
/etc/rclocal
Reboot. Should be good at that point.
if all you want to do is set the clock once, simple
Note that some current Ubuntu based systems don't even use the NTP service by default now. On my Linux Mint 19 (Ubuntu 18.04) machine, time is kept by
systemd-timesyncd
.So to get an up to date time after it has lost sync, I just run
Since 15.04 Ubuntu uses systemd by default. Therefore critical systems like time are managed through systemd. To find what service your system is using run something like
For TechJS on 16.04 the service was
ntp
. For myself on Ubuntu 18.04 (Mint 19) the service issystemd-timesyncd
. Interestingly, I logged into a 16.04 server I have and it usessystemd-timesyncd
as well.Check out this one (no apt, no service):
Cheers to Prashant, who solved that issue
The correct way to do this on a Debian / Mint / Ubuntu (or other Debian derivative) system is to have the line
in the file
This ensures that when ntpd is started from the /etc/init.d/ntp script, it runs with the "-g" option, viz
to allow ntpd to correct the system time when it is more than 1000 s out, eg when the system time is January 1st 1970 on startup because there is no hardware RTC.