我使用過的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 )
  -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.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要將時鐘分爲系統時鐘和硬件時鐘,這樣做有什麼好處?


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章