本文鏈接:http://codingstandards.CUOxin.com/blog/804830 (轉(zhuǎn)載請注明出處)
hwclock命令,與clock命令是同一個(gè)命令,主要用來查詢和設(shè)置硬件時(shí)鐘(query and set the hardware clock (RTC))。RTC=Real Time Clock,也就是硬件時(shí)鐘。在Linux中有硬件時(shí)鐘與系統(tǒng)時(shí)鐘等兩種時(shí)鐘。硬件時(shí)鐘是指主機(jī)板上的時(shí)鐘設(shè)備,也就是通常可在BIOS畫面設(shè)定的時(shí)鐘。系統(tǒng)時(shí)鐘則是指kernel中 的時(shí)鐘。所有Linux相關(guān)指令與函數(shù)都是讀取系統(tǒng)時(shí)鐘的設(shè)定。因?yàn)榇嬖趦煞N不同的時(shí)鐘,那么它們之間就會存在差異。根據(jù)不同參數(shù)設(shè)置,hwclock命令既可以將硬件時(shí)鐘同步到系統(tǒng)時(shí)鐘,也可以將系統(tǒng)時(shí)鐘同步到硬件時(shí)鐘。
關(guān)于系統(tǒng)時(shí)鐘與硬件時(shí)鐘的關(guān)系的說法,網(wǎng)上分成了兩種:
第一種:當(dāng)Linux啟動時(shí),硬件時(shí)鐘會去讀取系統(tǒng)時(shí)鐘的設(shè)置,然后系統(tǒng)時(shí)鐘就會獨(dú)立于硬件運(yùn)作。
第二種:當(dāng)Linux啟動時(shí),系統(tǒng)時(shí)鐘會去讀取硬件時(shí)鐘的設(shè)定,之后系統(tǒng)時(shí)鐘即獨(dú)立運(yùn)作。
到底那一種才是正確的呢?執(zhí)行一下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 timewhen Linux is not running. You initialize the System Time to the time from the Hardware Clock when Linuxstarts up, and then never use the Hardware Clock again. Note that in DOS, for which ISA was designed, theHardware Clock is the only real time clock.這段文字說明,第二種說法是正確的,第一種說法是錯(cuò)誤的。但網(wǎng)上大部分文章都是第一種說法,所以說,網(wǎng)絡(luò)雖好,要注意甄別。
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 Clockwhile the system is running, and the next time Linux starts up, it will do so with the adjusted time from theHardware Clock.如果使用date命令修改了系統(tǒng)時(shí)間,并不會自動去修改硬件時(shí)鐘,因此,當(dāng)系統(tǒng)下次重啟時(shí),系統(tǒng)時(shí)鐘還會從硬件時(shí)鐘去取,date設(shè)置的時(shí)間就無效了。這大概就是為什么需要hwclock命令的原因吧。
-r, --show 讀取并打印硬件時(shí)鐘(read hardware clock and print result ) -s, --hctosys 將硬件時(shí)鐘同步到系統(tǒng)時(shí)鐘(set the system time from the hardware clock ) -w, --systohc 將系統(tǒng)時(shí)鐘同步到硬件時(shí)鐘(set the hardware clock to the current system time )
[root@new55 ~]# type -a hwclock hwclock is /sbin/hwclockhwclock is /usr/sbin/hwclock[root@new55 ~]# ls -l /sbin/hwclock /usr/sbin/hwclock -rwxr-xr-x 1 root root 34096 2010-01-07 /sbin/hwclocklrwxrwxrwx 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時(shí)09分28秒 -0.134840 seconds[root@new55 ~]# hwclock -r 2010年11月06日 星期六 21時(shí)09分33秒 -0.469123 seconds[root@new55 ~]# hwclock --show 2010年11月06日 星期六 21時(shí)09分45秒 -0.127548 seconds[root@new55 ~]#
[root@new55 ~]# date 2010年 11月 06日 星期六 21:11:57 CST[root@new55 ~]# date 11062112 <== 格式 mmddHHMM,即 月日時(shí)分 2010年 11月 06日 星期六 21:12:00 CST[root@new55 ~]# hwclock -w [root@new55 ~]# hwclock 2010年11月06日 星期六 21時(shí)12分13秒 -0.648818 seconds[root@new55 ~]#
注:要使系統(tǒng)時(shí)間準(zhǔn)確,最好還是使用ntp方式。
ntpdate命令是使用NTP協(xié)議來從網(wǎng)絡(luò)同步時(shí)間的命令。NTP=Network Time Protocol 網(wǎng)絡(luò)時(shí)間協(xié)議。
[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要將時(shí)鐘分為系統(tǒng)時(shí)鐘和硬件時(shí)鐘,這樣做有什么好處?
【1】91linux.com Linux hwclock 命令 其他資料對硬件時(shí)鐘與系統(tǒng)時(shí)鐘的關(guān)系的說法有問題,所以此處不列出。
新聞熱點(diǎn)
疑難解答
圖片精選