天天看點

我使用過的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.

這段文字說明,第二種說法是正确的,第一種說法是錯誤的。但網上大部分文章都是第一種說法,是以說,網絡雖好,要注意甄别。

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 )

[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 ~]# date 

2010年 11月 06日 星期六 21:11:57 CST

[root@new55 ~]# date 11062112     <== 格式 mmddHHMM,即 月日時分 

2010年 11月 06日 星期六 21:12:00 CST

[root@new55 ~]# hwclock -w 

2010年11月06日 星期六 21時12分13秒  -0.648818 seconds

注:要使系統時間準确,最好還是使用ntp方式。

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

2010年 11月 06日 星期六 21:17:59 CST

     本文轉自yzy121403725 51CTO部落格,原文連結:http://blog.51cto.com/lookingdream/1855438,如需轉載請自行聯系原作者

上一篇: HTTP詳解
下一篇: Nmap總結

繼續閱讀