Linux online course

Linux date and hwclock Commands: System and Hardware Clock Management

Learn how Linux system and hardware clocks differ, and use date and hwclock to view, set, calculate, and synchronize time.

Linux uses two related clocks: a system clock maintained by the running kernel and a battery-backed hardware clock, also called the real-time clock (RTC). The date command works primarily with the system clock, while hwclock reads and modifies the RTC.

This distinction matters because changing one clock does not automatically change the other. You must explicitly synchronize them when necessary.

The Two Linux Clocks

System clock

The system clock is the time value maintained by the Linux kernel while the operating system is running. Programs, logs, scheduled tasks, and most user-facing time displays use this clock.

Hardware clock or RTC

The hardware clock is a small, battery-powered clock on the computer's motherboard. It continues keeping time while the machine is shut down, so it can provide an initial time during the next boot. Firmware setup, sometimes called BIOS or UEFI setup, can usually display or adjust this clock as well.

ClockAlternative namePersists while powered offPrimary roleTypical command
System clockSoftware clockNoProvides the running operating system's current timedate
Hardware clockRTCYes, while its battery remains usableRetains time across shutdown and helps initialize the system clock at boothwclock

During a usual startup, Linux initializes the system clock from the hardware clock. After boot, the kernel advances the system clock independently. If either clock is inaccurate, the other clock is not corrected automatically merely because it was read.

Viewing System Time with date

Run date without an option to print the current system date and time:

date

The output represents the system clock and is normally rendered in the host's configured local time zone. Local time means the time shown after applying that time zone's offset and daylight-saving rules, when applicable.

Use -u to display the same system-clock instant in UTC:

date -u

UTC, or Coordinated Universal Time, is a global time basis rather than a local time-zone presentation. For example, a local display might show an evening hour while date -u shows a different hour for the same instant.

Setting the System Clock with date

On typical Linux systems, changing the system clock requires administrative privileges. The traditional compact input format is:

sudo date MMDDhhmm[[CC]YY]
FieldMeaningValid formExample segment
MMMonthTwo digits from 01 through 1209
DDDay of the monthTwo digits16
hhHourTwo digits using 24-hour time, from 00 through 2319
mmMinuteTwo digits from 00 through 5955
Optional yearYearTwo-digit or four-digit year, in the supported position2014

For example, this sets the system clock to September 16, 2014 at 19:55 in the local time interpretation:

sudo date 091619552014

The value is read as MM DD hh mm YYYY: month 09, day 16, hour 19, minute 55, and year 2014. Hours must use the 24-hour convention; use 19, not 7 PM.

By default, the entered value is interpreted according to the system's local time-zone rules. Use -u when the supplied value is intended to be UTC:

sudo date -u MMDDhhmm[[CC]YY]

Calculating Other Dates with GNU date

GNU date can calculate a date without changing the system clock. The --date option supplies an expression to interpret:

date --date="next tue"

This prints the date and time corresponding to the next Tuesday according to GNU date's parser. Relative expressions can use units such as seconds, months, and years:

date --date="4 seconds ago"
date --date="2 months ago"
date --date="2 years ago 50 days ago"

The final example combines two relative clauses in one calculation. It asks for a time two years and fifty days before the reference time. Quoting is important because the expression contains spaces, allowing the shell to pass it as one argument.

These calculations print a result; they do not modify the system clock. Relative-date syntax is implementation-specific. The examples use GNU date syntax commonly found on Linux distributions, but another operating system or a non-GNU implementation may accept different options and expressions.

Viewing the Hardware Clock with hwclock

Use hwclock to read the RTC rather than the running system clock:

sudo hwclock

The displayed value may differ from date because the two clocks can be out of sync or because they are being interpreted with different UTC or local-time assumptions. Hardware-clock access often requires elevated privileges.

The RTC can also be viewed or adjusted through firmware setup. The hwclock command provides command-line access from Linux, which is useful for scripts and administration.

Manually Setting the Hardware Clock

Use --set together with --date to write a supplied value directly to the RTC:

sudo hwclock --set --date "09/12/2014 20:32:45"

The value uses a month/day/year and hour:minute:second style: MM/DD/YYYY HH:MM:SS. The quotes are required or strongly recommended because the value contains a space; without them, the shell would split the date and time into separate arguments.

This command changes the hardware clock only. It does not, by itself, set the current Linux system clock. Conversely, using date to set the system clock does not automatically write that value to the RTC.

Synchronizing the Two Clocks

hwclock provides two operations with opposite directions:

Command or optionClock affected or readDirection of changePurpose
dateSystem clockReadDisplay the current system date and time
date -uSystem clockRead, displayed as UTCDisplay the system-clock instant using UTC
date --date="..."No clock is changedCalculation onlyEvaluate a supplied date expression
sudo date MMDDhhmm[[CC]YY]System clockSupplied value to system clockSet the running system clock
sudo hwclockHardware clockReadDisplay the RTC value
sudo hwclock --set --date "..."Hardware clockSupplied value to RTCSet the RTC directly
sudo hwclock --systohc or sudo hwclock -wHardware clockSystem clock to RTCWrite the current system-clock value into the hardware clock
sudo hwclock --hctosys or sudo hwclock -sSystem clockRTC to system clockSet the running system clock from the hardware clock

System clock to hardware clock

Use --systohc, also written as -w, when the system clock is authoritative and the RTC should be updated:

sudo hwclock --systohc
sudo hwclock -w

Hardware clock to system clock

Use --hctosys, also written as -s, when the RTC contains the value that should initialize or replace the running system clock:

sudo hwclock --hctosys
sudo hwclock -s

A safe synchronization workflow

  1. Read both clocks with date and sudo hwclock.
  2. Decide which value is authoritative. A trusted time service may be preferable to either manually observed value.
  3. Correct the authoritative clock first, using date for the system clock or hwclock --set --date for the RTC.
  4. Copy the corrected value in the required direction: use hwclock --systohc for system clock to RTC, or hwclock --hctosys for RTC to system clock.
  5. Read both clocks again and verify the result.

Troubleshooting

date and hwclock show different times

The clocks may be out of sync, or one display may be local time while the other is being treated as UTC. Compare date with date -u, decide which clock is correct, and then use the appropriate synchronization direction.

A setting command is denied

Clock changes usually require administrative privileges. Run the command with sudo or use the administrative mechanism appropriate for the system, provided you are authorized to do so.

The displayed hour is offset

Check whether the intended value is local time or UTC. The local-time display depends on the configured time zone, while date -u explicitly displays UTC. Mixing these interpretations can produce an apparent hour offset.

A compact value is interpreted incorrectly

Check the order MMDDhhmm, place the optional year in the supported position, and use 24-hour hours from 00 through 23.

The machine has the wrong time after reboot

The RTC may retain an inaccurate value and supply it at startup. Correct the chosen authoritative clock, then synchronize the RTC with sudo hwclock --systohc when the system clock is correct.

A relative expression does not work

Relative parsing differs between implementations. Consult the local date manual page and use syntax supported by that platform. GNU date syntax is expected for the relative examples in this lesson.

Quick Reference

# Current local system time
date

# Current system time displayed as UTC
date -u

# Set system time: September 16, 2014, 19:55 local time
sudo date 091619552014

# Calculate relative dates
date --date="next tue"
date --date="4 seconds ago"
date --date="2 months ago"
date --date="2 years ago 50 days ago"

# Read the RTC
sudo hwclock

# Set the RTC directly
sudo hwclock --set --date "09/12/2014 20:32:45"

# Copy system clock to RTC
sudo hwclock --systohc
sudo hwclock -w

# Copy RTC to system clock
sudo hwclock --hctosys
sudo hwclock -s

For broader Linux command-line practice, see Linux, Show the Full Path of Shell Commands, and Bourne Again Shell Bash.