This tutorial explains Linux “hwclock” command, options and its usage with examples.
hwclock is a tool for accessing the Hardware Clock. You can display the current time, set the Hardware Clock to a specified time, set the Hardware Clock to the System Time, and set the System Time from the Hardware Clock.
The clock that is managed by Linux kernel is not the same as the hardware clock.
Hardware clock runs even when you shutdown your system.
Hardware clock is also called as BIOS clock. You can change the date and time of the hardware clock from the BIOS.
However, when the system is up and running, you can still view and set the hardware date and time using Linux hwclock command as explained in this tutorial.
hwclock -r or hwclock –show
hwclock -w or hwclock –systohc
hwclock -s or hwclock –hctosys
hwclock -a or hwclock –adjust
hwclock -v or hwclock –version
hwclock –set –date=newdate
hwclock –setepoch –epoch=year
Read the Hardware Clock and print the time on Standard Output. The time shown is always in local time, even if you keep your Hardware Clock in Coordinated Universal Time. See the –utc option.
Set the Hardware Clock to the time given by the –date option.
Set the System Time from the Hardware Clock.
Also set the kernel’s timezone value to the local timezone as indicated by the TZ environment variable and/or /usr/share/zoneinfo.
Set the Hardware Clock to the current System Time.
Add or subtract time from the Hardware Clock to account for systematic drift since the last time the clock was set or adjusted. See discussion below.
Print the kernel’s Hardware Clock epoch value to standard output. This is the number of years into AD to which a zero year value in the Hardware Clock refers. For example, if you are using the convention that the year counter in your Hardware Clock contains the number of full years since 1952, then the kernel’s Hardware Counter epoch value must be 1952.
This epoch value is used whenever hwclock reads or sets the Hardware Clock.
Set the kernel’s Hardware Clock epoch value to the value specified by the –epoch option. See the –getepoch option for details.
You need this option if you specify the –set option. Otherwise, it is ignored. This specifies the time to which to set the Hardware Clock. The value of this option is an argument to the date program. For example,
hwclock –set –date= 9/22/96 16:45:05
The argument is in local time, even if you keep your Hardware Clock in Coordinated Universal time. See the –utc option.
Specifies the year which is the beginning of the Hardware Clock’s epoch. I.e. the number of years into AD to which a zero value in the Hardware Clock’s year counter refers. It is used together with the –setepoch option to set the kernel’s idea of the epoch of the Hardware Clock, or otherwise to specify the epoch for use with direct ISA access. For example, on a Unix machine:
hwclock –setepoch –epoch=1952
–utc & –localtime
Indicates that the Hardware Clock is kept in Coordinated Universal Time or local time, respectively. It is your choice whether to keep your clock in UTC or local time, but nothing in the clock tells which you’ve chosen. So this option is how you give that information to hwclock. If you specify the wrong one of these options (or specify neither and take a wrong default), both setting and querying of the Hardware Clock will be messed up. If you specify neither –utc nor –localtime , the default is whichever was specified the last time hwclock was used to set the clock (i.e. hwclock was successfully run with the –set , –systohc , or –adjust options), as recorded in the adjtime file. If the adjtime file doesn’t exist, the default is local time.
When you pass –debug option to the hwclock, it displays some debug information, which shows exactly what hwclock command does.
1. Display Hardware Clock Date and Time
# hwclock Sat 10 Aug 2013 08:26:12 AM PDT -0.312862 seconds # hwclock -r Sat 10 Aug 2013 08:20:54 AM PDT -0.109748 seconds # hwclock --show Sat 10 Aug 2013 08:21:12 AM PDT -0.640982 seconds
2. Copy System Time to Hardware Time
As you see currently there is a difference between the system clock and hardware clock time.
# date Sat Aug 10 08:16:17 PDT 2013 # hwclock Sat 10 Aug 2013 08:26:53 AM PDT -0.687841 seconds # hwclock -w # hwclock Sat 10 Aug 2013 08:16:27 AM PDT -0.625382 seconds # date Sat Aug 10 08:16:28 PDT 2013
3. Set Hardware Clock Date and Time Manually
# hwclock --set --date "8/11/2013 23:10:45" # hwclock Sun 11 Aug 2013 11:10:48 PM PDT -0.562862 seconds
4. Copy Hardware Time to System Time using -s option
# hwclock Sat 10 Aug 2013 08:20:28 AM PDT -0.687872 seconds # date Sat Aug 10 08:34:48 PDT 2013 # hwclock -s # date Sat Aug 10 08:20:55 PDT 2013
5. Debug option usage
The following shows exactly what it does when we copy system time to hardware time.
# hwclock --systohc --debug hwclock from util-linux-ng 2.17.2 Using /dev interface to clock. Last drift adjustment done at 1375974983 seconds after 1969 Last calibration done at 1375974983 seconds after 1969 Hardware clock is on UTC time Assuming hardware clock is kept in UTC time. Waiting for clock tick... ...got clock tick Time read from Hardware Clock: 2013/08/10 15:16:37 Hw clock time : 2013/08/10 15:16:37 = 1375974997 seconds since 1969 Time elapsed since reference time has been 0.009002 seconds. Delaying further to reach the new time. Setting Hardware Clock to 15:16:37 = 1375974997 seconds since 1969 ioctl(RTC_SET_TIME) was successful. Not adjusting drift factor because it has been less than a day since the last calibration.
6. Adjust the Hardware Clock
When the system starts, it takes the time from the hardware clock. The /etc/adjtime file is used by the hwclock –adjust option to control the adjustment.
# hwclock --adjust
The /etc/adjtime file has the following three lines.
# cat /etc/adjtime -3.019877 1375975224 0.000000 1375975224 UTC
The three values in the 1st line represents
1) systematic drift rate in seconds per day,
2) number of seconds since 1969 UTC of recent adjustment
3) just 0
Line 2 is the same value that you see in the 1st line, 2nd value. i.e number of seconds since 1969 UTC of recent adjustment
Line 3 will say either UTC or LOCAL
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.