How to set Ubuntu to synchronize my clock with a time server?
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, running
timedatectl status
should 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,
sudo apt-get install ntp
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.
#!/bin/bash
ntpdate pool.ntp.org
In your .bash_login file (which you may need to create) add this:
at -f ~/update_time.sh now + 1 minute
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):
sudo chmod 4711 update_time.sh
sudo chown root update_time.sh