70

How do I retrieve the date from the Internet and set my computer's clock, from the command line?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Mohsen
  • 2,495
  • 4
  • 25
  • 29
  • 5
    I do not understand downvotes. Simple questions are what make a community powerful. For example see [this](http://stackoverflow.com/questions/948135/how-can-i-write-a-switch-statement-in-ruby) – Mohsen Jun 12 '13 at 06:35
  • 4
    When I hover above a question's down arrow with my mouse I see: "This question does not show any research effort; it is unclear or not useful". I would be tempted to downvote your question based on the first (no indication of what you found), the second (no indication on what specific platform you are working on, and why this has to be commandline (instead of the usual time daemon)) **and** the third (most *nix users will have some time daemon installed by default). – Anthon Jun 12 '13 at 07:09
  • 1
    +1 I do not understand the downvotes either, simply because this is something which has bugged me many times before, and it seems impossible to find anything concise and useful about this specific theme. – l0b0 Jun 12 '13 at 07:52
  • @Mohsen I didn't downvote but the difference to the question you linked is that there is no `switch` in ruby while you can set directly the date via `date -s` (as shown in the manpage), or use any of the commands described below... – Ulrich Dangel Jun 12 '13 at 08:33
  • possible duplicate of [Trying to set date on a Linux based machine from another machine](http://unix.stackexchange.com/questions/23265/trying-to-set-date-on-a-linux-based-machine-from-another-machine) – Anthon Jun 12 '13 at 11:22
  • 1
    @Anthon I disagree: that question is about a small device with BusyBox, which makes the generic advice “use NTP” not necessarily applicable – Gilles 'SO- stop being evil' Jun 12 '13 at 22:55
  • The example I saw was the highest voted comment: "This is amazing how this question is upvoted but doesn't show any search effort." You could find the answer if you type the title of your question plus your operating system into any search box google/ddg/bing. – dfc Sep 19 '15 at 23:53
  • related: https://superuser.com/questions/635020/how-to-know-current-time-from-internet-from-command-line-in-linux – Ciro Santilli OurBigBook.com Feb 08 '18 at 06:42

9 Answers9

62

A small command I found to update your time in case you don't want to install anything just to update the date. :)

sudo date -s "$(wget --method=HEAD -qSO- --max-redirect=0 google.com 2>&1 | sed -n 's/^ *Date: *//p')"

You can check whether date correctly understands the string before actually setting the time by running

$ date -d "$(wget --method=HEAD -qSO- --max-redirect=0 google.com 2>&1 | sed -n 's/^ *Date: *//p')"
Mon Jan 10 11:40:46 AM CET 2022

The wget command may be replaced by curl -Is --max-redirs 0 google.com.

Note that --method=HEAD is used because it seems that GET responses may be cached (with an old Date header) while HEAD responses are fresh.

tom
  • 162
  • 4
Prashant Kabade
  • 721
  • 5
  • 3
  • 3
    I use `google.in` to get the current time for my timezone (INDIA). – Dilawar Jul 25 '18 at 12:35
  • 2
    Note the trailing `Z` which indicates that the time is given in zulu time zone, so no special timezone manipulation should be needed if the computer timezone is set correctly. – user202729 Sep 06 '18 at 13:13
  • After that don't forget to change HW clock with `sudo hwlock --systohc`. – Monsieur Pierre Doune Feb 11 '20 at 05:06
  • This is wrong after PM. – Vitaly Zdanevich Jun 10 '20 at 12:01
  • 3
    I would switch the order of the test and set code examples in the answer, as it's better to test first. ;-) For me I needed to change the cut args to `-f4-10`, so `date -d "$(wget --method=HEAD -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f4-10)"` – matt wilkie Apr 14 '22 at 20:52
51

Firstly, if you want to change your timezone you can use:

sudo dpkg-reconfigure tzdata

To update the time and date from the internet on a Linux distribution that uses a modern version of systemd, you can use:

timedatectl set-ntp true

Alternatively, you can install the reference NTP implementation as described below.


Reference NTP implementation

Install

If ntpd is not installed use one of the following command to install ntpd:

  • For RPM based:
    yum install ntp 
    
  • For Debian based:
    sudo apt-get install ntp
    

Configuration

You should at least set following parameter in /etc/ntp.conf config file:

server <Time Server Name or IP Address>

For example, open /etc/ntp.conf file using vi text editor:

# vi /etc/ntp.conf

Locate server parameter and set it as follows:

server pool.ntp.org

Save the file and restart the ntpd service:

# /etc/init.d/ntpd start

You can synchronize the system clock to an NTP server immediately with following command:

# ntpdate pool.ntp.org

Manually setting the date

For setting the time and date manually use the following syntax:

date --set="STRING"

For example, to set the date to 2 Oct 2006 18:00:00, type the following command as root user:

# date -s "2 OCT 2006 18:00:00"

OR

# date --set="2 OCT 2006 18:00:00"

You can also simplify format using following syntax:

# date +%Y%m%d -s "20081128"

To set time use the following syntax:

# date +%T -s "10:13:13"

where:

  • 10: Hour (hh)
  • 13: Minute (mm)
  • 13: Second (ss)

You can use %p for the locale's equivalent of AM/PM:

# date +%T%p -s "6:10:30AM"
# date +%T%p -s "12:10:30PM"
AdminBee
  • 21,637
  • 21
  • 47
  • 71
tusharmakkar08
  • 1,805
  • 1
  • 22
  • 23
  • 1
    Although the date information seems all valid, it does not take the requirement `from the internet` from the **title** of the OP into account anywhere. – Anthon Jun 12 '13 at 07:03
  • I believe that tzdata (time zone data) package synchronizes the data from internet only. Correct me if I am wrong . – tusharmakkar08 Jun 12 '13 at 07:26
  • 3
    [tzdata](https://launchpad.net/ubuntu/+source/tzdata) is information about timezones and daylight saving time (start, end, epoch) not the *actual* date and time. Of course that is handy to know how to get to your local time based on knowledge of your timezone and a UTC time, but you still need to retrieve the latter from the internet. – Anthon Jun 12 '13 at 07:42
  • You are wrong. time zone data has to be updated (as there are changes happening all the time) BUT it does not update itself automatically from the internet NOR does it set the time from the internet. – Ulrich Dangel Jun 12 '13 at 08:32
  • @Anthon : I have edited my answer correspondingly.Thank You. – tusharmakkar08 Jun 12 '13 at 09:43
  • What distro uses dpkg AND yum? – dfc Sep 19 '15 at 23:54
  • No one (AFAIK). I answered it while I was shifting from Linux mint to fedora. Updating the answer correspondingly. – tusharmakkar08 Aug 03 '16 at 08:51
  • When I change from Windows to Linux, my laptops time gets changed. $ sudo ntpdate pool.ntp.org; resets it. – Prajwal Feb 11 '19 at 10:30
21

I use this:

sudo ntpd -qg; sudo hwclock -w

first tell ntpd to just set the time and stop after that with -q. Also, in case a your clock has a big error we need to tell ntpd to also adjust in that case with -g. Finally write the clock to hardware to preserve the changes when rebooting with hwclock -w (-w for setting hardwareclock to current system time, there is a difference).

Silverrocker
  • 1,815
  • 1
  • 15
  • 23
  • 2
    i tend to do this with a "&&" instead of the semicolon so that if the first command should fail (e.g. network connectivity down) the second command is short-circuited – Theophrastus Sep 19 '15 at 20:06
7

Use ntpdate, ntpd, or Chrony to connect to a NTP server.

Ignacio Vazquez-Abrams
  • 44,857
  • 7
  • 93
  • 100
7

Some distributions are shipping rdate for that purpose. Basic usage:

# just query
bash-4.2$ rdate time.nist.gov
rdate: [time.nist.gov]   Wed Jun 12 11:05:40 2013

# set system time
bash-4.2$ rdate -s time.nist.gov
manatwork
  • 30,549
  • 7
  • 101
  • 91
  • Way better and easier than ntp – andrewtweber Mar 30 '16 at 18:39
  • 1
    `pool.ntp.org` now refuses connections from `rdate`. `time.nist.gov` works. Also once you have set the system time, you should set the hardware clock as well by running `hwclock -w` to set it to the system time. You can then verify it is correct by running `hwclock -r` – MatthewLee Jul 14 '17 at 14:53
5

After some research, I ended up with this. I also applied it to my own server:

sudo apt-get install ntp
sudo dpkg-reconfigure ntp
ntpq -p

If the last command shows a valid list of servers, you are good to go. The command will run a quite complex set of algorithms which will iterate your clock drift, among other things, and compensate for them. You will end up with a pretty accurate clock even if you lose the connection to the NTP servers. However, the command does require a few minutes to get started.

Source: here and here.

Tubbles
  • 51
  • 1
  • 2
1

I also was looking for a non ntp/ntpd way of resetting clock periodically. I liked the google.com header parsing but found it did not work on ubuntu. I think this will also work on a Raspberry Pi.

sudo date +"%d %b %Y %T %Z" -s "$(wget -qSO- --max-redirect=0 http://google.com 2>&1 | grep '^  Date:' | cut -d' ' -f 5-)"

Also an alternative using curl instead of wget.

sudo date +"%d %b %Y %T %Z" -s "$(curl -s --head http://google.com | grep '^Date:' | cut -d' ' -f 3-)"

Tested on PI with Cellular Network and results are similar:

Using host & wget -- RCV: 1324 bytes SND: 581 bytes
Using host & curl -- RCV: 1318 bytes SND: 567 bytes

Note I use host to resolve google.com to an IP address so I can open only that IP for duration of this command with iptables.

Ron B
  • 11
  • 2
1

To find "peers" (hosts you can query) you can use ntpq -p.

If you already have an NTP service running you have to stop it before manually updating, for example using sudo service ntp stop.

Now you can query a peer, for example using sudo ntpdate "peer".

l0b0
  • 50,672
  • 41
  • 197
  • 360
0

Using rdate tool as suggested in manatwork's answer, but with SNTP protocol -n and IPv4 -4 options on:

# just print synced time, not set
rdate -n -4 -p time-a.nist.gov

# print and set synced time
sudo rdate -n -4 time-a.nist.gov

The tool may be installed on Debian this way:

sudo apt-get install rdate
Serge S.
  • 101
  • 2