I would like Ubuntu to automatically synchronize my system clock with a timeserver at startup.
However, my PC isn't connected to the Internet until after I've logged in (plus 5 - 10 seconds for good measure).
How can I set it to do this?
I would like Ubuntu to automatically synchronize my system clock with a timeserver at startup.
However, my PC isn't connected to the Internet until after I've logged in (plus 5 - 10 seconds for good measure).
How can I set it to do this?
It's generally recommended to run a service that uses NTP (Network Time Protocol) to regularly synchronize your computer's clock with a server. In recent versions of Ubuntu (at least since 18.10, or possibly earlier but I'm not sure), this is taken care of by the
systemd-timesyncd
service, which is installed and enabled by default, so there's no need to do anything special. If the service is available and active, runningshould tell you so.
For older versions of Ubuntu, you can follow instructions to set up an NTP daemon. There are several choices available but the "standard" one is in the package
ntp
. According to the instructions at the linked page,will get everything set up to synchronize with Ubuntu's NTP server.
If you really do only want to synchronize the time once at startup and never again (until the next startup), see e.g. mfisch's answer. But again, this is not recommended and there's rarely any reason it would be beneficial.
If you go to "System->Administration->Time and Date", you will get a GUI to set the date/time.
An option is provided for using time servers. If you check it and NTP is not installed, it will ask if you want to install it. Just click "yes", and let it do its job :)
You can do this using at and ntpdate. at is probably already installed, but ntpdate may not be. (apt-get install ntpdate).
First create a small script that runs ntpdate, lets call it update_time.sh.
In your .bash_login file (which you may need to create) add this:
That should do what you want. You can change the delay that at uses to be 5 minutes, 10 minutes etc.
EDIT: I just realized that you'll need to be root to run
ntpdate
. You'll need to set the SUID bit on the update_time.sh script that I mentioned. You can do that by running this from the command (only needs to be run once):Since Ubuntu 16.04 timedatectl / timesyncd (which are part of systemd) replace most of ntpdate / ntp. See Time Synchronization.
You might need to activate time synchronization using:
$ sudo timedatectl set-ntp on
If you want to use chrony:
If you require a one-shot sync use:
$ chronyd -q
If you require a one-shot time check, without setting the time use:
$ chronyd -Q
For continuous syncing, the recommended solution is chrony:
You might also need to activate
sudo timedatectl set-ntp on
Update: Another method if the above doesn't work is to set a cron job to run
$ chronyd -q