How do I set time and date from the Internet?

147,471

Solution 1

You can use :

sudo dpkg-reconfigure tzdata

for configuring your timezone .

For updating time and date from internet use the following :

Install

If ntpd is not installed use any 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

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

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

 date --set="STRING"

For example, set new data 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)

Use %p locale’s equivalent of either AM or PM, enter:

# date +%T%p -s "6:10:30AM"

# date +%T%p -s "12:10:30PM"

Solution 2

A small code 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 | grep Date: | cut -d' ' -f2-7)"

This command assumes that the "Date" header in the response to the wget request has the format Date: Wed, 05 Jan 2022 10:53:35 GMT. The format of the header has changed in the past. 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 | grep Date: | cut -d' ' -f2-7)"
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.

Solution 3

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

Solution 4

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

Solution 5

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

# just query
bash-4.2$ rdate pool.ntp.org
rdate: [pool.ntp.org]   Wed Jun 12 11:05:40 2013

# set system time
bash-4.2$ rdate -s pool.ntp.org
Share:
147,471

Related videos on Youtube

corsel
Author by

corsel

Updated on September 18, 2022

Comments

  • corsel
    corsel almost 2 years

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

    • corsel
      corsel about 11 years
      I do not understand downvotes. Simple questions are what make a community powerful. For example see this
    • Anthon
      Anthon about 11 years
      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).
    • l0b0
      l0b0 about 11 years
      +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.
    • sunnysideup
      sunnysideup about 11 years
      @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...
    • Anthon
      Anthon about 11 years
    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' about 11 years
      @Anthon I disagree: that question is about a small device with BusyBox, which makes the generic advice “use NTP” not necessarily applicable
    • dfc
      dfc almost 9 years
      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.
    • Ciro Santilli Путлер Капут 六四事
      Ciro Santilli Путлер Капут 六四事 over 6 years
  • Anthon
    Anthon about 11 years
    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.
  • tusharmakkar08
    tusharmakkar08 about 11 years
    I believe that tzdata (time zone data) package synchronizes the data from internet only. Correct me if I am wrong .
  • Anthon
    Anthon about 11 years
    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.
  • tusharmakkar08
    tusharmakkar08 about 11 years
    @Anthon : I have edited my answer correspondingly.Thank You.
  • Ulises Sánchez
    Ulises Sánchez almost 9 years
    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
  • dfc
    dfc almost 9 years
    What distro uses dpkg AND yum?
  • andrewtweber
    andrewtweber over 8 years
    Way better and easier than ntp
  • tusharmakkar08
    tusharmakkar08 almost 8 years
    No one (AFAIK). I answered it while I was shifting from Linux mint to fedora. Updating the answer correspondingly.
  • MatthewLee
    MatthewLee almost 7 years
    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
  • Kodiak
    Kodiak almost 6 years
    I use google.in to get the current time for my timezone (INDIA).
  • user202729
    user202729 almost 6 years
    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.
  • Prajwal
    Prajwal over 5 years
    When I change from Windows to Linux, my laptops time gets changed. $ sudo ntpdate pool.ntp.org; resets it.
  • Monsieur Pierre Dunn
    Monsieur Pierre Dunn over 4 years
    After that don't forget to change HW clock with sudo hwlock --systohc.
  • Vitaly Zdanevich
    Vitaly Zdanevich about 4 years
    This is wrong after PM.
  • matt wilkie
    matt wilkie about 2 years
    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)"