hwclock命令,与clock命令是同一个命令,主要用来查询和设置硬件时钟(query and set the hardware clock (RTC))。RTC=Real Time Clock,也就是硬件时钟。在Linux中有硬件时钟与系统时钟等两种时钟。硬件时钟是指主机板上的时钟设备,也就是通常可在BIOS画面设定的时钟。系统时钟则是指kernel中的时钟。所有Linux相关指令与函数都是读取系统时钟的设定。因为存在两种不同的时钟,那么它们之间就会存在差异。根据不同参数设置,hwclock命令既可以将硬件时钟同步到系统时钟,也可以将系统时钟同步到硬件时钟。
关于系统时钟与硬件时钟的关系的说法,网上分成了两种:
第一种:当Linux启动时,硬件时钟会去读取系统时钟的设置,然后系统时钟就会独立于硬件运作。
第二种:当Linux启动时,系统时钟会去读取硬件时钟的设定,之后系统时钟即独立运作。
到底那一种才是正确的呢?执行一下man hwclock可以看到里面有一段:
The System Time is the time that matters. The Hardware Clock’s basic purpose in a Linux system is to keep time
when Linux is not running. You initialize the System Time to the time from the Hardware Clock when Linux
starts up, and then never use the Hardware Clock again. Note that in DOS, for which ISA was designed, the
Hardware Clock is the only real time clock.
这段文字说明,第二种说法是正确的,第一种说法是错误的。但网上大部分文章都是第一种说法,所以说,网络虽好,要注意甄别。linux命令
It is important that the System Time not have any discontinuities such as would happen if you used the date(1L)
program to set it while the system is running. You can, however, do whatever you want to the Hardware Clock
while the system is running, and the next time Linux starts up, it will do so with the adjusted time from the
Hardware Clock.
如果使用date命令修改了系统时间,并不会自动去修改硬件时钟,因此,当系统下次重启时,系统时钟还会从硬件时钟去取,date设置的时间就无效了。linux命令这大概就是为什么需要hwclock命令的原因吧。
-r, --show 读取并打印硬件时钟(read hardware clock and print result)
-s, --hctosys 将硬件时钟同步到系统时钟(set the system time from the hardware clock)
-w, --systohc 将系统时钟同步到硬件时钟(set the hardware clock to the current system time)