81

Why is the output of the following commands different?

root@vmi2115:/var# hwclock
Sun 26 Jun 2011 01:21:38 PM CEST  -0.273230 seconds
root@vmi2495:/var# date
Sun Jun 26 15:21:39 CEST 2011
root@vmi2115:/var# 

And can I change the current time on Linux?

1

11 Answers 11

97

Usually you'll want to have the time set automatically, and in that case, you'll want to set up ntpd to automatically set the time for you.

The specifics differ slightly from distribution to distribution, but if you're running Ubuntu, for instance, there's a guide on setting up NTP on Ubuntu. Otherwise, just Google ntpd <distribution-name>, and you'll probably find it.

If you want to set it manually, however, you can use date --set="<date string>". Examples of this could be:

date --set="23 June 1988 10:00:00"
date --set="10:00:00"
0
41

Set the Hardware Clock to the current System Time.

# hwclock --systohc

Set the System Time from the Hardware Clock.

# hwclock --hctosys
27

Just ntpdate ntp.ubuntu.com and everything will be fine.

There is more information regarding this on Ubuntu Official Documentation.

7
  • 18
    please elaborate further on your answer and supply an explanation to the QA and future readers what does the command you mentioned do and why you recommend such action. note: the QA asked how to change the time, not how to keep it synced. Apr 1, 2013 at 13:55
  • 1
    @LorenzoVonMatterhorn ok Apr 5, 2014 at 0:40
  • This still has no explanation. What should I expect to happen if I run this command?
    – jerclarke
    Oct 2, 2014 at 17:19
  • 2
    Worked great for setting exact time. Oct 18, 2016 at 19:33
13

Another common, very annoying problem is, when the wrong timezone is selected...

Check the timezone with the date output:

$ date

Fre Aug 23 18:47:04 UTC 2013

To correct the timezone type:

$ sudo tzselect

and select the correct Region with the corresponding numbers.

A second task is to set the correct time:

$ sudo date --set="18:37:00"

Or simply take the time from the HW-Clock:

$ sudo hwclock --hctosys

5

"hwclock" is date of hardware (machine), and "date" is date of software (operative system).

For change pc hardware date:

hwclock --set --date="2013-7-31 09:30"

For change software date:

date --set "2013-7-31 09:30"
4

In CentOS timezones are located in /usr/share/zoneinfo/, to change the system date to a specific timezone you can:

First, make a backup of the existing localtime file.

sudo mv /etc/localtime /etc/localtime.bak

Next, create the link:

sudo ln -s /usr/share/zoneinfo/Europe/Lisbon  /etc/localtime

Check the new system date by typing date.

To adjust the Linux system date manually you can type:

date -s "11 MAR 2006 11:11:11"

OR

date --set="11 MAR 2015 11:11:11"

OR

date +%Y%m%d -s "20150311"

To set the Hardware Clock to the current System Time:

hwclock --systohc

OR

hwclock -w
2

Try this:

date +%T -s "12:21:00"

or

date +%T%p -s "12:22:20AM"
date +%T%p -s "12:23:30PM"
1

can I change the current time on Linux?

For me, I needed to run this command first:

sudo systemctl stop systemd-timesyncd

Otherwise, systemd would reset the time immediately. After stopping the systemd service, running date works as expected:

$ sudo date -s 'january 1 1971'
Fri Jan  1 00:00:00 CET 1971
$ date
Fri Jan  1 00:00:00 CET 1971

While the service was running, the date -s command seemed to work, but running date a moment later would give me the systemd time again.

0

date return the time given as the time from the moment the cpu started plus the internal hardware clock given time, hwclock gives the time the internal clock has.

The cpu based time tend to drift the long the machine is up, that is the reason behind the existence of hwclock --hctosys command. Also is one of the reasons behind the the use of the Network Protocol Time, which is used to coordinate time internationally on the internet.

0

use this if haven't NTP client

date -s $(wget -q -O - http://www.timeapi.org/eest/now | cut -d '+' -f 1 | tr -d : | tr -d - | tr -d T | cut -c 1-12)

eest in URL is your local timezone

You can use utc ... and etc

1
0

From Sebastian's answer and Ubuntu OS guide:

Using the command line (Terminal), you can use sudo dpkg-reconfigure tzdata:

  1. Open a terminal window by going to Applications>Accessories>Terminal
  2. sudo dpkg-reconfigure tzdata
  3. Follow the directions in the terminal.

The timezone info is saved in /etc/timezone - which can be edited.

My environment OS: Mendel Linux, HW: Google Coral

You must log in to answer this question.