Home » Article » How To: Change time/date in linux from Command Prompt

How To: Change time/date in linux from Command Prompt


October 2012

RSS Link

  • An error has occurred; the feed is probably down. Try again later.

Twitter Updates

Blog Stats

  • 410,417 hits



Before starting this tutorial, let me clarify one thing that changing the time/date and changing a timezone are two different things.

Changing the date and time:

Please note that you can change the time and date by graphical tool also but what I am explaining here is doing all that with command line.

In every system, there are two clocks which comes into action while setting the time, one is hardware clock and the other is linux (OS) clock. The hardware clock determines the system clock on system boot. While the system is running, changes to one of these doesn’t affect the other. Normally you can follow any procedure, like first update one clock and then sync it with the other but it is always advised to first update the hardware clock and then let the linux clock sync it with it at the next reboot. Changing the system clock by using the date program on a running system could cause date discontinuities and consequently problems. Down here, I will be discussing both ways.

For viewing/changing the time, we can use “date” command.

# date
Thu Feb 25 11:56:54 IST 2010

Changing time from “date” command is the easiest part. Simply copy paste the output of the date command and change the values you wanted to and then set it.

# date –set=”Thu Feb 28 14:05:15 IST 2010″
Sun Feb 28 14:05:15 IST 2010

But this is something which doesn’t work in all flavors. For ex, flavors like freebsd work in different ways. To change time/date in freebsd is a little bit confusing but very much easy. You just need to use “date” command followed by the month, day, hour, minute, and year all numeric and no spaces.

# date 021415232010
Sun Feb 14 15:23:31 PST 2010

If you will be using ntpd, you probably don’t need to set either of these clocks (unless the current time is more than 1000s off the real time). Just setup ntpd and let ntpd adjust the time (it will do it in small steps to keep system timestamps reliable).

Redhat Systems: Using the dateconfig tool will update both the system clock and the hardware clock. The dateconfig tool also allows you to setup ntpd, which will keep the system clock in sync with a remote server.

Only change time not date:

To only set the time use like this:

# date -s hh:mm:ss

Update Hardware clock:

To sync the hardware clock to the current system clock:

# hwclock –systohc
# hwclock –systohc –utc
***** use the second option if you use UTC.

Set Hardware clock manually:

Earlier i told you that better idea is to set the hardware clock and then let the OS clock sync with that.

To know the current time of the hardware clock:

# hwclock –show
Tue Nov  4 22:13:40 2003  -0.684660 seconds

To set the hw clock manually:

# hwclock –set –date=”09/21/2005 14:23:23″

Everytime you use the hwclock –set command, it will create or edit the file /etc/adjtime to determine the systematic drift. Once you have some history, you can use the –adjust option to adjust the hardware clock appropriately. Run as a cron job if you want the clock to adjust automatically on a regular schedule. Don’t use the –adjust function when using ntpd since ntpd will turn the “11 minute mode” on, which is best left alone. See the hwclock manpage for more info.

Now you can leave the OS clock to sync it with the hardware clock on the next reboot or else you can do it manually now:

# hwclock –hctosys
# date
Wed Sep 21 12:23:23 PST 2005

Using NTP (Network Time Protocol):

NTP will connect to the servers to get the atomic time. To use NTP, simply download and install it, and use the ntpdate command to sync your time with the NTP servers.

# ntpdate “server name or IP address”

Now update the hardware clock also.

# hwclock –systohc

To keep your system always updated you can create a cronjob which could run on daily basis and put this:

# ntpdate “server” && hwclock -w
***-w == –systohw

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: