LinuxCommandLibrary

hwclock

Manage the hardware clock

TLDR

Display the current time as reported by the hardware clock

$ hwclock
copy

Write the current software clock time to the hardware clock (sometimes used during system setup)
$ hwclock [[-w|--systohc]]
copy

Write the current hardware clock time to the software clock
$ hwclock [[-s|--hctosys]]
copy

SYNOPSIS

hwclock [options]

PARAMETERS

--show
    Display the current time of the hardware clock.

--systohc
    Set the hardware clock from the current system time.

--hctosys
    Set the system time from the hardware clock.

--set
    Set the hardware clock to the time specified by the --date option. This requires superuser privileges.

--date="YYYY-MM-DD HH:MM:SS"
    Specifies the new time to set the hardware clock to, used in conjunction with --set.

--utc
    Tells hwclock that the hardware clock is set to Coordinated Universal Time (UTC). This is the recommended setting for Linux systems.

--localtime
    Tells hwclock that the hardware clock is set to local time. This is often used in dual-boot systems where another OS (e.g., Windows) expects the RTC to be in local time.

--adjust
    Adjust the hardware clock for systematic drift. This requires a drift file (e.g., /etc/adjtime) that hwclock maintains to track the clock's inaccuracy.

--rtc=dev
    Specify the RTC device to use (e.g., /dev/rtc0). If not specified, hwclock attempts to find one automatically.

DESCRIPTION

hwclock is a utility for accessing and managing the system's Real Time Clock (RTC), also known as the hardware clock.

The RTC is a persistent clock that runs even when the system is powered off, typically backed by a small battery. The system clock, on the other hand, is a software clock maintained by the kernel and can be easily adjusted.

hwclock's primary functions include reading the time from the RTC to set the system clock (commonly performed at boot), or writing the current system time back to the RTC (often done at shutdown or periodically). It is crucial for maintaining accurate time across reboots, especially in systems without constant network time synchronization (NTP).

CAVEATS

When Network Time Protocol (NTP) or similar services (like chrony) are running, they actively manage the system clock. Using hwclock --systohc while NTP is active can interfere with its operation, as NTP is designed to keep the system clock highly accurate.

The RTC relies on a small battery; if this battery dies, the RTC time will become unreliable.

The choice between --utc and --localtime is critical. Misconfiguration can lead to incorrect time after a reboot, especially during Daylight Saving Time transitions.

SYSTEM CLOCK VS. HARDWARE CLOCK (RTC)

The System Clock is a software clock maintained by the Linux kernel. It starts from 0 at boot and is typically synchronized with the RTC at startup, or continuously updated by network time services (NTP/chrony). The Hardware Clock (RTC) is an independent, battery-backed chip on the motherboard that keeps time even when the system is powered off. hwclock interacts directly with the RTC.

TIME SYNCHRONIZATION AT BOOT/SHUTDOWN

Traditionally, at system boot, hwclock --hctosys is run to set the system clock from the RTC. Conversely, at system shutdown, hwclock --systohc is often executed to save the current, accurate system time back to the RTC, ensuring the RTC remains synchronized.

UTC VS. LOCAL TIME CONFIGURATION

It is generally recommended practice for the hardware clock (RTC) to be set to Coordinated Universal Time (UTC). This avoids issues with Daylight Saving Time (DST) changes. The Linux kernel then calculates the local time based on the RTC's UTC time and the system's configured timezone. If the system is dual-booting with an operating system (like Windows) that expects the RTC to be set to local time, special consideration (e.g., configuring Linux to treat RTC as local time or configuring Windows to use UTC) is needed to prevent time discrepancies.

HISTORY

hwclock has been a fundamental utility in Linux systems for managing the Real Time Clock (RTC) since early versions. Its primary role has been to synchronize the hardware clock with the software system clock, especially during boot and shutdown, ensuring time persistence across reboots. In modern Linux distributions that use systemd, timedatectl has largely superseded hwclock for general time management, offering a more integrated and user-friendly approach. However, hwclock still serves its purpose for specific RTC operations or on systems that do not use systemd.

SEE ALSO

date(1), timedatectl(1), ntpd(8), chronyd(8)

Copied to clipboard