hwclock command in Linux with examples
Last Updated :
10 Oct, 2024
hwclock also called Real Time Clock (RTC), is a utility for accessing the hardware clock. The hardware clock is independent of the OS(operating system) you use and works even when the machine is shut down. The hardware clock is also called a BIOS clock. A user can also change the date and time of the hardware clock from the BIOS.
The hardware clock stores the values of the second, minute, hour, day, month, and year. The hwclock utility saves its settings in the “/etc/adjtime” file, which is created when a user makes the first change.
Syntax
hwclock [function] [option...]
Key Functions of the hwclock Command
1. -r, –show
Displays the current time stored in the hardware clock (RTC).
Example:
sudo hwclock -r
2. –get
Displays the drift-corrected RTC time.
Example:
sudo hwclock --get
3. –set
Sets the hardware clock to a specified date and time. You must provide the date using the –date option.
Example:
sudo hwclock --set --date="2024-10-10 15:45:00"
4. -s, –hctosys
Syncs the system time to match the hardware clock.
Example:
sudo hwclock -s
5. -w, –systohc
Updates the hardware clock to match the system clock. This is the reverse of –hctosys.
Example:
sudo hwclock -w
6. –systz
Sends the timescale configurations (like UTC or local time) to the kernel.
7. -a, –adjust
Adjusts the hardware clock to account for systematic drift based on the drift information stored in the “/etc/adjtime” file.
8. –predict
Predicts the drift-adjusted RTC time based on the given date.
Common Options in the hwclock Command
Option |
Description |
-u, --utc |
Shows that the RTC (Real Time Clock) timescale is set to UTC. |
-l, --localtime |
Shows that the RTC timescale is set to Local Time. |
-D, --debug |
Displays detailed debug information based on the demands of the hwclock command. |
-V, --version |
Displays the version information of the hwclock utility and exits. |
-h, --help |
Displays a help text with an overview of the available options and exits the command. |
Note: You should run the hwclock command with sudo privileges to make changes to the hardware clock or view the current RTC time.
Examples of Using the hwclock Command
1. To display the Hardware Clock Date and Time:
2. Using adjust function:
3. Using debug option to get the debug information:
Conclusion
The hwclock utility is an essential tool for managing hardware clock settings in Linux. If you’re adjusting for drift, syncing system time, or simply checking the current RTC time, mastering the hwclock command can help you maintain accurate and reliable timekeeping on your Linux machine. With easy-to-use functions and options, it’s a must-know utility for any Linux user or system administrator.
hwclock command in Linux – FAQs
What is the difference between the hardware clock and the system clock?
The hardware clock runs even when the system is off, while the system clock is managed by the operating system and is reset every time the machine is restarted.
Why do I need to adjust the hardware clock for drift?
Over time, hardware clocks can lose or gain time. Using the –adjust function, you can correct this systematic drift to ensure more accurate timekeeping.
Should I use UTC or local time for my hardware clock?
It’s generally recommended to set your hardware clock to UTC to avoid confusion when switching time zones or during daylight saving time changes.
What does the “/etc/adjtime” file do?
The “/etc/adjtime” file keeps track of any adjustments made to the hardware clock, including drift data, and stores the timescale (UTC or local time) used by the clock.