温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

NTP介绍与分析

发布时间:2020-06-28 09:22:49 来源:网络 阅读:2604 作者:网络大神经 栏目:建站服务器

NTP简介

NTPNetwork Time Protocol,网络时间协议)采用的传输层协议为 UDP,使用的 UDP 端口号为 123

NTP介绍与分析

NTP介绍与分析

NTP基本工作原理

NTP的基本工作原理如所示。Device ADevice B通过网络相连,Device ADevice B的时间不同,需要通过NTP实现时间的自动同步。为便于理解,作如下假设:

·     Device ADevice B的时间同步之前,Device A的时间设定为10:00:00 amDevice B的时间设定为11:00:00 am

·     DeviceB作为NTP时间服务器,即Device ADevice B的时间同步。

·     NTP报文从Device ADevice B、从Device BDevice A单向传输所需要的时间均为1秒。

·     DeviceB处理NTP报文所需的时间是1秒。

NTP介绍与分析

Device ADevice B时间同步的工作过程如下:

(1)     Device A发送一个NTP报文给Device B,该报文带有它离开Device A时的时间戳,该时间戳为10:00:00 amT1)。

(2)     当此NTP报文到达Device B时,Device BNTP报文上增加该报文到达Device B时的时间戳,该时间戳为11:00:01 amT2)。

(3)     当此NTP报文离开Device B时,Device B再在NTP报文上增加该报文离开Device B时的时间戳,该时间戳为11:00:02 amT3)。

(4)     Device A接收到该响应报文时,Device A的本地时间为10:00:03 amT4)。

至此,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通过时钟层数来定义时钟的准确度。时钟层数的取值范围为116,取值越小,时钟准确度越高。层数为115的时钟处于同步状态;层数为16的时钟处于未同步状态。

NTP介绍与分析

通常将从权威时钟(如原子时钟)获得时间同步的NTP服务器的层数设置为1,并将其作为主时间服务器,为网络中其他设备的时钟提供时间同步。网络中的设备与主时间服务器的NTP距离,即NTP同步链上NTP服务器的数目,决定了设备上时钟的层数。例如,从主时间服务器获得时间同步的设备的时钟层数为2,即比主时间服务器的时钟层数大1;从时钟层数为2的时间服务器获得时间同步的设备的时钟层数为3,以此类推。

NTP的工作模式

模式

工作过程

时间同步方向

应用场合

客户端/服务器模式

客户端上需要手工指定NTP服务器的地址。客户端向NTP服务器发送NTP时间同步报文。NTP服务器收到报文后会自动工作在服务器模式,并回复应答报文。
 如果客户端可以从多个时间服务器获取时间同步,则客户端收到应答报文后,进行时钟过滤和选择,并与优选的时钟进行时间同步。

客户端能够与NTP服务器的时间同步
NTP
服务器无法与客户端的时间同步

如上图所示,该模式通常用于下级的设备从上级的时间服务器获取时间同步

对等体模式

主动对等体(Symmetricactivepeer)上需要手工指定被动对等体(Symmetricpassivepeer)的地址。主动对等体向被动对等体发送NTP时间同步报文。被动对等体收到报文后会自动工作在被动对等体模式,并回复应答报文。
 如果主动对等体可以从多个时间服务器获取时间同步,则主动对等体收到应答报文后,进行时钟过滤和选择,并与优选的时钟进行时间同步。

主动对等体和被动对等体的时间可以互相同步
如果双方的时钟都处于同步状态,则层数大的时钟与层数小的时钟的时间同步

如上图所示,该模式通常用于同级的设备间互相同步,以便在同级的设备间形成备份。如果某台设备与所有上级时间服务器的通信出现故障,则该设备仍然可以从同级的时间服务器获得时间同步

广播模式

广播服务器周期性地向广播地址255.255.255.255发送NTP时间同步报文。广播客户端侦听来自广播服务器的广播报文,根据接收的广播报文将设备的时间与广播服务器的时间进行同步。
 广播客户端接收到广播服务器发送的第一个
NTP报文后,会与广播服务器进行报文交互,以获得报文的往返时延,为时间同步提供必要的参数。之后,只有广播服务器单方向发送报文

广播客户端能够与广播服务器的时间同步
广播服务器无法与广播客户端的时间同步

广播服务器广播发送时间同步报文,可以同时同步同一个子网中多个广播客户端的时间。
 如上图所示,使用同一个时间服务器为同一个子网中的大量设备提供时间同步时,可以使用广播模式,以简化网络配置由于只有广播服务器单方向发送报文,广播模式的时间准确度不如客户端/服务器模式和对等体模式

组播模式

组播服务器周期性地向指定的组播地址发送NTP时间同步报文。客户端侦听来自服务器的组播报文,根据接收的组播报文将设备的时间与组播服务器的时间进行同步

组播客户端能够与组播服务器的时间同步
组播服务器无法与组播客户端的时间同步

组播模式对广播模式进行了扩展,组播服务器可以同时为同一子网、不同子网的多个组播客户端提供时间同步组播模式的时间准确度不如客户端/服务器模式和对等体模式

 

配置测试

NTP介绍与分析

Ntp-srv88.1.1.254)作为ntp server

Ntp-client0188.1.1.1)作为ntp client,服务指向ntp-srv88.1.1.254

Ntp-client0188.1.1.2)作为ntp client,服务指向ntp-srv88.1.1.254

Ntp-client0188.1.1.3)作为ntp client,服务指向ntp-srv88.1.1.1

Ntp-client0188.1.1.4)作为ntp client,服务指向ntp-srv88.1.1.254以及88.1.1.188.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-srv88.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,服务器IP88.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 sessionclint01相同

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-client0188.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.

 

总结

  1. NTP时钟源为本地时,假设指定本地时钟层数为1,则

本地时钟dis ntp sessions时,显示时钟源和参考时钟都为本地(local),显示时钟层数为0

本地时钟dis ntp status时,会显示本地时钟的的模式为local,时钟服务器和参考时钟都为本地(local

  1. 二级时钟以一级时钟源为服务器时,

dis ntp session的参考时钟指的是时钟源的参考时钟,时钟层数也是指时钟源的时钟层数,会显示时钟源的参考时钟

dis ntp status的时钟层数也是指本地的时钟层数,时钟服务器和参考时钟为一级时钟源

#

  1. 三级时钟以二级时钟源为服务器时,

dis ntp session的参考时钟指的是时钟源的参考时钟,时钟层数也是指时钟源的时钟层数,会显示时钟源的参考时钟

dis ntp status的时钟层数也是指本地的时钟层数,时钟服务器和参考时钟为二级时钟源

  1. 当某台设备配置多个ntp服务器时,即指向1级时钟源,也指向2级时钟源,

根据优先策略会优先选择一级时钟源同步

  • 当一级时钟源down,二级时钟源还未切换为init状态时,新上的设备(配置多个ntp)会作为3级时钟源同步2级时钟源时间

  • 当一级时钟源down,二级时钟源感知到切换到init状态时,三级时钟源和新上(配置多个ntp)设备也会init,不会以时钟源的本地时钟同步

  • 除非2级时钟源上配置ntp-servicerefclock-master 2,该命令将配置该设备的时钟源位本地且层数为2.

  • 但当2级时钟源上配置这条命令后,如果一级时钟源up2级时钟源也是一本地时钟为源。

cisco通过对一级时钟源都配置prefer参数,并在本地配置master 2可以实现当一级时钟源down时,三级时钟源可以以二级时钟源的本地时间同步


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI