linux的系統(tǒng)時(shí)鐘在很多地方都要用到,要是不準(zhǔn),就會出現(xiàn)一些奇怪的問題;
在Linux中,用于時(shí)鐘查看和設(shè)置的命令主要有date、hwclock和clock。Linux時(shí)鐘分為系統(tǒng)時(shí)鐘(System Clock)和硬件(Real Time Clock,簡稱RTC)時(shí)鐘。系統(tǒng)時(shí)鐘: 是指當(dāng)前Linux Kernel中的時(shí)鐘,硬件時(shí)鐘: 是主板上由電池供電的時(shí)鐘,這個(gè)硬件時(shí)鐘可以在BIOS中進(jìn)行設(shè)置。
當(dāng)Linux啟動時(shí),硬件時(shí)鐘會去讀取系統(tǒng)時(shí)鐘的設(shè)置,然后系統(tǒng)時(shí)鐘就會獨(dú)立于硬件運(yùn)作。
Linux 中的所有命令(包括函數(shù))都是采用的系統(tǒng)時(shí)鐘設(shè)置。在Linux中,用于時(shí)鐘查看和設(shè)置的命令主要有date、hwclock和clock。其中,clock和hwclock用法相近,只用一個(gè)就行,只不過clock命令除了支持x86硬件體系外,還支持Alpha硬件體系。
1、 date
查看系統(tǒng)時(shí)間
# date
設(shè)置系統(tǒng)時(shí)間
# date –set “07/07/06 10:19″ //(月/日/年時(shí):分:秒)
2、hwclock/clock
查看硬件時(shí)間
# hwclock –show //或者
# clock –show
設(shè)置硬件時(shí)間
# hwclock –set –date=”07/07/06 10:19″ (月/日/年 時(shí):分:秒) 或者
# clock –set –date=”07/07/06 10:19″ (月/日/年 時(shí):分:秒)
3、硬件時(shí)間和系統(tǒng)時(shí)間的同步
按照前面的說法,重新啟動系統(tǒng),硬件時(shí)間會讀取系統(tǒng)時(shí)間,實(shí)現(xiàn)同步,
但是在不重新啟動的時(shí)候,需要用hwclock或clock命令實(shí)現(xiàn)同步。
硬件時(shí)鐘與系統(tǒng)時(shí)鐘同步:
# hwclock –hctosys // (hc代表硬件時(shí)間,sys代表系統(tǒng)時(shí)間)或者
# clock –hctosys
系統(tǒng)時(shí)鐘和硬件時(shí)鐘同步:
# hwclock –systohc // 或者
# clock –systohc
4. 和外部的NTP時(shí)間服務(wù)器同步
$ service ntpd stop
這一步是必須的,否則出出現(xiàn):
25 Nov 18:10:34 ntpdate[2106]: the NTP socket is in use, exiting
的失敗提示;
$ ntpdate ntp.sjtu.edu.cn
正常返回如下:
25 Nov 18:14:34 ntpdate[2164]: adjust time server 202.120.2.101 offset -0.006107 sec
錯(cuò)誤返回如:
25 Nov 18:13:44 ntpdate[2158]: no server suitable for synchronization found
$ service ntpd start
$ chkconfig ntpd on
$ clock -w
還可以寫進(jìn)定時(shí)任務(wù)中,以做定時(shí)的時(shí)鐘同步:
$ crontab -e
05 * * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn > /dev/null 2>1
05 17 * * * /sbin/clock -w
附上中國大概能用的NTP時(shí)間服務(wù)器地址
server 133.100.11.8 prefer
server 210.72.145.44
server 203.117.180.36
server 131.107.1.10
server time.asia.apple.com
server 64.236.96.53
server 130.149.17.21
server 66.92.68.246
server www.freebsd.org
server 18.145.0.30
server clock.via.net
server 137.92.140.80
server 133.100.9.2
server 128.118.46.3
server ntp.nasa.gov
server 129.7.1.66
server ntp-sop.inria.frserver 210.72.145.44(中國國家授時(shí)中心服務(wù)器IP地址)
server ntp.sjtu.edu.cn(上海交通大學(xué)網(wǎng)絡(luò)中心NTP服務(wù)器地址)
以上就是設(shè)置Linux 的系統(tǒng)時(shí)鐘和硬件時(shí)鐘同步的方法,謝謝閱讀,希望能幫到大家,請繼續(xù)關(guān)注腳本之家,我們會努力分享更多優(yōu)秀的文章。