Hwclock

From LQWiki
Jump to navigation Jump to search

hwclock

The hardware clock in Linux

hwclock is the program used within Linux to read and control the hardware clock, also known as the real-time clock or rtc. It replaces an earlier program called clock.

In Linux there are actually two clocks: the hardware clock and the system clock, which is part of the kernel. Windows has no system clock and it is the hardware clock that provides all timestamps on files. For this reason, the hardware clock on a machine that runs Windows must always be set to local time. In Linux, the hardware clock is used only to set the system clock to the correct time at boot. Thereafter all timestamps are provided by the system clock, which uses the tick rate of your cpu as its time-giver. The system clock is actually more accurate than the hardware clock, hence the long-standing Linux tradition of resetting the hardware clock from the system clock at shut-down. On a Linux-only box, it is usual to set the hardware clock to UTC (universal time or Greenwich Mean Time). The system clock always uses local time.

Common options for hwclock

For a full list of options and sub-commands, see the hwclock man page. The commonest sub-commands used are:

  • --hctosys (-s) sets the system clock by the hardware clock (usually called at boot)
  • --systohc (-w) sets the hardware clock by the system clock (usually called at shutdown)
  • --show (-r) shows the time on the hardware clock
  • --set sets the hardware clock to a different time.

It is important to understand that the --show command does not normally show the actual time on the clock. It corrects that time to local time using the /etc/localtime file, which is usually a link to the actual timezone file in /usr/share/zoneinfo. The same correction is made when setting the system clock by the hardware clock. To see what the clock is actually showing, you need to use the -D or --debug option.

How does hwclock know whether the hardware clock is showing utc or local time? It needs this information after all to know whether the clock needs to be corrected for the time zone. You can, if you want, provide this information explicitly by using the options --utc or --localtime with your hwclock command. Otherwise, hwclock will look in the /etc/adjtime file, which records the most recently specified alternative.