Linux命令之hwclock-查询和设置硬件时钟
公司测试部常常需要测试‘结算单催收单等等东东’,时常把服务器时间改来改去,平常⾃⼰定时备份的东西⽇期那真是天马⾏空,今后备份取时间都只敢⽤硬件时钟了。
⽤途说明
hwclock命令,与clock命令是同⼀个命令,主要⽤来查询和设置硬件时钟(query and set the hardware clock (RTC))。RTC=Real Time Clock,也就是硬件时钟。在Linux中有硬件时钟与系统时钟等两种时钟。硬件时钟是指主机板上的时钟设备,也就是通常可在BIOS画⾯设定的时钟。系统时钟则是指kernel中的时钟。所有Linux相关指令与函数都是读取系统时钟的设定。因为存在两种不同的时钟,那么它们之间就会存在差异。根据不同参数设置,hwclock命令既可以将硬件时钟同步到系统时钟,也可以将系统时钟同步到硬件时钟。
关于系统时钟与硬件时钟的关系的说法,⽹上分成了两种:
第⼀种:当Linux启动时,硬件时钟会去读取系统时钟的设置,然后系统时钟就会独⽴于硬件运作。
第⼆种:当Linux启动时,系统时钟会去读取硬件时钟的设定,之后系统时钟即独⽴运作。
到底那⼀种才是正确的呢?执⾏⼀下man hwclock可以看到⾥⾯有⼀段:
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.
这段⽂字说明,第⼆种说法是正确的,第⼀种说法是错误的。但⽹上⼤部分⽂章都是第⼀种说法,所以说,⽹络虽好,要注意甄别。
man hwclock 写道
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设置的时间就⽆效了。这⼤概就是为什么需要hwclock命令的原因吧。
常⽤参数
-r, --show        读取并打印硬件时钟(read hardware clock and print result )
linux在线命令查询平台
-s, --hctosys      将硬件时钟同步到系统时钟(set the system time from the hardware clock )
-w, --systohc    将系统时钟同步到硬件时钟(set the hardware clock to the current system time )
使⽤⽰例
⽰例⼀ hwclock命令与clock命令是⼀个东西
[root@new55 ~]# type -a hwclock
hwclock is /sbin/hwclock
hwclock is /usr/sbin/hwclock
[root@new55 ~]# ls -l /sbin/hwclock /usr/sbin/hwclock
-rwxr-xr-x 1 root root 34096 2010-01-07 /sbin/hwclock
lrwxrwxrwx 1 root root    18 08-13 00:14 /usr/sbin/hwclock -> ../../sbin/hwclock
[root@new55 ~]# type -a clock
clock is /sbin/clock
[root@new55 ~]# ls -l /sbin/clock
lrwxrwxrwx 1 root root 7 08-13 00:14 /sbin/clock -> hwclock
[root@new55 ~]#
⽰例⼆显⽰硬件时钟
[root@new55 ~]# hwclock
2010年11⽉06⽇星期六 21时09分28秒  -0.134840 seconds
[root@new55 ~]# hwclock -r
2010年11⽉06⽇星期六 21时09分33秒  -0.469123 seconds
[root@new55 ~]# hwclock --show
2010年11⽉06⽇星期六 21时09分45秒  -0.127548 seconds
[root@new55 ~]#
⽰例三修改⽇期并同步到硬件时钟
[root@new55 ~]# date
2010年 11⽉ 06⽇星期六 21:11:57 CST
[root@new55 ~]# date 11062112<== 格式 mmddHHMM,即⽉⽇时分
2010年 11⽉ 06⽇星期六 21:12:00 CST
[root@new55 ~]# hwclock -w
[root@new55 ~]# hwclock
2010年11⽉06⽇星期六 21时12分13秒  -0.648818 seconds
[root@new55 ~]#
注:要使系统时间准确,最好还是使⽤ntp⽅式。
⽰例四使⽤ntpdate命令从⽹络同步时间,再同步到硬件时钟
ntpdate命令是使⽤NTP协议来从⽹络同步时间的命令。NTP=Network Time Protocol ⽹络时间协议。[root@new55 ~]# ntpdate 0.p.org
6 Nov 21:17:55 ntpdate[4829]: step time server 123.146.124.28 offset -1.025258 sec
[root@new55 ~]# date
2010年 11⽉ 06⽇星期六 21:17:59 CST
[root@new55 ~]# hwclock -w
[root@new55 ~]#
问题思考

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。