您的位置:首页 > 运维架构 > Linux

Linux命令之hwclock - 查询和设置硬件时钟--补充

2012-05-02 10:34 513 查看
prothes:以下是转载,最后有我的补充 我使用过的Linux命令之hwclock - 查询和设置硬件时钟 本文链接:/article/4053419.html (转载请注明出处)
用途说明
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 )
-s, --hctosys 将硬件时钟同步到系统时钟(set the system time from the hardware clock )
-w, --systohc 将系统时钟同步到硬件时钟(set the hardware clock to the current system time )
-u --utc the hardware clock is kept in UTC
--localtime the hardware clock is kept in local 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,即 月日时分 《年份呢?下面介绍--prothes》
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.rhel.pool.ntp.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 ~]#
问题思考 1. 为什么Linux要将时钟分为系统时钟和硬件时钟,这样做有什么好处?
------------------------prothes 补充------------------------------
对于linux来讲机器里有两个时钟:硬件时钟和系统时钟
硬件时钟从根本上讲是CMOS时钟或者说是RTC IC的时钟,只要是IC供电正常就在运行,与linux的更新修改无关;系统时钟是由内核维护的,它是从1969年末(即传说中的标志Unix时代开端的那个拂晓)开始算起的累积秒数。在Linux系统中,起作用的是系统时钟。在启动时,它靠读取硬件时钟获得计时起点,当然可以通过linux修改RTC IC的时钟。
在DOS或Mac系统中,起作用的是硬件时钟就是 RTC IC。是由小型电池供电的警报器时钟,这种锂电池一般可持续供电三年左右,那时候你系统各大块差不多都已经过时了。
在Linux中可以是使用shell命令:hwclock来设置硬件时钟,hwclock就会获取你的时钟推移速率,然后就会把这个信息存在/etc/adjtime里边,可以用下面一条命令来随时更新硬件时间:hwcolck --adjust,这是最后手动更新的时间的命令。或者使用:
hwclock --adjfile=path specifies the path to the adjust file (default is /etc/adjtime)
查看硬件时钟用命令:

hwclock --show 或者 hwclock -r

重置硬件时钟用:
#date 050809522012.20 //设置系统时钟 2012 年 05 月 8日 星期四 09:52:20CST
#hwclock -w
#date 050809522012.20 结构是:mmddhhmmyy.ss 月月日日时时分分年年年年.秒秒
如果是要修改时区可以使用txset或者使用:ln -s /etc/localtime /usr/share/zoneinfo/US/Pacific

测试通过:
#date 050809522012.20 //设置系统时钟 2012 年 05 月 8日 星期四 09:52:20CST
#hwclock -w //将系统时钟写入硬件RTC IC时钟
#hwclock -r //看时间是否在变化
如果没有出错, 就已经把2012 年 05 月 8日 星期四 09:52:20 CST 写入RTC IC了

为了使系统时间和RTC时间同步,可以在初始化文件中添加命令
hwclock –s
使每次开机时读取RTC时间,并同步给系统时间。
在根文件系统:usr/etc/rc.local中添加
/sbin/hwclock -s
可以断电测试看看

本文出自 “prothes blog” 博客,请务必保留此出处http://prothesman.blog.51cto.com/8610862/1411569
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: