国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Linux > 正文

我使用過的Linux命令之hwclock - 查詢和設(shè)置硬件時(shí)鐘

2024-06-28 16:02:22
字體:
供稿:網(wǎng)友

我使用過的linux命令之hwclock - 查詢和設(shè)置硬件時(shí)鐘

本文鏈接: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命令的原因吧。

常用參數(shù)

  -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 )

使用示例

示例一 hwclock命令與clock命令是一個(gè)東西

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

示例二 顯示硬件時(shí)鐘

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

 

示例三 修改日期并同步到硬件時(shí)鐘

[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命令從網(wǎng)絡(luò)同步時(shí)間,再同步到硬件時(shí)鐘

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í)鐘,這樣做有什么好處?

相關(guān)資料

【1】91linux.com Linux hwclock 命令 其他資料對硬件時(shí)鐘與系統(tǒng)時(shí)鐘的關(guān)系的說法有問題,所以此處不列出。

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 东乡县| 和龙市| 即墨市| 河北区| 临湘市| 大兴区| 汪清县| 郴州市| 凌云县| 武清区| 曲阜市| 沁阳市| 广河县| 新和县| 南雄市| 东丽区| 淮南市| 大理市| 临海市| 天全县| 冀州市| 浙江省| 贵港市| 永寿县| 陇南市| 凉山| 武乡县| 吴桥县| 万全县| 南汇区| 刚察县| 灵丘县| 普陀区| 维西| 拜城县| 黔南| 宣化县| 建宁县| 洛川县| 隆子县| 广河县|