hwclock命令,與clock命令是同一個命令,主要用來查詢和設(shè)置硬件時鐘(query and set the hardware clock (RTC))。RTC=Real Time Clock,也就是硬件時鐘。在Linux中有硬件時鐘與系統(tǒng)時鐘等兩種時鐘。硬件時鐘是指主機(jī)板上的時鐘設(shè)備,也就是通?稍贐IOS畫面設(shè)定的時鐘。系統(tǒng)時鐘則是指kernel中的時鐘。所有Linux相關(guān)指令與函數(shù)都是讀取系統(tǒng)時鐘的設(shè)定。因?yàn)榇嬖趦煞N不同的時鐘,那么它們之間就會存在差異。根據(jù)不同參數(shù)設(shè)置,hwclock命令既可以將硬件時鐘同步到系統(tǒng)時鐘,也可以將系統(tǒng)時鐘同步到硬件時鐘。
關(guān)于系統(tǒng)時鐘與硬件時鐘的關(guān)系的說法,網(wǎng)上分成了兩種:
第一種:當(dāng)Linux啟動時,硬件時鐘會去讀取系統(tǒng)時鐘的設(shè)置,然后系統(tǒng)時鐘就會獨(dú)立于硬件運(yùn)作。
第二種:當(dāng)Linux啟動時,系統(tǒng)時鐘會去讀取硬件時鐘的設(shè)定,之后系統(tǒng)時鐘即獨(dú)立運(yùn)作。
到底那一種才是正確的呢?執(zhí)行一下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.
這段文字說明,第二種說法是正確的,第一種說法是錯誤的。但網(wǎng)上大部分文章都是第一種說法,所以說,網(wǎng)絡(luò)雖好,要注意甄別。linux命令
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命令修改了系統(tǒng)時間,并不會自動去修改硬件時鐘,因此,當(dāng)系統(tǒng)下次重啟時,系統(tǒng)時鐘還會從硬件時鐘去取,date設(shè)置的時間就無效了。linux命令這大概就是為什么需要hwclock命令的原因吧。
-r, --show 讀取并打印硬件時鐘(read hardware clock and print result)
-s, --hctosys 將硬件時鐘同步到系統(tǒng)時鐘(set the system time from the hardware clock)
-w, --systohc 將系統(tǒng)時鐘同步到硬件時鐘(set the hardware clock to the current system time)