What is the command to update time and date from Internet? Is there any application that allows me to do so from its user interface rather than from the shell?
13 Answers
This is a nice little code I found to update your time in case you have issues with ntp:
sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"
- 41,268
- 2,717
You can do so with e.g. sudo ntpdate time.nist.gov. Other servers include time.windows.com, etc.
http://www.pool.ntp.org/ lists time servers around the world.
As of 2018 with a fresh installed Ubuntu 16.04 LTS, running sudo ntpdate time.nist.gov gives:
sudo: ntpdate: command not found
This is because (official source):
ntpdateis considered deprecated in favour oftimedatectland thereby no more installed by default.
Instead do this to force the sync to happen now:
sudo timedatectl set-ntp off
sudo timedatectl set-ntp on
In my case I was running a Ubuntu on a virtualbox and had saved the machine state so when I started the instance back up again it did not automatically sync the clock since there was no boot event to trigger the sync. So the time was still showing what it was the last time I was running the virtual box.
- 1,493
It's very easy to set up from command line: https://help.ubuntu.com/lts/serverguide/NTP.html From that link:
Ubuntu comes with ntpdate as standard, and will run it once at boot time to set up your time according to Ubuntu's NTP server:
ntpdate -s ntp.ubuntu.com
Here's GUI example https://help.ubuntu.com/community/UbuntuTime#Time_Synchronization_using_NTP
- 33,013
- 280
I had this issue because I would pause a VM for a while and when I resume, Ubuntu 20.04 would refuse to update any packages until the time was corrected. (i.e. packages were not from the 'future').
The easiest way (by terminal) was to run this command:
sudo systemctl restart systemd-timesyncd
Tested on Ubuntu's main GNOME edition 20.04.
No need for a separate command to restart NTP. Updates the reported time in GNOME as well.
You can add this to your bash files:
synctime() {
sudo systemctl restart systemd-timesyncd
}
and run the command after reloading your bash files (or opening a new terminal).
synctime
- 101
Most here won't work, since ntp will override your settings within seconds.
You need to disable NTP first. On ubuntu it is done as:
# Disable ntp
sudo timedatectl set-ntp 0
Then you can do:
# Set software clock
sudo date --set="2018-04-01 22:22:22"
# Sync with hardware clock
sudo hwclock --systohc
- 168
dateFromServer=$(curl -v --silent https://google.com/ 2>&1 \
| grep Date | sed -e 's/< Date: //'); date +"%d%m%Y%H%M%S" -d "$dateFromServer"
or
date -s `curl -I 'https://startpage.com/' 2>/dev/null | grep -i '^date:' | sed 's/^[Dd]ate: //g'`
- 213
Is there any application that allows me to do so from its user interface rather than from the shell?
I'm using 17.10 and can go to Settings (from the upper-right menu in the UI) > Details > Date & Time. In my case, my system wasn't updating from the Internet even though "Automatic Date & Time" was set to "ON". I simply changed it to "OFF", waited a second, then changed it back to "ON". It picked up the current date and time and I was good to go.
- 1,156
- 6
- 15
- 21
Thanks to Twiglets [For AsusWRT/Merlin Routers]
Here is an alternative that DOES set the date !!! [-s option]. Prints out 'Date' it retrieves & the 'Date' that is set for comparison.
On AsusWRT / Merlin, the only thing that is odd is that the date retrieved is ".... GMT" and the date utility sets the correct time but changes it to "... DST" Environment has TZ set to "GMT"
datetext=$(curl -I 'https://1.1.1.1/' 2>/dev/null | grep "Date:" |sed 's/Date: [A-Z][a-z][a-z], //g'| sed 's/\r//') ; echo "Date Retrieved = $datetext" ; echo -n "Date set = " ; date -s "$datetext" -D'%d %b %Y %T %Z'
- 213
Sometimes when time is out of sync, it is really hard to even go to the internet since it could make DNSSEC validation failures.
This is another way to sync time using ssh
sshpass -p 'password' ssh -t -o StrictHostKeyChecking=no user@hostname "sudo -S <<< 'password' date -s @$(date -u +"%s")"
- 101
You can check if automatic time synchronization is enabled by running:
timedatectl status
If you see System clock synchronized: no, it means automatic syncing is off. To enable it, run:
sudo systemctl enable --now chronyd
This will start the chrony service, which keeps your system clock synchronized automatically.
If you want to force an immediate time update, use:
sudo chronyc makestep
This command forces the system clock to be corrected right away.
You need to install the ntp package. Date/Time settings are availble under system settings. Here's some more information.
- 7,502