238

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?

0xF2
  • 3,155
Vikramjeet
  • 5,996
  • Can you identify which Ubuntu version you are using? – david6 Nov 21 '11 at 22:04
  • 1
    Do you need extremely accurate time? (If so, you need to familiarize yourself with ntpq and choose time servers). If you just want your time to be (approx.) correct, then try: System Settings >> Time & Date, and check that 'Set the time' is set to 'Automatically from the Internet'. – david6 Nov 21 '11 at 23:26
  • If you want constantly extremely accurate, install ntpd. This is a small process that runs in the background and adjusts time constantly instead of bursts/jumps, but it will take up a bit of your resources. – strugee May 24 '13 at 04:08
  • Worth noting that this changed as of 2018, and the top answers are no longer correct for recent versions of Ubuntu. You will likely need this answer: https://askubuntu.com/a/998449/53783 – Niels Abildgaard May 15 '18 at 11:38
  • sudo ntpd should be enough – forzagreen Jun 21 '18 at 08:38
  • 2
    This question is of critical importance nowadays. Lately it's become very tricky : some PCs use chrony or ntpd, they always find a reason NOT to update the time (clock is too wrong, win dual-boot-related issues, ntp may also refuse to trust clock sources that don't have internet). Moreover, if you have a PC with internet, but a wrong clock, as now browsers and websites force you to use HTTPS, it will block as your clock is wrong, and you can't even google how to fix it! It would be nice if there was an answer that addresses all cases. "Set the clock by hand" is the most reliable one now... – bct Apr 23 '19 at 05:55

13 Answers13

260

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"
kos
  • 41,378
201

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.

B--rian
  • 85
Anonymous
  • 12,009
124

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.

User
  • 1,513
28

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.

Zanna
  • 72,471
Jayo
  • 1,251
  • Timezones are not really the same as (accurate) time. But, it also not very clear what Vikramjeet was asking either. – david6 Nov 21 '11 at 22:05
  • What is the command to update time and date from Internet is the question, sudo dpkg-reconfigure tzdata is the answer, so what gives? the comment about the timezones, is related to the only thing i know to be possible graphically – Jayo Nov 21 '11 at 22:24
  • I assumed that by graphically they meant a graphical (or 'GUI') method, and NOT geographic (or worldwide). – david6 Nov 21 '11 at 22:55
  • so did i, i am not aware of a way to update the date and time via the net graphically, so i use the terminal, what i do know how to do graphically is set up time zones, the part about time zones was my two cents on what can be done to affect the time in a graphical manner, as far as i knew, am i understood now? – Jayo Nov 21 '11 at 23:19
  • this works for me since it does update the time – ch271828n Oct 01 '21 at 00:39
  • 1
    This update timezone but not the date of the system – Loenix Jun 15 '22 at 09:44
13

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

mikewhatever
  • 33,073
Daimon
  • 280
  • 6
    Please do not use links to other sites. When they disappear so does the information you posted. Prevent 'link rot' from happening by answering with the content of the link (and give credit where credit is due). – Rinzwind Nov 21 '11 at 13:46
11

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
7

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
mjs
  • 168
4
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'`
Tyler
  • 213
1

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.

0

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'
Tyler
  • 213
0

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")"
0

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.

0

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

tohuwawohu
  • 7,522
  • 4
    The link is dead... As said by Rinzwind "Please do not use links to other sites. When they disappear so does the information you posted. Prevent 'link rot' from happening by answering with the content of the link (and give credit where credit is due)." Thanks – Michele Sep 07 '14 at 14:30