hwclock Command in Linux with Examples

This tutorial explains Linux “hwclock” command, options and its usage with examples.

hwclock – query and set the hardware clock (RTC).

DESCRIPTION

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.

advertisement
advertisement

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.

SYNOPSIS

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 –getepoch
hwclock –setepoch –epoch=year

OPTIONS :

Note: Join free Sanfoundry classes at Telegram or Youtube

–show
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
Set the Hardware Clock to the time given by the –date option.
–hctosys
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.
–systohc
Set the Hardware Clock to the current System Time.
–adjust
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.
–getepoch
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.
–setepoch
Set the kernel’s Hardware Clock epoch value to the value specified by the –epoch option. See the –getepoch option for details.
–date=date_string
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.
–epoch=year
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.
–debug
When you pass –debug option to the hwclock, it displays some debug information, which shows exactly what hwclock command does.

EXAMPLES

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

advertisement

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

advertisement
# 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.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.