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.
| Clock | Alternative name | Persists while powered off | Primary role | Typical command |
|---|---|---|---|---|
| System clock | Software clock | No | Provides the running operating system's current time | date |
| Hardware clock | RTC | Yes, while its battery remains usable | Retains time across shutdown and helps initialize the system clock at boot | hwclock |
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]
| Field | Meaning | Valid form | Example segment |
|---|---|---|---|
MM | Month | Two digits from 01 through 12 | 09 |
DD | Day of the month | Two digits | 16 |
hh | Hour | Two digits using 24-hour time, from 00 through 23 | 19 |
mm | Minute | Two digits from 00 through 59 | 55 |
| Optional year | Year | Two-digit or four-digit year, in the supported position | 2014 |
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 option | Clock affected or read | Direction of change | Purpose |
|---|---|---|---|
date | System clock | Read | Display the current system date and time |
date -u | System clock | Read, displayed as UTC | Display the system-clock instant using UTC |
date --date="..." | No clock is changed | Calculation only | Evaluate a supplied date expression |
sudo date MMDDhhmm[[CC]YY] | System clock | Supplied value to system clock | Set the running system clock |
sudo hwclock | Hardware clock | Read | Display the RTC value |
sudo hwclock --set --date "..." | Hardware clock | Supplied value to RTC | Set the RTC directly |
sudo hwclock --systohc or sudo hwclock -w | Hardware clock | System clock to RTC | Write the current system-clock value into the hardware clock |
sudo hwclock --hctosys or sudo hwclock -s | System clock | RTC to system clock | Set 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
- Read both clocks with
dateandsudo hwclock. - Decide which value is authoritative. A trusted time service may be preferable to either manually observed value.
- Correct the authoritative clock first, using
datefor the system clock orhwclock --set --datefor the RTC. - Copy the corrected value in the required direction: use
hwclock --systohcfor system clock to RTC, orhwclock --hctosysfor RTC to system clock. - 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.