Linux 下用 hwclock 调节系统时钟

由 Linux 内核管理的时钟与硬件时钟并不相同。硬件时钟又叫 BIOS 时钟,在系统关闭后仍在运行,可以通过修改 BIOS 修改硬件时钟。不过当系统已经启动之后,仍可通过 hwclock 命令查看并修改硬件时间。

1.显示系统日期和时刻

输入 hwclock 即可查看硬件时钟,默认选项是 -r 或者 --show。

# hwclock
Fri 16 Aug 2013 04:45:28 PM CST  -0.428900 seconds
# hwclock -r
Fri 16 Aug 2013 04:45:35 PM CST  -0.063468 seconds
# hwclock --show
Fri 16 Aug 2013 04:45:42 PM CST  -0.719671 seconds

hwclock 得到的时间和 date 命令得到的时间可能相同,也可能不同,因为来源不同。
# date
Fri Aug 16 16:47:30 CST 2013

在本人的机器上,有大约两分钟的差别,内核时钟与北京时间吻合,硬件时钟略微有点慢。

2.将系统时间复制到硬件时间

当然,系统时间和硬件时间相等是满足强迫症患者的必要条件。而它们的差别的确是存在的。
我们需要的选项是 --systohc
# hwclock --systohc
# hwclock
Fri 16 Aug 2013 04:54:23 PM CST  -0.438495 seconds
# date
Fri Aug 16 16:54:24 CST 2013

我们看到,差别已经消失了,相应的短选项是 -w,不过这个非常难记,长选项意思很明确:SYStem TO Hardware Clock,非常容易记忆。

3.手动调节硬件时钟

手动调节主要使用 --set 命令和 --date 选项。
# hwclock --set --debug --date 8/16/2013                                     
Issuing date command: date --date="8/16/2013" +seconds-into-epoch=%s
response from date command = seconds-into-epoch=1376582400

date string 8/16/2013 equates to 1376582400 seconds since 1969.
hwclock from util-linux 2.20.1
Using /dev interface to clock.
Last drift adjustment done at 1376643258 seconds after 1969
Last calibration done at 1376643258 seconds after 1969
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
...got clock tick
Time read from Hardware Clock: 2013/08/16 10:20:49
Hw clock time : 2013/08/16 10:20:49 = 1376648449 seconds since 1969
Time elapsed since reference time has been 0.318806 seconds.
Delaying further to reach the new time.
Setting Hardware Clock to 16:00:00 = 1376582400 seconds since 1969
ioctl(RTC_SET_TIME) was successful.
Not adjusting drift factor because it has been less than a day since the last calibration.
这里用到了 --debug 选项,后面详细说。
hwclock 命令的 --date 选项还与 date 命令输出格式兼容。
# hwclock --debug --set --date "`date`"
Issuing date command: date --date="Fri Aug 16 18:24:44 CST 2013" +seconds-into-epoch=%s
response from date command = seconds-into-epoch=1376648684

date string Fri Aug 16 18:24:44 CST 2013 equates to 1376648684 seconds since 1969.
hwclock from util-linux 2.20.1
Using /dev interface to clock.
Last drift adjustment done at 1376648658 seconds after 1969
Last calibration done at 1376648658 seconds after 1969
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
...got clock tick
Time read from Hardware Clock: 2013/08/16 10:24:44
Hw clock time : 2013/08/16 10:24:44 = 1376648684 seconds since 1969
Time elapsed since reference time has been 0.428459 seconds.
Delaying further to reach the new time.
Setting Hardware Clock to 10:24:44 = 1376648684 seconds since 1969
ioctl(RTC_SET_TIME) was successful.
Not adjusting drift factor because it has been less than a day since the last calibration.
上面的命令相当于 --systohc 选项。
还有一种格式为:
# hwclock --set --date "8/11/2013 23:10:45"
注意,这种方式调整时间后默认为本地时间(local time)而不是 UST。

4.将硬件时钟复制到系统时钟

基本命令和上面相似,长选项是 --hctosys,相应的短选项是 -s。

5.回过头来说说调试模式

如果加上 --debug 选项,程序将输出一些调试信息,这些调试信息表明了程序究竟做了什么。例子见上面的 shell 输出,更多的例子在下面两节。

6.调节硬件时钟

系统启动的时候会读取硬件时钟,/etc/adjtime 文件被 hwclock 的 --adjust 选项调用以控制调节过程,先看看该文件:
# cat /etc/adjtime
0.380822 1376648684 0.000000
1376648684
UTC
第一行有三个数字,分别为:系统漂移率(时钟每天产生的偏差),从最近一次调整算起回溯到 1969 UTC 的总秒数,一个零(没有物理意义);第二行是第一行的第二个数字;第三行是 UTC 或者是 local。
# hwclock --debug --adjust
hwclock from util-linux 2.20.1
Using /dev interface to clock.
Last drift adjustment done at 1376648684 seconds after 1969
Last calibration done at 1376648684 seconds after 1969
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
...got clock tick
Time read from Hardware Clock: 2013/08/16 10:44:58
Hw clock time : 2013/08/16 10:44:58 = 1376649898 seconds since 1969
Time since last adjustment is 1214 seconds
Need to insert 0 seconds and refer time back 0.005351 seconds ago
Needed adjustment is less than one second, so not setting clock.
上面是一个典型的例子,仔细读一下后,--adjust 选项的作用还是很明显的。

7.测试模式

由于操作系统时间是个比较敏感的问题,搞乱了内核时钟或是硬件时钟都会惹来麻烦,所以 hwclock 增加了 --test 选项。
# hwclock --systz --test
Not setting system clock because running in test mode.
也就是说程序执行了,但是没有真正改动任何数据,所以总要加上 --debug 选项来输出程序的行为。
# hwclock --debug --test --systz
hwclock from util-linux 2.20.1
Last drift adjustment done at 1376648684 seconds after 1969
Last calibration done at 1376648684 seconds after 1969
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Current system time: 1376650362 = 2013/08/16 10:52:42
Calling settimeofday:
	UTC: 2013/08/16 10:52:42
	tv.tv_sec = 1376650362, tv.tv_usec = 932224
	tz.tz_minuteswest = -480
Not setting system clock because running in test mode.
从上面的输出可以看出 --systz 选项自动调节系统时区,有时候看 man page 看的云里雾里的时候可以用 --test 做个实验,一切就简单了。
hwclock 还有很多功能和参数,上面几个是主要的,其他的可以用 --test 玩一下就可以明白其功用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值