≡ Menu

7 Linux hwclock Command Examples to Set Hardware Clock Date Time

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.

1. Display Hardware Clock Date and Time

Just type hwclock, which will display the date and time of your system’s hardware clock. This is the same date and time that you’ll see from the BIOS screen. You can also use option -r, or –show to display the 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

Note: The output of the date command might be same as the output of the hwclock command (actually it should be the same). But, the date command gets the date and time from the clock managed from Linux kernel. This in example, there is around 10 mins difference between them.

# date
Sat Aug 10 08:11:21 PDT 2013

2. Copy System Time to Hardware Time

You really want to make sure your hardware clock has the same date and time as the system clock.

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

So, use the hwclock command as shown below to set the hardware clock same as system clock. This is the typical use of the hwclock command.

# hwclock -w

# hwclock
Sat 10 Aug 2013 08:16:27 AM PDT  -0.625382 seconds

# date
Sat Aug 10 08:16:28 PDT 2013

Note: Both -w and –systohc option does the same. I like to use –systohc as it is easy to remember. –systohc stands for “system to hardware clock”, which copies the time from system to hardware clock.

# hwclock --systohc

3. Set Hardware Clock Date and Time Manually

Instead of copying the system date and time to the hardware clock, you can also manually set the value using –set and –date option as shown below.

The following will set the date to Aug 11th, 2013. Please note that if you don’t specify the time, it will be set to 12:00:00 AM.

# hwclock --set --date 8/11/2013

# hwclock
Sun 11 Aug 2013 12:00:04 AM PDT  -0.703489 seconds

You can also set both date and time at the same time as shown below. Please note that it will use the local time by default.

# hwclock --set --date "8/11/2013 23:10:45"

# hwclock
Sun 11 Aug 2013 11:10:48 PM PDT  -0.562862 seconds

You can also take the output of the date command, and pass it to the –set and –date option as shown below.

# hwclock --set --date "Sat Aug 10 08:31:24 PDT 2013"

# hwclock
Sat 10 Aug 2013 08:31:28 AM PDT  -0.594118 seconds

4. Copy Hardware Time to System Time

When your hardware clock shows the correct date and time, but not your system clock, you can use -s option as shown below to copy the hardware time to system time as shown below.

# 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

Note: Both -s and –hctosys option does the same. I like to use –hctosys as it is easy to remember. –hctosys stands for “hardware clock to system”, which copies the time from hardware clock to system clock.

# hwclock -hctosys

While testing this, if you’ve messed-up your system time, you can use the date command examples to set it to correct date and time.

5. hwclock Debug Mode

When you pass –debug option to the hwclock, it displays some debug information, which shows exactly what hwclock command does.

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.

The following shows exactly what it does when we copy hardware time to system time.

# hwclock -hctosys --debug
hwclock from util-linux-ng 2.17.2
Using /dev interface to clock.
Last drift adjustment done at 1375975224 seconds after 1969
Last calibration done at 1375975224 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:23:20
Hw clock time : 2013/08/10 15:23:20 = 1375975400 seconds since 1969
Calling settimeofday:
        tv.tv_sec = 1375975400, tv.tv_usec = 0
        tz.tz_minuteswest = 420

Note: On alpha machines you can use getepoch to view the epoch, setepoch to set the epoch. When you try to run this on a on Alpha machine, you’ll get the following error machine.

# hwclock --setepoch --epoch=1952
The kernel keeps an epoch value for the Hardware Clock only on an Alpha machine.
This copy of hwclock was built for a machine other than Alpha
(and thus is presumably not running on an Alpha now).  No action taken.

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

7. hwclock Test Mode

When you change things using hwclock, using –test option, you can execute the command without really making any changes.

The –systz option will change the system timezone accordingly. But, since we gave –test it will not really make the change, but it will still execute the command without any issues.

# hwclock --systz --test
Not setting system clock because running in test mode.

The best use of test mode is when you combine it with debug option. I.e When you want to see what the hwclock command does, but without making any changes.

# hwclock --systz --test --debug
hwclock from util-linux-ng 2.17.2
Last drift adjustment done at 1375975224 seconds after 1969
Last calibration done at 1375975224 seconds after 1969
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Current system time: 1375975586 = 2013/08/10 15:26:26
Calling settimeofday:
        UTC: 2013/08/10 15:26:26
        tv.tv_sec = 1375975586, tv.tv_usec = 556277
        tz.tz_minuteswest = 420
Not setting system clock because running in test mode.

The following shows exactly what the –adjust option does without really doing any adjustment.

# hwclock --adjust --test --debug
hwclock from util-linux-ng 2.17.2
Using /dev interface to clock.
Last drift adjustment done at 1375975224 seconds after 1969
Last calibration done at 1375975224 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:30:35
Hw clock time : 2013/08/10 15:30:35 = 1375975835 seconds since 1969
Time since last adjustment is 611 seconds
Need to insert -1 seconds and refer time back 0.978644 seconds ago
Needed adjustment is less than one second, so not setting clock.

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 17 comments… add one }

  • Deniz Gezmis August 13, 2013, 9:34 am

    Well structured and explained. Good job.

  • Jalal Hajigholamali August 13, 2013, 10:06 am

    Hi,

    Very nice and useful article….

    I sent this article to more than 100 students..

  • kamal babu August 13, 2013, 11:07 am

    Again good Job .

  • Mustapha Oldache August 13, 2013, 12:30 pm

    h!

    It could be helpful. Thanks!

  • Selvam August 13, 2013, 8:21 pm

    Explained well, its really helpful. Thanks!

  • Ponraj Kumar August 19, 2013, 12:23 am

    Hi,

    Its Excellent, Thanks you very much.

  • Edier October 31, 2013, 8:39 am

    Hi, thank you very much. This was too much help for me!
    God bless you.

  • Thamarai December 22, 2013, 11:12 pm

    Thank you

  • Guru February 6, 2014, 11:46 pm

    It’s Excellent and works like charm, thank you very much…

  • Narender March 12, 2014, 7:58 am

    Thanks for the post, very nice explanation

  • sal November 7, 2014, 10:58 am

    Yes, was able to set hardware clock to system time given your instructions. Thanks!

  • pavan April 14, 2015, 5:25 am

    The command hwclock -hctosys should be hwclock –hctosys.

  • prshant September 30, 2015, 10:31 am

    Hi,
    The hwclock command very very useful for me.

  • prshant September 30, 2015, 10:33 am

    Hi,
    The hwclock command very very useful for me.
    because it’s explanation solve my all conclusion of particular command.
    Thank you so much again.

  • Anonymous December 3, 2015, 6:10 am

    How to do this in shell. Date time comparison.
    For example I have a value “26/11/10 7:33 pm”. I need to check if it is between “25/11/10 8:30 pm” and “26/11/10 8:30 pm”.

  • Justin February 9, 2016, 4:34 am

    For some it might be important to run as root to get the command hwclock work otherwise you get “command not found”.

  • Chhaterpal Singh July 1, 2016, 4:48 am

    hi
    hwclock command very very useful

Leave a Comment