新聞中心
建站服務(wù)器
一、先介紹幾個與時間相關(guān)常見的概念
GMT:Greenwich Mean Time 格林威治時間
理論上來說,格林尼治標準時間的正午是指當太陽橫穿格林尼治子午線時(也就是在格林尼治上空高點時)的時間。由于地球在它的橢圓軌道里的運動速度不均勻,這個時刻可能和實際的太陽時相差16分鐘。
UTC:Coordinated Universal Time 協(xié)調(diào)世界時
世界標準時間
CST:(Chinese Standard Time)
CST卻同時可以代表如下 4 個不同的時區(qū):
Central Standard Time (USA) UT-6:00
Central Standard Time (Australia) UT+9:30
China Standard Time UT+8:00
Cuba Standard Time UT-4:00
二、操作系統(tǒng)上的兩種時間
軟件時鐘:也叫系統(tǒng)時間,根據(jù)1970/01/01開始計算的總秒數(shù)
查看系統(tǒng)時間: [root@node1~]#date#查看當前時間 WedJun2819:57:53CST2017 [root@node1~]#date+%Y-%m-%d#設(shè)置當前時間格式 2017-06-28 [root@node1~]#date+%Y-%m-%d%H:%M:%S#設(shè)置當前時間格式 2017-06-2819:58:42 [root@node1~]#date+%s#從1970/01/01到現(xiàn)在的總秒數(shù) 1498651211 設(shè)置時間: date-s2012052301:01:01#設(shè)置當前系統(tǒng)時間,一般不建議直接在生產(chǎn)主機上執(zhí)行。 date-sdd/mm/yyyyhh:mm:ss
硬件時鐘:主機硬件系統(tǒng)上的時鐘。主板上BIOS中的時間,由主板電池供電來維持運行,系統(tǒng)開機時要讀取這個時間,并根據(jù)它來設(shè)定系統(tǒng)時間.
查看硬件時間:hwclock或clock 設(shè)置:hwclock--set--date=06/18/1414:55
硬件時間與系統(tǒng)時間相互轉(zhuǎn)換:
clock--hctosys#將系統(tǒng)時間調(diào)整為目前的硬件時間一致;當Linux啟動時,系統(tǒng)時鐘會去讀取硬件時鐘的設(shè)定,之后系統(tǒng)時鐘即獨立運作. clock--systohc#將硬件時鐘調(diào)整為與目前的系統(tǒng)時鐘一致;將系統(tǒng)時間存回硬件時鐘內(nèi)
三、NTP服務(wù)器安裝與設(shè)置
1.所需要的軟件
ntp 主要軟件
tzdata 提供各時區(qū)對應(yīng)的顯示格式
2.相關(guān)文件
/etc/ntp.conf 主配置文件
/usr/share/zoneinfo/ 由tzdata提供,為各時區(qū)的時間格式對應(yīng)文件。
/etc/sysconfig/clock 設(shè)置時區(qū)是否使用UTC時鐘的配置文件。
[root@node1zoneinfo]#cat/etc/sysconfig/clock ZONE=Asia/Shanghai #表明我們的時間配置文件要使用/usr/share/zoneinfo/Asia/Shanghai那個文件
/etc/localtime 本地端的時間配置文件
簡單的一個例子: 目前在上海,假如到了美國,怎么調(diào)整時間? 首先vim/etc/sysconfig/clock ZONE=America/New_York 然后cp/usr/share/zoneinfo/America/New_York/etc/localtime 最后date
3.主配置文件
[root@node1~]#cat/etc/ntp.conf #Formoreinformationaboutthisfile,seethemanpages #ntp.conf(5),ntp_acc(5),ntp_auth(5),ntp_clock(5),ntp_misc(5),ntp_mon(5). driftfile/var/lib/ntp/drift #Permittimesynchronizationwithourtimesource,butdonot #permitthesourcetoqueryormodifytheserviceonthissystem. restrictdefaultkodnomodifynotrapnopeernoquery restrict-6defaultkodnomodifynotrapnopeernoquery #Permitallaccessovertheloopbackinterface.Thiscould #betightenedaswell,buttodosowouldeffectsomeof #theadministrativefunctions. restrict127.0.0.1 restrict-6::1 #Hostsonlocalnetworkarelessrestricted. #restrict192.168.1.0mask255.255.255.0nomodifynotrap #Usepublicserversfromthepool.ntp.orgproject. #Pleaseconsiderjoiningthepool(http://www.pool.ntp.org/join.html). #server0.rhel.pool.ntp.orgiburst #server1.rhel.pool.ntp.orgiburst #server2.rhel.pool.ntp.orgiburst #server3.rhel.pool.ntp.orgiburst server1.cn.pool.ntp.org #broadcast192.168.1.255autokey#broadcastserver #broadcastclient#broadcastclient #broadcast224.0.1.1autokey#multicastserver #multicastclient224.0.1.1#multicastclient #manycastserver239.255.254.254#manycastserver #manycastclient239.255.254.254autokey#manycastclient #Enablepublickeycryptography. #crypto includefile/etc/ntp/crypto/pw #Keyfilecontainingthekeysandkeyidentifiersusedwhenoperating #withsymmetrickeycryptography. keys/etc/ntp/keys #Specifythekeyidentifierswhicharetrusted. #trustedkey4842 #Specifythekeyidentifiertousewiththentpdcutility. #requestkey8 #Specifythekeyidentifiertousewiththentpqutility. #controlkey8 #Enablewritingofstatisticsrecords. #statisticsclockstatscryptostatsloopstatspeerstats
參數(shù)解釋:
(1)driftfile 記錄時間差異
(2)restrict 管理權(quán)限控制
restrict[ip]mask[netmask_ip][parameter] parameter主要有: ignore:拒絕所有類型的NTP連接 nomodify:客戶端不能使用ntpc/ntpq來修改服務(wù)器的時間參數(shù),但客戶端可以進行網(wǎng)絡(luò)校時 noquery:客戶端不能用ntpq/ntpc來查詢時間服務(wù)器,等于不提供NTP的網(wǎng)絡(luò)校時 notrap:不提供trap這個遠程事件登陸 notrust:拒絕沒有認證的客戶端 如果在[parameter]沒有設(shè)置,則表示該網(wǎng)段不受任何限制。
(3)server:設(shè)置上層的NTP服務(wù)器
server[ip或主機名][prefer]
四、服務(wù)器NTP服務(wù)的啟動
#/etc/init.d/ntpdstart#啟動服務(wù) #chkconfigntpdon#開機自啟 #tail/var/log/messages#查看日志 查看端口: [root@node1~]#netstat-tulnp|grepntp udp00192.168.209.128:1230.0.0.0:*2766/ntpd udp00127.0.0.1:1230.0.0.0:*2766/ntpd udp000.0.0.0:1230.0.0.0:*2766/ntpd udp00fe80::20c:29ff:fe67:be62:123:::*2766/ntpd udp00::1:123:::*2766/ntpd udp00:::123:::*2766/ntpd 通常啟動NTP15分鐘后才會和上層NTP服務(wù)器順利連接上。 #ntpstat查看ntp服務(wù)器是否與上層連接。 synchronisedtoNTPserver(xx.xx.xx.xx)atstratum3 timecorrecttowithin538ms pollingserverevery128s [root@node1~]#ntpq-p remoterefidsttwhenpollreachdelayoffsetjitter ============================================================================== 85.199.214.101.GPS.1u55641245.097179.4150.000 remote:NTP主機的IP或者主機名 *代表目前正在作用當中的上層NTP +代表已經(jīng)連接成功,可以作為下一個提供更新時間的候選者 refid:NTP服務(wù)器使用的更高一級服務(wù)器的名稱 st:stratum階層 when:幾秒前曾經(jīng)做過時間同步化更新的操作.上一次成功請求之后到現(xiàn)在的秒數(shù) poll:下一次更新在幾秒鐘之后.本地和遠程服務(wù)器多少時間進行一次同步,單位秒,在一開始運行NTP的時候這個poll值會比較小,服務(wù)器同步的頻率大,可以盡快調(diào)整到正確的時間范圍,之后poll值會逐漸增大,同步的頻率也就會相應(yīng)減小 reach:已經(jīng)向上層NTP服務(wù)器要求更新的次數(shù).用來測試能否和服務(wù)器連接,是一個八進制值,每成功連接一次它的值就會增加 delay:網(wǎng)絡(luò)傳輸過程當中延遲的時間.從本地機發(fā)送同步要求到ntp服務(wù)器的往返時間 offset:時間補償?shù)慕Y(jié)果.主機通過NTP時鐘同步與所同步時間源的時間偏移量,單位為毫秒,offset越接近于0,主機和ntp服務(wù)器的時間越接近 jitter:Linux系統(tǒng)時間與硬件時間的差異時間。這個數(shù)值的絕對值越小,主機的時間就越精確
五、客戶端同步NTP服務(wù)器的時間
方法1: ntpdate192.168.209.128 編輯定時任務(wù)crontab,每隔30分鐘自動同步,并寫入硬件時鐘。 */30****(/usr/sbin/ntpdate192.168.209.128&&hwclock-w)&>/dev/null 方法2: 修改/etc/ntp.conf配置文件,開啟ntpd服務(wù),使其自動去同步上層ntp服務(wù)器。 添加server1.cn.pool.ntp.org /etc/init.d/ntpdstart chkconfigntpdon
當前文章:9.linuxntp服務(wù)器搭建
瀏覽地址:http://www.ef60e0e.cn/article/cpjgeo.html