What is the command to update time and date from internet

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?

8

11 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"
12

You can do so with e.g. sudo ntpdate time.nist.gov. Other servers include time.windows.com, etc.

lists time servers around the world.

9

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):

ntpdate is considered deprecated in favour of timedatectl and 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.

6

Running this command in a terminal should do the trick

sudo dpkg-reconfigure tzdata

You can add extra time zones graphically, I think, by clicking on the clock and going through its options.

6

It's very easy to set up from command line: 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

1

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
dateFromServer=$(curl -v --silent 2>&1 \ | grep Date | sed -e 's/< Date: //'); date +"%d%m%Y%H%M%S" -d "$dateFromServer"

or

date -s `curl -I ' 2>/dev/null | grep -i '^date:' | sed 's/^[Dd]ate: //g'`
3

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

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.

You need to install the ntp package. Date/Time settings are availble under system settings. Here's some more information.

1

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 ' 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'

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like