我使用过的Linux命令之hwclock - 查询和设置硬件时钟
我使用過的Linux命令之hwclock - 查詢和設(shè)置硬件時鐘
本文鏈接:http://codingstandards.iteye.com/blog/804830?? (轉(zhuǎn)載請注明出處)
?
用途說明
hwclock命令,與clock命令是同一個命令,主要用來查詢和設(shè)置硬件時鐘(query and set the hardware clock (RTC))。RTC=Real Time Clock,也就是硬件時鐘。在Linux中有硬件時鐘與系統(tǒng)時鐘等兩種時鐘。硬件時鐘是指主機板上的時鐘設(shè)備,也就是通常可在BIOS畫面設(shè)定的時鐘。系統(tǒng)時鐘則是指kernel中 的時鐘。所有Linux相關(guān)指令與函數(shù)都是讀取系統(tǒng)時鐘的設(shè)定。因為存在兩種不同的時鐘,那么它們之間就會存在差異。根據(jù)不同參數(shù)設(shè)置,hwclock命令既可以將硬件時鐘同步到系統(tǒng)時鐘,也可以將系統(tǒng)時鐘同步到硬件時鐘。
關(guān)于系統(tǒng)時鐘與硬件時鐘的關(guān)系的說法,網(wǎng)上分成了兩種:
第一種:當Linux啟動時,硬件時鐘會去讀取系統(tǒng)時鐘的設(shè)置,然后系統(tǒng)時鐘就會獨立于硬件運作。
第二種:當Linux啟動時,系統(tǒng)時鐘會去讀取硬件時鐘的設(shè)定,之后系統(tǒng)時鐘即獨立運作。
到底那一種才是正確的呢?執(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 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ò)雖好,要注意甄別。
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命令修改了系統(tǒng)時間,并不會自動去修改硬件時鐘,因此,當系統(tǒng)下次重啟時,系統(tǒng)時鐘還會從硬件時鐘去取,date設(shè)置的時間就無效了。這大概就是為什么需要hwclock命令的原因吧。
常用參數(shù)
? -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 )
使用示例
示例一 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 ~]#
?
注:要使系統(tǒng)時間準確,最好還是使用ntp方式。
示例四 使用ntpdate命令從網(wǎng)絡(luò)同步時間,再同步到硬件時鐘
ntpdate命令是使用NTP協(xié)議來從網(wǎng)絡(luò)同步時間的命令。NTP=Network Time Protocol 網(wǎng)絡(luò)時間協(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要將時鐘分為系統(tǒng)時鐘和硬件時鐘,這樣做有什么好處?
相關(guān)資料
【1】91linux.com?Linux hwclock 命令?其他資料對硬件時鐘與系統(tǒng)時鐘的關(guān)系的說法有問題,所以此處不列出。
?
總結(jié)
以上是生活随笔為你收集整理的我使用过的Linux命令之hwclock - 查询和设置硬件时钟的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 妄想山海青叶在哪抓
- 下一篇: linux 同步IO: sync msy