NTP簡介
NTP(Network Time Protocol,網絡時間協議)採用的傳輸層協議爲 UDP,使用的 UDP 端口號爲 123。
NTP基本工作原理
NTP的基本工作原理如所示。Device A和Device B通過網絡相連,Device A和Device B的時間不同,需要通過NTP實現時間的自動同步。爲便於理解,作如下假設:
· 在Device A和Device B的時間同步之前,Device A的時間設定爲10:00:00 am,Device B的時間設定爲11:00:00 am。
· DeviceB作爲NTP時間服務器,即Device A與Device B的時間同步。
· NTP報文從Device A到Device B、從Device B到Device A單向傳輸所需要的時間均爲1秒。
· DeviceB處理NTP報文所需的時間是1秒。
Device A和Device B時間同步的工作過程如下:
(1) Device A發送一個NTP報文給Device B,該報文帶有它離開Device A時的時間戳,該時間戳爲10:00:00 am(T1)。
(2) 當此NTP報文到達Device B時,Device B在NTP報文上增加該報文到達Device B時的時間戳,該時間戳爲11:00:01 am(T2)。
(3) 當此NTP報文離開Device B時,Device B再在NTP報文上增加該報文離開Device B時的時間戳,該時間戳爲11:00:02 am(T3)。
(4) 當Device A接收到該響應報文時,Device A的本地時間爲10:00:03 am(T4)。
至此,Device A可以根據上述時間戳計算兩個重要的參數:
· NTP報文的往返時延Delay = (T4-T1) – (T3-T2) = 2秒。
· DeviceA相對Device B的時間差offset = ((T2-T1) + (T3-T4)) / 2 = 1小時。
這樣,Device A就能夠根據這些信息來設定自己的時間,使之與Device B的時間同步。
以上內容只是對NTP工作原理的一個粗略描述,詳細內容請參閱相關的協議規範。
NTP網絡結構及時鐘層數
NTP通過時鐘層數來定義時鐘的準確度。時鐘層數的取值範圍爲1~16,取值越小,時鐘準確度越高。層數爲1~15的時鐘處於同步狀態;層數爲16的時鐘處於未同步狀態。
通常將從權威時鐘(如原子時鐘)獲得時間同步的NTP服務器的層數設置爲1,並將其作爲主時間服務器,爲網絡中其他設備的時鐘提供時間同步。網絡中的設備與主時間服務器的NTP距離,即NTP同步鏈上NTP服務器的數目,決定了設備上時鐘的層數。例如,從主時間服務器獲得時間同步的設備的時鐘層數爲2,即比主時間服務器的時鐘層數大1;從時鐘層數爲2的時間服務器獲得時間同步的設備的時鐘層數爲3,以此類推。
NTP的工作模式
模式 | 工作過程 | 時間同步方向 | 應用場合 |
客戶端/服務器模式 | 客戶端上需要手工指定NTP服務器的地址。客戶端向NTP服務器發送NTP時間同步報文。NTP服務器收到報文後會自動工作在服務器模式,並回復應答報文。 | 客戶端能夠與NTP服務器的時間同步 | 如上圖所示,該模式通常用於下級的設備從上級的時間服務器獲取時間同步 |
對等體模式 | 主動對等體(Symmetricactivepeer)上需要手工指定被動對等體(Symmetricpassivepeer)的地址。主動對等體向被動對等體發送NTP時間同步報文。被動對等體收到報文後會自動工作在被動對等體模式,並回復應答報文。 | 主動對等體和被動對等體的時間可以互相同步 | 如上圖所示,該模式通常用於同級的設備間互相同步,以便在同級的設備間形成備份。如果某臺設備與所有上級時間服務器的通信出現故障,則該設備仍然可以從同級的時間服務器獲得時間同步 |
廣播模式 | 廣播服務器週期性地向廣播地址255.255.255.255發送NTP時間同步報文。廣播客戶端偵聽來自廣播服務器的廣播報文,根據接收的廣播報文將設備的時間與廣播服務器的時間進行同步。 | 廣播客戶端能夠與廣播服務器的時間同步 | 廣播服務器廣播發送時間同步報文,可以同時同步同一個子網中多個廣播客戶端的時間。 |
組播模式 | 組播服務器週期性地向指定的組播地址發送NTP時間同步報文。客戶端偵聽來自服務器的組播報文,根據接收的組播報文將設備的時間與組播服務器的時間進行同步 | 組播客戶端能夠與組播服務器的時間同步 | 組播模式對廣播模式進行了擴展,組播服務器可以同時爲同一子網、不同子網的多個組播客戶端提供時間同步組播模式的時間準確度不如客戶端/服務器模式和對等體模式 |
配置測試
Ntp-srv(88.1.1.254)作爲ntp server,
Ntp-client01(88.1.1.1)作爲ntp client,服務指向ntp-srv(88.1.1.254)
Ntp-client01(88.1.1.2)作爲ntp client,服務指向ntp-srv(88.1.1.254)
Ntp-client01(88.1.1.3)作爲ntp client,服務指向ntp-srv(88.1.1.1)
Ntp-client01(88.1.1.4)作爲ntp client,服務指向ntp-srv(88.1.1.254以及88.1.1.1、88.1.1.2)
ntp-srv配置:
先配置本地時間:
[ntp-srv]clock protocol none
<ntp-srv>clock datetime 12:00:002017/05/28
#ntp服務配置如下:
ntp-service enable
ntp-service refclock-master 1
# ntp-service refclock-master命令用來設置本地時鐘作爲參考時鐘並制定層數,如果不指定層數,ntp-server會自動指定,我模擬器配置中看到自動指定爲8,推薦手動指定,一般指定爲1或者2.
實際網絡中,通常將從權威時鐘(如原子時鐘)獲得時間同步的NTP 服務器的層數設置爲 1,並將其作爲主時間服務器同步網絡中其他設備的時鐘。
NTP狀態:
Ntp-srv:
<ntp-srv>dis ntp sessions
source reference stra reach poll now offset delay disper
*******************************************************************************[12345]LOCAL(0) LOCL 0 255 64 8 0.0000 0.0000 0.9460
Notes: 1 source(master), 2 source(peer), 3selected, 4 candidate, 5 configured.
Total sessions: 1
<ntp-srv>dis ntp status
Clock status: synchronized
Clock stratum: 1
System peer: LOCAL(0)
Local mode: client
Reference clock ID: LOCL
Leapindicator: 00
Clock jitter: 0.000031 s
Stability: 0.000 pps
Clock precision: 2^-15
Rootdelay: 0.00000 ms
Rootdispersion: 11.16943 ms
Reference time: dcd54bbf.b54d3a2e Sun, May 28 2017 13:14:39.708
#
dis ntp sessions參數說明:
Source:時鐘源,參考時鐘爲本地時鐘時,顯示爲LOCAL(number)
Reference:參考時鐘,當 stra 字段爲 0 或 1 時,本字段將顯示爲 LOCAL
Stra:時鐘層數
Reach:可達性計數,0表示時間服務器不可達
Poll:輪詢間隔,即兩個連續NTP報文之間的時間間隔,單位爲秒
Now:最近一次接收到NTP報文或更新本地時間到現在的時間間隔
Offset:系統時鐘相對於參考時鐘的時鐘偏移,單位爲毫秒
delay disper:統時鐘相對於參考時鐘的最大誤差,單位爲毫秒
[12345]:1:系統選中的時間服務器,即當前與設備進行時間同步的時間服務器
2:該時間服務器的時鐘層數小於等於15
3:該時間服務器的時鐘通過了時鐘選擇算法
4:該時間服務器的時鐘爲候選時鐘
5:該時間服務器的時鐘是配置命令指定的
# dis ntp status參數說明
Clock status: 同步狀態,synchronized:系統時間已同步,unsynchronized:系統時間未同步
Clock stratum: 系統時鐘的層數
System peer: 系統時鐘選中的時間服務器的IP地址
Local mode: 本地設備的工作模式
unspec:未指定模式
sym_active:主動對等體模式
sym_passive:被動對等體模式
client:客戶端模式
server:服務器模式
broadcast:廣播服務器模式或組播服務器模式
bclient:廣播客戶端模式或組播客戶端模式)
Reference clock ID: 參考時鐘ID
Leapindicator: 告警狀態
00:正常狀態
01:閏秒標誌,表示一天中的最後一分鐘有 61 秒
10:閏秒標誌,表示一天中的最後一分鐘有 59 秒
11:時間未被同步的告警狀態
Clock jitter: 系統時鐘相對於參考時鐘的偏移量,單位爲秒
Stability: 時鐘頻率的穩定性,取值越小,時鐘頻率越穩定
Clock precision: 系統時鐘的精度
Rootdelay: 本地設備到主時間服務器的往返時延,單位爲毫秒
Rootdispersion: 系統時鐘相對主參考時鐘的最大誤差,單位爲毫秒
Reference time: 參考時間戳
#
<ntp-srv>dis ntp sessions
source reference stra reach poll now offset delay disper
*******************************************************************************[12345]LOCAL(0) LOCL 0 255 64 8 0.0000 0.0000 0.9460
Notes: 1 source(master), 2 source(peer), 3selected, 4 candidate, 5 configured.
Total sessions: 1
#
#時鐘源(source)爲本地時鐘(LOCAL(0)),參考時鐘LOCL,時鐘層數爲0(本地)
#
<ntp-srv>dis ntp status
Clock status: synchronized
Clock stratum: 1
System peer: LOCAL(0)
Local mode: client
Reference clock ID: LOCL
Leapindicator: 00
Clock jitter: 0.000031 s
Stability: 0.000 pps
Clock precision: 2^-15
Rootdelay: 0.00000 ms
Rootdispersion: 11.16943 ms
Reference time: dcd54bbf.b54d3a2e Sun, May 28 2017 13:14:39.708
#
#時間狀態(Clock status)已同步(synchronized),時鐘層數爲1,服務器IP爲本地(LOCAL(0)),服務器模式爲客戶端(client,華三默認爲客戶端)參考時鐘爲本地(LOCL)
ntp-client01
<ntp-client01>dis ntp sessions
source reference stra reach poll now offset delay disper
*******************************************************************************[12345]88.1.1.254 LOCL 1 255 64 271 111.87 0.9307 0.0762
Notes: 1 source(master), 2 source(peer), 3selected, 4 candidate, 5 configured.
Total sessions: 1
#
#時鐘源(source)爲ntp-srv(88.1.1.254),參考時鐘LOCL,時鐘層數爲1,因此可以看出dis ntp session的參考時鐘指的是時鐘源的參考時鐘,時鐘層數也是指時鐘源的時鐘層數
#
<ntp-client01>dis ntp status
Clock status: synchronized
Clock stratum: 2
System peer: 88.1.1.254
Local mode: client
Reference clock ID: 88.1.1.254
Leapindicator: 00
Clock jitter: 0.011368 s
Stability: 0.000 pps
Clock precision: 2^-15
Rootdelay: 0.93079 ms
Rootdispersion: 138.96179 ms
Reference time: dcd550a1.77781a5e Sun, May 28 2017 13:35:29.466
<ntp-client01>
#
#時鐘層數爲2,服務器IP爲88.1.1.254,參考時鐘爲88.1.1.254
dis ntp session的時鐘層數也是指本地的時鐘層數
#
ntp-client02
<ntp-client02>dis ntp sessions
source reference stra reach poll now offset delay disper
*******************************************************************************
[12345]88.1.1.254 LOCL 1 255 64 31 960.74 1.9531 2.3040
Notes: 1 source(master), 2 source(peer), 3selected, 4 candidate, 5 configured.
Total sessions: 1
<ntp-client02>dis ntp st
<ntp-client02>dis ntp status
Clock status: synchronized
Clock stratum: 2
System peer: 88.1.1.254
Local mode: client
Reference clock ID: 88.1.1.254
Leapindicator: 00
Clock jitter: 0.945007 s
Stability: 0.000 pps
Clock precision: 2^-15
Rootdelay: 1.95313 ms
Rootdispersion: 1919.72351 ms
Reference time: dcd5538c.1df3f67c Sun, May 28 2017 13:47:56.117
<ntp-client02>
#
Dis ntp session與clint01相同
dis ntp status的時鐘層數爲2,因此時鐘層數與拓撲位置無關
#
ntp-client03
<ntp-client03>dis ntp sessions
source reference stra reach poll now offset delay disper
*******************************************************************************
[12345]88.1.1.1 88.1.1.254 2 255 64 44 1192.9 1.0986 4.8522
Notes: 1 source(master), 2 source(peer), 3selected, 4 candidate, 5 configured.
Total sessions: 1
<ntp-client03>dis ntp status
Clock status: synchronized
Clock stratum: 3
System peer: 88.1.1.1
Local mode: client
Reference clock ID: 88.1.1.1
Leapindicator: 00
Clock jitter: 0.517822 s
Stability: 0.000 pps
Clock precision: 2^-15
Rootdelay: 2.09045 ms
Rootdispersion: 1800.03357 ms
Reference time: dcd55428.a6a220e1 Sun, May 28 2017 13:50:32.650
<ntp-client03>
#
#dis ntp sesion時鐘源(source)爲ntp-client01(88.1.1.1),參考時鐘ntp-srv88.1.1.254,時鐘層數爲2(時鐘源88.1.1.1的時鐘層數)
# dis ntp status時鐘層數爲3(時鐘源+1),服務器爲時鐘源8.1.1.1,參考時鐘也爲時鐘源8.1.1.1
#
ntp-client04
<ntp-client04>dis ntp sessions
source reference stra reach poll now offset delay disper
*******************************************************************************
[12345]88.1.1.254 LOCL 1 255 64 50 -98.51 1.8768 2.4719
[5]88.1.1.1 INIT 16 0 64 - 0.0000 0.0000 0.0000
[5]88.1.1.2 INIT 16 0 64 - 0.0000 0.0000 0.0000
Notes: 1 source(master), 2 source(peer), 3selected, 4 candidate, 5 configured.
Total sessions: 3
<ntp-client04>dis ntp status
Clock status: synchronized
Clock stratum: 2
System peer: 88.1.1.254
Local mode: client
Reference clock ID: 88.1.1.254
Leapindicator: 00
Clock jitter: 0.153503 s
Stability: 0.000 pps
Clock precision: 2^-15
Rootdelay: 1.87683 ms
Rootdispersion: 266.90674 ms
Reference time: dcd555eb.f296693d Sun, May 28 2017 13:58:03.947
<ntp-client04>
#
#爲一臺設備指定多個時間服務器,設備根據時鐘層數等參數進行時鐘過濾和選擇,從多個時間服務器中選擇最優的時鐘,與其同步
#
NTP切換
1級時鐘源down
如果2級時鐘源還未感知或未切換
<ntp-client01>dis ntp sessions
source reference stra reach poll now offset delay disper
*******************************************************************************
[12345]88.1.1.254 LOCL 1 224 64 358 -101.1 1.0986 8.5144
Notes: 1 source(master), 2 source(peer), 3selected, 4 candidate, 5 configured.
那麼3級時鐘源重啓後依然會同步到2級時鐘源(配置多個ntp的會切換到2級)
<ntp-client04>dis ntp sessions
source reference stra reach poll now offset delay disper
*******************************************************************************
[5]88.1.1.254 INIT 16 0 64 - 0.0000 0.0000 0.0000
[245]88.1.1.1 88.1.1.254 2 120 64 203 2546.3 1.0070 0.9765
[12345]88.1.1.2 88.1.1.254 2 31 64 64 -548.1 2.7465 2.2277
Notes: 1 source(master), 2 source(peer), 3selected, 4 candidate, 5 configured.
Total sessions: 3
#
如果二級時鐘源感知到一級時鐘源down
<ntp-client02>dis ntp sessions
source reference stra reach poll now offset delay disper
*******************************************************************************
[5]88.1.1.254 INIT 16 0 64 - 0.0000 0.0000 0.0000
Notes: 1 source(master), 2 source(peer), 3selected, 4 candidate, 5 configured.
Total sessions: 1
#
那麼3級時鐘源也會down
<ntp-client04>dis ntp sessions
source reference stra reach poll now offset delay disper
*******************************************************************************
[5]88.1.1.254 INIT 16 0 64 - 0.0000 0.0000 0.0000
[5]88.1.1.1 INIT 16 0 64 - 0.0000 0.0000 0.0000
[5]88.1.1.2 INIT 16 0 64 - 0.0000 0.0000 0.0000
Notes: 1 source(master), 2 source(peer), 3selected, 4 candidate, 5 configured.
總結
當NTP時鐘源爲本地時,假設指定本地時鐘層數爲1,則
本地時鐘dis ntp sessions時,顯示時鐘源和參考時鐘都爲本地(local),顯示時鐘層數爲0
本地時鐘dis ntp status時,會顯示本地時鐘的的模式爲local,時鐘服務器和參考時鐘都爲本地(local)
二級時鐘以一級時鐘源爲服務器時,
dis ntp session的參考時鐘指的是時鐘源的參考時鐘,時鐘層數也是指時鐘源的時鐘層數,會顯示時鐘源的參考時鐘
dis ntp status的時鐘層數也是指本地的時鐘層數,時鐘服務器和參考時鐘爲一級時鐘源
#
三級時鐘以二級時鐘源爲服務器時,
dis ntp session的參考時鐘指的是時鐘源的參考時鐘,時鐘層數也是指時鐘源的時鐘層數,會顯示時鐘源的參考時鐘
dis ntp status的時鐘層數也是指本地的時鐘層數,時鐘服務器和參考時鐘爲二級時鐘源
當某臺設備配置多個ntp服務器時,即指向1級時鐘源,也指向2級時鐘源,
根據優先策略會優先選擇一級時鐘源同步
當一級時鐘源down,二級時鐘源還未切換爲init狀態時,新上的設備(配置多個ntp)會作爲3級時鐘源同步2級時鐘源時間
當一級時鐘源down,二級時鐘源感知到切換到init狀態時,三級時鐘源和新上(配置多個ntp)設備也會init,不會以時鐘源的本地時鐘同步
除非2級時鐘源上配置ntp-servicerefclock-master 2,該命令將配置該設備的時鐘源位本地且層數爲2.
但當2級時鐘源上配置這條命令後,如果一級時鐘源up,2級時鐘源也是一本地時鐘爲源。
cisco通過對一級時鐘源都配置prefer參數,並在本地配置master 2可以實現當一級時鐘源down時,三級時鐘源可以以二級時鐘源的本地時間同步