Article directory
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 --systohc
and hwclock --hctosys
two 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 --systohc
the 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 --systohc
a command will need to be run to maintain the new time setting the next time the system starts.
hwclock --hctosys
command analysis
In hwclock --systohc
contrast, hwclock --hctosys
the 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 --hctosys
the command to recover the time from the hardware clock.
hwclock command extension
Previously, we mainly discussed hwclock --systohc
the and hwclock --hctosys
two commands. Next, we will explore some other common commands of hwclock.
hwclock --show、hwclock -r
command analysis
Use hwclock --show
the or hwclock -r
command to display the current hardware clock time.
sudo hwclock --show
or:
sudo hwclock -r
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 --set
or hwclock -s
command 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 --adjust
or hwclock -a
command 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.