Set Date And Time In Linux And Sync With Hardware (CMOS)

Why are the date and time important?

Having the correct date and time in your Linux box is very important. When you use emails ,the default time used by the application is the system calendar's date and time. Unless otherwise you have specified a different time scale. If you work in a client server network, most of the tasks are scheduled according to the system clock and the client synchronizes time with the server.

Interestingly, some people (including me) check the computer to find the date and time!
So, everybody should know how to change the date and time. For example, when you set up a new server and start business, you are not going to shutdown the server . What if you forget to change the date and time? The 'hwclock' command is there to save you. By using 'hwclock' you can change the time in the clock without shutting down the server.

Suppose, you are the network admin and you need to schedule a backup at mid night. If the system clock is not correct, you will end up running the backup at some other time and when you check the system logs you will go nuts!

Date command

To check the system date:

[centoslive@livecd Desktop]$ date
Sun Jul 8 22:15:55 EDT 2012

How to check the date 2 days ago:

[centoslive@livecd Desktop]$ date --date="2 days ago"
Fri Jul 6 22:17:58 EDT 2012

How to set the system date? ( I am going o change date to 2010-12-12 3.30p.m):

[root@livecd Desktop]# date '-s 2010-12-12 15:30:15'
Sun Dec 12 15:30:15 EST 2010
[root@livecd Desktop]# date
Sun Dec 12 15:30:17 EST 2010

Hwclock command

How to check CMOS date:

[root@livecd Desktop]# hwclock
Sun 08 Jul 2012 10:20:06 PM EDT -1.120674 seconds

Ok, now my system clock date is 2010 and CMOS date is 2012.Now I am going to change the CMOS date in order to synchronize it with my system date:

[root@livecd Desktop]# hwclock --systohc
[root@livecd Desktop]#

[root@livecd Desktop]# hwclock
Sun 12 Dec 2010 03:31:28 PM EST -1.499303 seconds
[root@livecd Desktop]#

Now, the CMOS clock date is set to 2010-12-12.I am going to change the system date to 2011-11-11.

[root@livecd Desktop]# date '-s 2011-11-11 15:30:15'
Fri Nov 11 15:30:15 EST 2011

Still my CMOS date shows 2010-12-12.I am going to change this to system date:

[root@livecd Desktop]# hwclock --systohc

[root@livecd Desktop]#
[root@livecd Desktop]# hwclock
Fri 11 Nov 2011 03:31:20 PM EST -0.449282 seconds
[root@livecd Desktop]#

Reverting back the changes:

[root@livecd Desktop]# hwclock --hctosys
[root@livecd Desktop]# date
Fri Nov 11 15:31:39 EST 2011
[root@livecd Desktop]#

What really happens

The computer's CMOS battery always synchronizes time with the system clock provided that the CMOS is working properly. You can use the 'hwclock' command to synchronize date manually.

About Bobbin Zachariah

Founder of LinOxide, passionate lover of Linux and technology writer. Started his career in Linux / Opensource from 2000. Love traveling, blogging and listening music. Reach Bobbin Zachariah about me page and google plus page.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.