How to Use the hwclock Command to Sync the Hardware Clock in Linux

What is the hwclock Command?

Every Linux computer actually keeps track of two entirely separate clocks: the System Clock (managed by the Linux kernel and kept accurate via network time protocols like NTP) and the Hardware Clock (the physical Real-Time Clock or RTC chip on the motherboard, powered by a tiny CMOS battery).

While the system clock is highly accurate while the computer is running, it ceases to exist the moment the server is powered off. During a reboot, the motherboard’s hardware clock provides the initial time to the Linux kernel. If the hardware clock drifts and becomes inaccurate over several years, your server will boot up with the wrong time, causing SSL certificate failures, broken database timestamps, and failed cron jobs before the network time daemon has a chance to correct it.

The hwclock command allows system administrators to read, query, and synchronize the physical hardware clock directly from the Linux terminal.

Step 1: Read the Hardware Clock

To view the current time stored on the motherboard’s physical RTC chip, open your terminal and run the command with the -r (read) or --show flag. Because interacting directly with hardware requires root privileges, you must use sudo:

sudo hwclock -r

The output will display the date, time, and the timezone offset. Compare this output to the standard date command (which shows the software System Clock) to see if there is a significant discrepancy between the two.

date

Step 2: Sync the Hardware Clock to the System Clock

If your Linux system clock is perfectly accurate (because it is actively syncing with an NTP server over the internet), but your hardware clock is five minutes behind, you need to push the correct system time down to the motherboard.

Use the -w (write) or --systohc (System to Hardware Clock) flag:

sudo hwclock -w

This command takes the highly accurate software time and overwrites the time stored on the physical CMOS chip. This is the most common use of the hwclock command and ensures your server will boot with the correct time during its next restart.

Step 3: Sync the System Clock to the Hardware Clock

In rare scenarios, such as working on an air-gapped machine with no internet access and a failing software clock, you might need to do the reverse: pull the time from the physical motherboard and force the Linux kernel to use it.

Use the -s (set) or --hctosys (Hardware Clock to System) flag:

sudo hwclock -s

This command grabs the time from the RTC chip and overwrites the current Linux system time.

Step 4: Check if the Clock uses UTC or Local Time

Historically, Windows machines store the physical hardware clock time in Local Time, while Linux and macOS strongly prefer to store the hardware clock time in UTC (Coordinated Universal Time). If you are dual-booting Linux and Windows on the same hardware, they will constantly fight over the hardware clock, causing your time to be wildly incorrect after every reboot.

You can force hwclock to assume the motherboard is storing Local Time (to pacify Windows) by using the --localtime flag:

sudo hwclock --systohc --localtime

However, for dedicated Linux servers, it is always recommended to ensure the hardware clock runs strictly in UTC.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.