今有一小型項(xiàng)目,完全自主弄,原來(lái)以為很簡(jiǎn)單的NTP服務(wù),我給折騰了2個(gè)多小時(shí)才整撐頭(以前都是運(yùn)維搞,沒(méi)太注意,所以這技術(shù)的東西,在簡(jiǎn)單都需要親嘗啊),這里記錄為以后別再浪費(fèi)時(shí)間。
目標(biāo)環(huán)境,5臺(tái)linux centos 6.3, 一臺(tái)作為NTPD服務(wù)與外部公共NTP服務(wù)同步時(shí)間,同時(shí)作為內(nèi)網(wǎng)的NTPD服務(wù)器,其他機(jī)器與這臺(tái)服務(wù)做時(shí)間同步。
服務(wù)器IP角色 說(shuō)明同步方式
192.168.1.135 NTPD服務(wù) 1、負(fù)責(zé)與外部公共NTPD服務(wù)同步標(biāo)準(zhǔn)時(shí)間
2、作為內(nèi)外網(wǎng)絡(luò)的NTPD服務(wù)
NTPD服務(wù)平滑同步
192.168.1.xxx 內(nèi)外NTP客戶端 內(nèi)網(wǎng)設(shè)備與192.168.1.135同步時(shí)間NTPD服務(wù)平滑同步
…… 內(nèi)外NTP客戶端 內(nèi)網(wǎng)設(shè)備與192.168.1.135同步時(shí)間NTPD服務(wù)平滑同步
1、NTP時(shí)間同步方式選擇
NTP同步方式在linux下一般兩種:使用ntpdate命令直接同步和使用NTPD服務(wù)平滑同步。有什么區(qū)別呢,簡(jiǎn)單說(shuō)下,免得時(shí)間長(zhǎng)了,概念又模糊。
現(xiàn)有一臺(tái)設(shè)備,系統(tǒng)時(shí)間是 13:00 , 真實(shí)的當(dāng)前時(shí)間(在空中,也許衛(wèi)星上,這里假設(shè)是在準(zhǔn)備同步的上級(jí)目標(biāo)NTP服務(wù)器)是: 12:30 。如果我們使用ntpdate同步(ntpdate -u 目標(biāo)NTP服務(wù)器IP),操作系統(tǒng)的時(shí)間立即更新為12:30,假如,我們的系統(tǒng)有一個(gè)定時(shí)應(yīng)用,是在每天12:40運(yùn)行,那么實(shí)際今天這個(gè)的任務(wù)已經(jīng)運(yùn)行過(guò)了(當(dāng)前時(shí)間是13:00嘛),現(xiàn)在被ntpdate修改為12:30,那么意味作10分鐘后,又會(huì)執(zhí)行一次任務(wù),這就糟糕了,這個(gè)任務(wù)只能執(zhí)行一次的嘛??!我想你(其實(shí)是我)已經(jīng)懂了ntpdate時(shí)間同步的隱患,當(dāng)然這個(gè)例子有些極端,但的確是有風(fēng)險(xiǎn)的,生產(chǎn)環(huán)境我不打算這么干,還是穩(wěn)妥點(diǎn)好。所以解決該問(wèn)題的辦法就是時(shí)間平滑更改,不會(huì)讓一個(gè)時(shí)間點(diǎn)在一天內(nèi)經(jīng)歷兩次,這就是NTPD服務(wù)方式平滑同步時(shí)間,它每次同步時(shí)間的偏移量不會(huì)太陡,是慢慢來(lái)的(問(wèn):怎么來(lái),沒(méi)有細(xì)究,只曉得一次一點(diǎn)的同步,完全同步好需要較長(zhǎng)時(shí)間,所以一般開(kāi)啟NTPD服務(wù)同步前先用ntpdate先手動(dòng)同步一次)。
2、安裝配置
CentOS 6.3系統(tǒng)已經(jīng)自帶了NTPD服務(wù),一般默認(rèn)是按照了的,如果沒(méi)有安裝,先檢查下,然后配置好yum倉(cāng)庫(kù),yum方式安裝下就OK,具體如下:
# rpm -q ntp
ntp-4.2.4p8-2.el6.x86_64 // 這表示已安裝了,如果沒(méi)有安裝,這是空白。
如果沒(méi)有安裝,我們按照下
# yum install ntp
......
按上面的安裝方式在內(nèi)網(wǎng)每臺(tái)服務(wù)器上都安裝好NTP軟件包。
完成后,都需要配置NTP服務(wù)為自啟動(dòng)
# chkconfig ntpd on
# chkconfig --list ntpd
ntpd 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉
在配置前,先使用ntpdate手動(dòng)同步下時(shí)間,免得本機(jī)與外部時(shí)間服務(wù)器時(shí)間差距太大,讓ntpd不能正常同步。
# ntpdate -u 202.112.10.36
22 Dec 16:52:38 ntpdate[6400]: adjust time server 202.112.10.36 offset 0.012135 sec
配置內(nèi)網(wǎng)NTP-Server(192.168.1.135)
下面主要是配置內(nèi)網(wǎng)的NPTD服務(wù)器(192.168.1.135), NTPD服務(wù)配置核心就在/etc/ntp.conf文件,配置好了就OK。網(wǎng)上特別是老外的文章都很簡(jiǎn)單,我上當(dāng)了,媽喲,基礎(chǔ)環(huán)境不一樣,我們得中國(guó)特色才行。先上配置文件再說(shuō),紅色部分是我的修改,其他的是默認(rèn)。
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict -6 ::1
# Hosts on local network are less restricted.
# 允許內(nèi)網(wǎng)其他機(jī)器同步時(shí)間
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 中國(guó)這邊最活躍的時(shí)間服務(wù)器 :
http://www.pool.ntp.org/zone/cnserver 210.72.145.44 perfer # 中國(guó)國(guó)家受時(shí)中心
server 202.112.10.36 # 1.cn.pool.ntp.org
server 59.124.196.83 # 0.asia.pool.ntp.org
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# allow update time by the upper server
# 允許上層時(shí)間服務(wù)器主動(dòng)修改本機(jī)時(shí)間
restrict 210.72.145.44 nomodify notrap noquery
restrict 202.112.10.36 nomodify notrap noquery
restrict 59.124.196.83 nomodify notrap noquery
# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available.
# 外部時(shí)間服務(wù)器不可用時(shí),以本地時(shí)間作為時(shí)間服務(wù)
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
配置參數(shù)和命令簡(jiǎn)單說(shuō)明請(qǐng)參考:
http://linux.vbird.org/linux_server/0440ntp.php#server_ntp.conf配置文件修改完成,保存退出,啟動(dòng)服務(wù)。
# service ntpd start
......
啟動(dòng)后,一般需要5-10分鐘左右的時(shí)候才能與外部時(shí)間服務(wù)器開(kāi)始同步時(shí)間??梢酝ㄟ^(guò)命令查詢NTPD服務(wù)情況。
查看服務(wù)連接和監(jiān)聽(tīng)
# netstat -tlunp | grep ntp
udp 0 0 192.168.1.135:123 0.0.0.0:* 23103/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 23103/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 23103/ntpd
udp 0 0 fe80::6cae:8bff:fe3d:f65:123 :::* 23103/ntpd
udp 0 0 fe80::6eae:8bff:fe3d:f65:123 :::* 23103/ntpd
udp 0 0 ::1:123 :::* 23103/ntpd
udp 0 0 :::123 :::* 23103/ntpd
看紅色加粗的地方,表示連接和監(jiān)聽(tīng)已正確,采用UDP方式
ntpq -p 查看網(wǎng)絡(luò)中的NTP服務(wù)器,同時(shí)顯示客戶端和每個(gè)服務(wù)器的關(guān)系
# ntpq -p
# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*202.112.10.36 202.112.10.60 2 u 277 128 314 201.553 9.193 17.068
+59.124.196.83 129.6.15.28 2 u 88 128 377 71.153 -25.111 14.004
LOCAL(0) .LOCL. 10 l 15 64 377 0.000 0.000 0.000
位置標(biāo)志含義
符號(hào)*響應(yīng)的NTP服務(wù)器和最精確的服務(wù)器
+響應(yīng)這個(gè)查詢請(qǐng)求的NTP服務(wù)器
blank(空格)沒(méi)有響應(yīng)的NTP服務(wù)器
標(biāo)題remote響應(yīng)這個(gè)請(qǐng)求的NTP服務(wù)器的名稱
refidNTP服務(wù)器使用的更高一級(jí)服務(wù)器的名稱
st正在響應(yīng)請(qǐng)求的NTP服務(wù)器的級(jí)別
when上一次成功請(qǐng)求之后到現(xiàn)在的秒數(shù)
poll本地和遠(yuǎn)程服務(wù)器多少時(shí)間進(jìn)行一次同步,單位秒,在一開(kāi)始運(yùn)行NTP的時(shí)候這個(gè)poll值會(huì)比較小,服務(wù)器同步的頻率大,可以盡快調(diào)整到正確的時(shí)間范圍,之后poll值會(huì)逐漸增大,同步的頻率也就會(huì)相應(yīng)減小
reach用來(lái)測(cè)試能否和服務(wù)器連接,是一個(gè)八進(jìn)制值,每成功連接一次它的值就會(huì)增加
delay從本地機(jī)發(fā)送同步要求到ntp服務(wù)器的往返時(shí)間
offset主機(jī)通過(guò)NTP時(shí)鐘同步與所同步時(shí)間源的時(shí)間偏移量,單位為毫秒,offset越接近于0,主機(jī)和ntp服務(wù)器的時(shí)間越接近
jitter統(tǒng)計(jì)了在特定個(gè)連續(xù)的連接數(shù)里offset的分布情況。簡(jiǎn)單地說(shuō)這個(gè)數(shù)值的絕對(duì)值越小,主機(jī)的時(shí)間就越精確
ntpstat 命令查看時(shí)間同步狀態(tài),這個(gè)一般需要5-10分鐘后才能成功連接和同步。所以,服務(wù)器啟動(dòng)后需要稍等下。
剛啟動(dòng)的時(shí)候,一般是:
# ntpstat
unsynchronised
time server re-starting
polling server every 64 s
連接并同步后:
synchronised to NTP server (202.112.10.36) at stratum 3
time correct to within 275 ms
polling server every 256 s
OK,內(nèi)網(wǎng)的NTPD服務(wù)已經(jīng)配置完成,如果所有正常后,開(kāi)始配置內(nèi)網(wǎng)的其他設(shè)備與這臺(tái)服務(wù)器作為時(shí)間同步服務(wù)。
配置內(nèi)網(wǎng)NTP-Clients
內(nèi)網(wǎng)其他設(shè)備作為NTP的客戶端配置,相對(duì)就比較簡(jiǎn)單,而且所有設(shè)備的配置都相同。
首先需要安裝NTPD服務(wù),然后配置為自啟動(dòng)(與NTP-Server完全一樣)。然后找其中一臺(tái)配置/etc/ntp.conf文件,配置完成驗(yàn)證通過(guò)后,拷貝到其他客戶端機(jī)器,直接使用即可。
# yum install ntp
...
# chkconfig ntp on
# vim /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict 127.0.0.1
restrict -6 ::1
# 配置時(shí)間服務(wù)器為本地的時(shí)間服務(wù)器
server 192.168.1.135
restrict 192.168.1.135 nomodify notrap noquery
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
為了簡(jiǎn)單,這里只列出了配置項(xiàng),注釋全部清理了。
OK,保存退出,請(qǐng)求服務(wù)器前,請(qǐng)先使用ntpdate手動(dòng)同步下時(shí)間
# ntpdate -u 192.168.0.135
22 Dec 17:09:57 ntpdate[6439]: adjust time server 192.168.1.135 offset 0.004882 sec
這里有可能出現(xiàn)同步失敗,一般情況下原因都是本地的NTPD服務(wù)器還沒(méi)有正常啟動(dòng)起來(lái),一般需要幾分鐘時(shí)間后才能開(kāi)始同步。
錯(cuò)誤判斷請(qǐng)參考后面的錯(cuò)誤處理。
# service ntpd start
....
啟動(dòng)后,查看同步情況
# ntpq -p
# ntpstat
.....
因?yàn)槭莾?nèi)網(wǎng),一般ntpstat很快就可以同步上,幾分鐘需要等下.
OK,本機(jī)客戶端配置完成后,使用SCP拷貝/etc/ntp.conf到其他需要同步的客戶端機(jī)器,啟動(dòng)NTPD服務(wù)即可。
其他客戶端機(jī)器上操作配置如下:
# ntpdate -u 192.168.0.135
22 Dec 17:09:57 ntpdate[6439]: adjust time server 192.168.1.135 offset 0.004882 sec
# scp 192.168.1.xxx:/etc/ntp.conf /etc/ntp.conf
# service ntpd start
3、錯(cuò)誤問(wèn)題處理
用于收集安裝,配置和應(yīng)用中出現(xiàn)的問(wèn)題
錯(cuò)誤1:ntpdate -u ip -> no server suitable for synchronization found
判斷:在ntp客戶端用ntpdate –d serverIP查看,發(fā)現(xiàn)有“Server dropped: strata too high”的錯(cuò)誤,并且顯示“stratum 16”。而正常情況下stratum這個(gè)值得范圍是“0~15”。
原因:NTP server還沒(méi)有和其自身或者它的server同步上。在ntp server上重新啟動(dòng)ntp服務(wù)后,ntp server自身或者與其server的同步的需要一個(gè)時(shí)間段,這個(gè)過(guò)程可能是5分鐘,在這個(gè)時(shí)間之內(nèi)在客戶端運(yùn)行ntpdate命令時(shí)會(huì)產(chǎn)生no server suitable for synchronization found的錯(cuò)誤。
處理:等待幾分鐘后,重試一般解決。
也可以使用命令 ntpq -p查看情況
參考:
http://blog.csdn.net/weidan1121/article/details/3953021