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?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityI 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, 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.
timedatectl
command shows that the systemd-timesyncd
service is running and indeed the time is actually being synchronized. The GUI confused me so much.
Dec 5, 2020 at 19:00
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
at
? And if you're going to automatically run ntpdate
, why not just install the ntp
service?
Aug 27, 2014 at 12:07
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:
chrony(d)
The NTP daemon chronyd calculates the drift and offset of your system clock and continuously adjusts it, so there are no large corrections that could lead to inconsistent logs for instance. The cost is a little processing power and memory, but for a modern server this is usually negligible. Installation
To install chrony, from a terminal prompt enter:
$ sudo apt install 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