How do I retrieve the date from the Internet and set my computer's clock, from the command line?
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
wget command may be replaced by
curl -Is --max-redirs 0 google.com.
--method=HEAD is used because it seems that
GET responses may be cached (with an old
Date header) while
HEAD responses are fresh.
You can use :
sudo dpkg-reconfigure tzdata
for configuring your timezone .
For updating time and date from internet use the following :
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
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:
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:
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"
# 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"
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"
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).
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
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.
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
In addition to Tushar's reply, I also had to do
apt-get install ntpdate on my Ubuntu 14.04.
Posting as answer because reputation does not suffice for commenting.
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.