How To: Change time/date in linux from Command Prompt
Before starting this tutorial, let me clarify one thing that changing the time/date and changing a timezone are two different things. If you are looking for changing the Timezone then refer to this article: Change Timezone in Linux/Unix.
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.
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.
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.
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:
Update Hardware clock:
To sync the hardware clock to the current system clock:
# 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:
Tue Nov 4 22:13:40 2003 -0.684660 seconds
To set the hw clock manually:
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:
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.
Now update the hardware clock also.
To keep your system always updated you can create a cronjob which could run on daily basis and put this:
***-w == --systohw