VMware ESXi and vSphere Cluster Management

Manage Linux System Time with date and hwclock

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

Linux keeps time using two related clocks: the system clock and the hardware clock. The system clock is maintained by the running kernel and is used by applications, logs, scheduled jobs, and most Linux commands. The hardware clock, also called the real-time clock (RTC), is maintained by the computer hardware and usually has a battery so it can retain time while the machine is powered off.

These commands can inspect or manually change either clock. Clock changes affect the whole system, so use them carefully and prefer network time synchronization for normal operation.

Linux Timekeeping Model

The system clock is a software clock maintained by the Linux kernel while the operating system is running. The date command normally displays this clock. Applications and services use it for timestamps, logs, authentication checks, file times, and scheduling.

The hardware clock is a battery-backed clock on the computer's hardware. It is also called the RTC. Unlike the system clock, it continues counting time while the machine is shut down, provided its battery and hardware are working correctly.

During a typical boot, Linux initializes the system clock using the RTC value. After boot, the system clock is the clock used most actively. The two clocks can drift apart unless they are synchronized.

ClockWhere it is maintainedPersists without powerPrimary commandTypical purpose
System clockLinux kernel and running operating systemNodateApplications, logs, scheduling, and normal time display
Hardware clockComputer hardware; the RTCUsually, using a batteryhwclockRetaining a time value across shutdown and boot

Viewing the System Clock with date

Run date without options to print the current system date and time in the configured local time zone:

date

To display the same system clock expressed as UTC, use -u:

date -u

Local time is the time shown according to the configured time zone. UTC, or Coordinated Universal Time, is a global reference used widely in administration, logging, and synchronization. Local time and UTC represent the same instant but can show different clock readings.

TaskCommand or optionTime basisNotes
Display current timedateLocal timeReads the running system clock.
Display current time in UTCdate -uUTCUseful when comparing timestamps across systems.
Set the system clocksudo date MMDDhhmmYYYYLocal time by defaultRequires administrative privileges on typical Linux systems.
Set the system clock using UTC semanticssudo date -u MMDDhhmmYYYYUTCUse only when the supplied value is intended as UTC.

Setting the System Clock with date

Many Linux implementations accept a compact numeric setting format:

MMDDhhmm[YY|YYYY]
  • MM: month, from 01 through 12
  • DD: day of the month
  • hh: hour using the 24-hour clock, from 00 through 23
  • mm: minute
  • YY or YYYY: optional year component, depending on the implementation and syntax accepted

Month, day, hour, and minute are required. The hour is not written using AM or PM. On typical systems, setting the clock requires sudo or another form of elevated privilege.

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

sudo date 091619552014

To interpret the supplied value as UTC instead of local time, add -u:

sudo date -u 091619552014

Calculating Relative Dates with GNU date

GNU date, the implementation provided by GNU coreutils, supports --date and its short form -d. These options calculate a date from a natural-language-style expression instead of changing the system clock.

date --date='next tue'
date --date='4 seconds ago'
date --date='2 month ago'
date --date='2 year ago'
date --date='2 years ago 50 days ago'
ExpressionMeaningExample command
next tueThe next named Tuesdaydate --date='next tue'
4 seconds agoA timestamp four seconds before nowdate --date='4 seconds ago'
2 month agoA date approximately two months in the pastdate --date='2 month ago'
2 year agoA date two years in the pastdate --date='2 year ago'
2 years ago 50 days agoMultiple relative offsets combineddate --date='2 years ago 50 days ago'

Relative-date interpretation is implementation-dependent. The examples in this section target GNU coreutils date; other operating systems or Unix implementations may support different syntax.

Viewing the Hardware Clock with hwclock

Run hwclock to read and display the RTC:

sudo hwclock

This reads the hardware clock rather than the ordinary running system clock. The displayed result can depend on whether the RTC is configured to store local time or UTC. Firmware setup, such as a BIOS or UEFI configuration screen, can also display or adjust the hardware clock.

Compare both clocks when investigating a mismatch:

date
date -u
sudo hwclock

A difference of a whole time-zone offset may indicate that local time and UTC have been confused, that the configured time zone is wrong, or that Linux is interpreting a local-time RTC as UTC or vice versa.

Setting the Hardware Clock with hwclock

Use --set together with --date to write a supplied date and time to the RTC. The date-time argument should be quoted so the shell passes it as one argument:

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

Changing the hardware clock requires administrative privileges. The exact interpretation of the value should agree with the RTC's local-time or UTC convention.

Command substitution can use GNU date to generate a timestamp for hwclock. The shell runs the inner command and replaces the substitution with its output:

sudo hwclock --set --date "$(date '+%m/%d/%Y %H:%M:%S')"

This example takes the current local system-clock value, formats it, and supplies that formatted value to hwclock. For a UTC-oriented workflow, generate and interpret the value consistently with the chosen RTC convention.

Synchronizing the System and Hardware Clocks

The direction of synchronization matters. The hwclock command provides explicit operations for copying one clock to the other.

CommandAliasSource clockDestination clockTypical use
hwclock --systohchwclock -wSystem clockHardware clockSave a corrected running system time into the RTC.
hwclock --hctosyshwclock -sHardware clockSystem clockInitialize or recover system time from the RTC when no network source is available.

After manually correcting the running system clock, write it to the RTC with:

sudo hwclock --systohc

When the RTC contains the authoritative value and Linux needs to use it, copy it into the system clock with:

sudo hwclock --hctosys

Do not reverse these commands accidentally: systohc means system clock to hardware clock, while hctosys means hardware clock to system clock.

Time Synchronization in Normal Operations

Manual clock changes are generally temporary or intended for troubleshooting. For long-term accuracy, Linux systems normally use the Network Time Protocol (NTP), which synchronizes clocks with network time sources.

Common modern time-synchronization services include systemd-timesyncd and chrony. This lesson does not configure those services, but you should know that an active time-sync service may overwrite a manual adjustment. Before making a lasting manual change, determine whether network synchronization is running and decide which source should be authoritative.

Correct timekeeping is especially important for at, the command used to schedule a one-time future job, as well as recurring schedulers such as cron and systemd timers. A wrong time zone, an incorrect clock, or a clock adjustment after scheduling can cause a job to run earlier, later, or unexpectedly.

Troubleshooting Clock Problems

The time differs by a whole time-zone offset

  • Compare date with date -u.
  • Verify the intended time zone separately from the clock value.
  • Confirm whether the RTC stores local time or UTC before using hwclock.

The time changes back after a manual correction

  • Check whether an NTP-based service is actively adjusting the system clock.
  • Choose whether network synchronization or the manual value is authoritative.
  • Pause or configure the synchronization service appropriately before testing a manual adjustment.

The time is correct while running but wrong after reboot

  • Compare date and sudo hwclock.
  • After correcting the system clock, use sudo hwclock --systohc when appropriate.
  • If the RTC repeatedly loses time, inspect BIOS or UEFI clock settings and consider a failing RTC battery or hardware problem.

A clock-setting command is rejected

  • Use sudo where you are authorized to perform administrative operations.
  • Check the numeric field order, digit count, valid calendar date, and 24-hour time values.
  • Remember that flexible relative expressions target GNU date; consult the local manual page on non-GNU systems.

Scheduled tasks run at an unexpected time

  • Verify both local time and UTC before scheduling.
  • Check the configured time zone.
  • Review at jobs and recurring scheduler entries after a significant clock change.

Key Exam and Administration Notes

  • System clock: the kernel-maintained software clock used while Linux is running.
  • Hardware clock or RTC: the battery-backed hardware clock that normally persists through shutdown.
  • Boot relationship: Linux commonly initializes the system clock from the RTC.
  • date: displays or, with appropriate privileges, sets the system clock.
  • date -u: displays or sets using UTC semantics.
  • hwclock: reads or changes the RTC.
  • --systohc / -w: copies system time to the hardware clock.
  • --hctosys / -s: copies hardware-clock time to the system clock.
  • NTP: the preferred long-term method for maintaining accurate time.

For a related reference, see Linux date and hwclock commands.