centos7 ntp服務器配置


一、ntp服務是什么

1. 定義

NTP是網絡時間協議(Network Time Protocol),它是用來同步網絡中各個計算機的時間的協議。

 

2. 發展

首次記載在Internet Engineering Note之中,其精確度為數百毫秒。
稍后出現了首個時間協議的規范RFC-778,它被命名為DCNET互聯網時間服務,這種服務還是借助於Internet control Message Protocol (ICMP)中的時間戳和時間戳應答消息作為NTP。
名稱的首次出現是在RFC-958之中,該版本也被稱為NTP v0,其目的是為ARPA網提供時間同步。已脫離ICMP,作為獨立協議運行,
美國特拉華大學的David L.Mills主持了網絡時間同步項目,成功的開發出了NTP協議的第1, 2, 3版。

v1:出現於1988年6月,在RFC-1059中描述了首個完整的NTP的規范和相關算法。這個版本已經采用了client/server模式以及對稱操作,但是它不支持授權鑒別和NTP的控制消息。

v2:1989年9月推出了取代RFC-958和RFC-1059的NTP v2版本即RFC-1119。

v3:在1992 年3月,NTP v3版本RFC-1305問世,該版本總結和綜合了NTP先前版本和DTSS,正式引入了校正原則,並改進了時鍾選擇和時鍾濾波的算法,而且還引入了時間消息發送的廣播模式,這個版本取代了NTP的先前版本。NTP v 3 發布后,一直在不斷地進行改進,NTP實現的一個重要功能是對計算機操作系統的時鍾調整。

v4:截止到2010年6月,最新的NTP版本是第4版(NTPv4),其標准化文檔為 RFC 5905,它繼承自RFC 1305所描述的NTP v3。網絡時間同步技術也將向更高精度、更強的兼容性和多平台的適應性方向發展。網絡時間協議NTP是用於互聯網中時間同步的標准之一,它的用途是把計算機的時鍾同步到世界協調時UTC,其精度在局域網內可達0.lms,在Internet上絕大多數的地方其精度可以達到1- 50ms.

值得提一下的是,簡單的NTP(SNTP)version4已經在RFC2030描述了。

 

3. 主要結構

(1) 時間來源

現在的標准時間是由原子鍾報時的國際標准時間UTC(Universal Time Coordinated,世界協調時),所以NTP獲得UTC的時間來源可以是原子鍾、天文台、衛星,也可以從Internet上獲取。

 

(2) 傳播

在NTP中,定義了時間按照服務器的等級傳播,按照離外部UTC源遠近將所有的服務器歸入不同的Stratum(層)中,例如把通過GPS取得發送標准時間的服務器叫Stratum-1的NTP服務器,而Stratum-2則從Stratum-1獲取時間,Stratum-3從Stratum-2獲取時間,以此類推,但Stratum層的總數限制在15以內。所有這些服務器在邏輯上形成階梯式的架構相互連接,而Stratum-1的時間服務器是整個系統的基礎,這種階梯式的架構示意圖如下圖所示:

d0c8a786c9177f3e95090eb873cf3bc79f3d5642副本

計算機主機一般同多個時鍾服務器連接,利用統計學的算法過濾來自不同服務器的時間,以選擇最佳的路徑和來源以便校正主機時間。即使在主機長時間無法與某一時鍾服務器聯系的情況下,NTP服務依然可以有效運轉。

 

二、架構及安裝

1. 架構

未命名文件

如上圖所示,對於一些服務依賴於時間的集群(如hadoop集群),需要有一部主機作為ntp服務器,其他客戶端主機從這部主機進行時間同步,另外ntp服務主機從更高一層的服務器獲得時間信息。

 

2. 安裝

centos系統,直接用yum進行安裝就可以了:

yum -y install ntp

 

三、主要配置

1. 配置文件

centos7的ntp配置文件存放路徑為:/etc/ntp.conf。

 

2. restrict 控制相關權限。

語法為: restrict IP地址 mask 子網掩碼 參數

其中IP地址也可以是default ,default 就是指所有的IP。

參數有以下幾個:

ignore  :關閉所有的 NTP 聯機服務

nomodify:客戶端不能更改服務端的時間參數,但是客戶端可以通過服務端進行網絡校時。

notrust :客戶端除非通過認證,否則該客戶端來源將被視為不信任子網

noquery :不提供客戶端的時間查詢:用戶端不能使用ntpq,ntpc等命令來查詢ntp服務器

notrap :不提供trap遠端登陸:拒絕為匹配的主機提供模式 6 控制消息陷阱服務。陷阱服務是 ntpdq 控制消息協議的子系統,用於遠程事件日志記錄程序。

nopeer :用於阻止主機嘗試與服務器對等,並允許欺詐性服務器控制時鍾

kod : 訪問違規時發送 KoD 包。

restrict -6 表示IPV6地址的權限設置。

 

3. server 設定NTP主機來源

語法為:server 主機ip或域名 參數

參數有以下幾個:

burst:When the server is reachable, send a burst of eight packets instead of the usual one. The packet spacing is nor‐mally 2 s; however, the spacing between the first and second packets can be changed with the calldelay command to allow additional time for a modem or ISDN call to complete. This option is valid only with the server command and type s addressesa. It is a recommended option when the maxpoll option is greater than 10 (1024 s).

iburst:When the server is unreachable, send a burst of eight packets instead of the usual one.  The  packet  spacing  is normally 2 s; however, the spacing between the first and second packets can be changed with the calldelay command to allow additional time for a modem or ISDN call to complete. This option is valid only with the server  command and type s addresses. It is a recommended option with this command.

prefer:Mark  the server as preferred. All other things being equal, this host will be chosen for synchronization among a set of correctly operating hosts. See the Mitigation Rules and the prefer Keyword page for  further  information. This option is valid only with the server and peer commands.

true:Mark  the  association  to  assume  truechimer status; that is, always survive the selection and clustering algorithms. This option can be used with any association, but is most useful for reference clocks with  large  jitter on  the  serial  port  and  precision pulse-per-second (PPS) signals. Caution: this option defeats the algorithms designed to cast out falsetickers and can allow these sources to set the system clock. This option is valid  only with the server and peer commands.

因為翻譯捉急就不翻譯了。

例如:以下表示優先連接192.168.7.49這部主機進行時間同步。

server 192.168.7.49 prefer

 

4. fudge 主機ip stratum 層數

這個配置主要用來設定本機作為為其他機器的時間源的時候,其層數為多少,層數必須在15層之內,一般取10。

 

四、服務端與客戶端配置

1. 服務端配置

driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1 
restrict ::1

server 0.cn.pool.ntp.org 
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 127.0.0.1 

fudge 127.0.0.1 stratum 10

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor

實際上修改的地方不多,主要是修改了幾個時間源:

server 0.cn.pool.ntp.org 
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 127.0.0.1

前面一個是中國的時間源,后面兩個是亞洲洲際的時間源,最后一個表示允許在前面同步失敗的情況從本機同步。

然后就是設定本機的時間服務層次為10:

fudge 127.0.0.1 stratum 10

 

2. 客戶端配置

driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1 
restrict ::1

server host1

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor

客戶端只改了一個地方,就是將時間源服務器定為host1,這是時間服務主機的域名,也可以直接指定ip地址。

 

3. 檢查狀態

以服務端為例子,設置完之后啟動服務:

systemctl start ntpd.service

檢查是否成功,用ntpstat命令查看同步狀態,出現以下狀態代表啟動成功:

synchronised to NTP server (85.199.214.101) at stratum 2 
   time correct to within 155 ms
    polling server every 64 s

如果出現異常請等待幾分鍾,一般等待5-10分鍾才能同步。

如果客戶端無法連接服務端,請禁用防火牆和selinux后再試試。

 

五、參考

1. 百度百科:NTP

2. Linux的NTP配置總結

(完)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM