How to set current time on Linux?

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?


Solution 1:

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"

Solution 2:

Set the Hardware Clock to the current System Time.

# hwclock --systohc

Set the System Time from the Hardware Clock.

# hwclock --hctosys

Solution 3:

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

There is more information regarding this on Ubuntu Official Documentation.

Solution 4:

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

Solution 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"