hwclock Command in Linux



The hwclock is a robust command-line utility that allows you to interact with the hardware clock on your system. The hardware clock, also known as the Real-Time Clock (RTC), is a battery-backed clock that keeps track of the current time even when the computer is powered off.

Ensuring the hardware clock is accurate helps maintain consistent time across reboots, which is critical for logging, scheduled tasks, and time-sensitive applications. Besides that, synchronizing the hardware clock with NTP servers ensures your system time is accurate even after power outages or battery replacements.

In addition, during system maintenance, you might need to adjust or verify the hardware clock to ensure it matches the system clock or meets specific operational requirements.

Table of Contents

Here is a comprehensive guide to the options available with the hwclock command −

Syntax of hwclock Command

The following is the general syntax for the hwclock command −

hwclock [options]

hwclock Command Options

The following is a list of options available for the hwclock command, which gives you a range of controls for managing and troubleshooting the hardware clock on your system.

Options Description
-u, --utc / --localtime Indicates whether the Hardware Clock (RTC) is kept in Coordinated Universal Time (UTC) or local time. This option is how you inform hwclock about your preference, as the clock itself does not store this information.
--noadjfile Disables the use of the /etc/adjtime file, which hwclock uses for recording and correcting clock drift. When using this option, you must specify either --utc or --localtime.
--adjfile=filename Overrides the default /etc/adjtime file. This can be useful if you want to use a different adjustment file for managing clock drift.
-f, --rtc=filename Specifies a different RTC device file than the default. This is useful if your system's RTC is located at a different path, such as /dev/rtc0.
--directisa Relevant only for ISA machines or certain Alpha systems. It allows direct access to the hardware clock on these systems.
--badyear Used for systems with a known issue where the RTC reports an incorrect year. This option helps compensate for the problem, though a BIOS update is recommended.
--srm

This option is equivalent to --epoch=1900 and is used to specify the most common epoch on Alphas with SRM console.

Files cannot be extracted from a thin hwclockchive.

--arc This option is equivalent to --epoch=1980 and is used to specify the most common epoch on Alphas with ARC console (but Ruffians have epoch 1900).
--jensen, --funky-toy These options specify the type of Alpha machine. They are usually unnecessary because hwclock can detect the machine type if /proc is mounted.
--test Runs all operations except updating the hardware clock or other changes. This is useful for testing and debugging.
--debug Provides detailed output about hwclock's internal operations. Useful for understanding how the command works and for debugging.
-r, --show Read and display the current hardware clock time.
-s, --hctosys Set the system time

Examples of hwclock Command in Linux

In this section, we'll explore various practical examples of the hwclock command so that you can effectively manage the hardware clock and ensure your system's timekeeping is reliable and accurate.

Display the Current Hardware Clock Time

hwclock can show you the current time stored in the hardware clock. This is useful to check if the clock is correct and synchronized. To check the time, you can simply use the following commands −

Hwclock

Or,

hwclock -r

Or,

hwclock --show
hwclock Command in Linux1

Set the Hardware Clock to a Specified Time

You can manually set the hardware clock to a specific time. This might be necessary if the clock has drifted and you need to adjust it: To set it, you can simply use the following syntax −

hwclock --set --date="YYYY-MM-DD HH:MM:SS"

So if you want to set the hardware clock to November 6, 2024, 15:30:00, you would simply run −

sudo hwclock --set --date="2024-11-06 15:30:00"

This command ensures that your hardware clock is accurate, which helps maintain the correct system time, especially after reboots or power shutdowns.

hwclock Command in Linux2

Set the Hardware Clock to the System Time

You can synchronize the hardware clock with the current system time. This is useful to ensure both clocks are in sync. To set the hardware clock to the current system time, you can use the following command −

sudo hwclock --systohc

This command ensures that both the system and hardware clocks are in sync, which is particularly useful for maintaining accurate timekeeping across reboots and power outages.

hwclock Command in Linux3

Set the System Time from the Hardware Clock

You can also set the system time based on the hardware clock. This is typically done at boot time. To set the system time from the hardware clock, you can use the following command −

sudo hwclock --hctosys

This command is primarily useful during system startup to ensure that the system time is accurate and aligned with the hardware clock.

hwclock Command in Linux4

Adjust the Hardware Clock Drift

The drift adjustment process works by recording the difference between the hardware clock and the system clock over time. The hwclock command then uses this recorded drift information to correct the hardware clock, ensuring more accurate timekeeping.

For instance, if the hardware clock has been drifting forward by 5 seconds every day, the --adjust option will subtract 5 seconds from the hardware clock when the command is run, bringing it back in sync with the system time.

To adjust the hardware clock drift, you can use the --adjust option with the hwclock command −

sudo hwclock --adjust
hwclock Command in Linux5

Show Hardware Clock Time in UTC

To display the hardware clock time in Coordinated Universal Time (UTC), you can simply run −

sudo hwclock --utc

This is helpful if you are working in environments where UTC is the standard time reference.

hwclock Command in Linux6

Show Hardware Clock Time in Local Time

To display the hardware clock time in local time, you can run the following command −

sudo hwclock --localtime

This command displays the hardware clock time as local time, based on your system's time zone settings.

hwclock Command in Linux7

Conclusion

The hwclock is a vital command-line utility for system administrators to maintain the correct time across reboots and power outages.

By using hwclock, you can ensure accurate timekeeping on your system, which is essential for logging, scheduled tasks, and other time-sensitive applications. If you need more details or have further questions, feel free to reach out to us!

Advertisements