Introduction to Linux hwclock tool (hwclock --systohc, hwclock --hctosys) hardware clock and system clock (hardware time, system time) (time synchronization)

hwclock tool introduction

hwclock is an important tool in Linux systems. It is mainly used to read and set the hardware clock. This article will have an in-depth discussion around hwclock --systohcand hwclock --hctosystwo commands.

Hardware clock and system clock

Before discussing hwclock in depth, you need to understand the difference between hardware clock and system clock.

  • Hardware clock (also known as real-time clock, RTC): It refers to an independent clock device on the computer motherboard. Even if the computer is turned off, it will continue to run as long as there is battery power.

  • System clock : Also known as the kernel clock or software clock, it is managed by the operating system. When the system starts, it reads the time from the hardware clock and then runs independently.

Therefore, the problem of synchronization between the two naturally arises. This is the main purpose of the hwclock command.

hwclock --systohc

command analysis

Use hwclock --systohcthe command to set the time of the system clock to the hardware clock. This is usually performed after the system clock has been changed manually or automatically adjusted via Network Time Protocol (NTP).

sudo hwclock --systohc

This command is usually run automatically when the system is shut down or restarted to ensure that the hardware clock has the latest system time the next time it is started.

Use cases and application scenarios

If an administrator manually changes the system time, for example, by setting a new time zone, hwclock --systohca command will need to be run to maintain the new time setting the next time the system starts.

hwclock --hctosys

command analysis

In hwclock --systohccontrast, hwclock --hctosysthe command gets the time from the hardware clock and sets it to the system clock.

sudo hwclock --hctosys

This is typically performed at system startup to ensure that the system clock is running from the most recent hardware clock time.

Use cases and application scenarios

If system power is suddenly lost, the system clock may lose the current time after the system is restarted. In this case, run hwclock --hctosysthe command to recover the time from the hardware clock.

hwclock command extension

Previously, we mainly discussed hwclock --systohcthe and hwclock --hctosystwo commands. Next, we will explore some other common commands of hwclock.

hwclock --show、hwclock -r

command analysis

Use hwclock --showthe or hwclock -rcommand to display the current hardware clock time.

sudo hwclock --show

or:

sudo hwclock -r

Insert image description here

This command can help you check the current status of the hardware clock to determine whether you need to synchronize the system clock.

Use cases and application scenarios

If you want to see the current time of the hardware clock instead of the system clock, you need to use this command.

hwclock --set / -s

command analysis

Use the hwclock --setor hwclock -scommand to manually set the time of the hardware clock.

sudo hwclock --set --date="2022-01-01 20:30:00"

This command sets the hardware clock to the specified date and time. Please note that this operation does not affect the system clock.

Use cases and application scenarios

If there is a problem with the hardware clock, for example, a dead battery causing time to be lost, then you may need to manually set the time on the hardware clock.

hwclock --adjust / -a

command analysis

hwclock --adjustor hwclock -acommand is used to adjust the hardware clock to reduce drift.

sudo hwclock --adjust

This command adjusts based on the historical drift rate since the hardware clock was last set or calibrated. On most systems, this command is run once a day, usually via a cron job.

Use cases and application scenarios

Because hardware clocks can drift, they need to be adjusted periodically to remain accurate. If your system doesn't do this automatically, you may need to do it manually.

in conclusion

The hwclock tool contains many powerful commands that allow us to precisely control the hardware clock. Understanding these commands and their uses is key to managing time on your Linux system.

References

  1. How to Set Time, Timezone and Synchronize System Clock Using Linux Command Line
  2. hwclock - RTC command line tool
  3. Dealing with Hardware and System Clock Time Skew
  4. Linux hwclock command
  5. Hardware clock and system clock
  6. Understand and use hwclock
  7. How To Sync Hardware Clock And System Clock In Linux

Guess you like

Origin blog.csdn.net/Dontla/article/details/132664647