在大型的网络中,如果依靠管理员手工配置来修改网络中各台设备的系统时间,不但工作量巨大,而且也不能保证时间的精确性。NTP(Network Time Protocol,网络时间协议)可以用来在分布式时间服务器和客户端之间进行时间同步,使网络内所有设备的时间保持一致,并提供较高的时间同步精度。NTP采用的传输层协议为UDP,使用的UDP端口号为123。
NTP的基本工作原理如图1-1所示。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。
·Device B作为NTP时间服务器,即Device A与Device B的时间同步。
·NTP报文从Device A到Device B、从Device B到Device A单向传输所需要的时间均为1秒。
·Device B处理NTP报文所需的时间是1秒。
图1-1 NTP基本工作原理图
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秒。
·Device A相对Device B的时间差Offset = ((T2 – T1) + (T3 – T4)) / 2 = 1小时。
这样,Device A就能够根据这些信息来设定自己的时间,使之与Device B的时间同步。
以上内容只是对NTP工作原理的一个粗略描述,详细内容请参阅相关的协议规范。
| 模式 | 工作过程 | 时间同步方向 | 应用场合 |
|---|---|---|---|
| 客户端/服务器模式 | 客户端上需要手工指定NTP服务器的地址。客户端向NTP服务器发送NTP时间同步报文。NTP服务器收到报文后会自动工作在服务器模式,并回复应答报文 | · 客户端能够与NTP服务器的时间同步 | 该模式通常用于下级的设备从上级的时间服务器获取时间同步 |
| 如果客户端可以从多个时间服务器获取时间同步,则客户端收到应答报文后,进行时钟过滤和选择,并与优选的时钟进行时间同步 | · NTP服务器无法与客户端的时间同步 | ||
| 对等体模式 | 主动对等体(Symmetric active peer)上需要手工指定被动对等体(Symmetric passive peer)的地址。主动对等体向被动对等体发送NTP时间同步报文。被动对等体收到报文后会自动工作在被动对等体模式,并回复应答报文 | ·主动对等体和被动对等体的时间可以互相同步 | 该模式通常用于同级的设备间互相同步,以便在同级的设备间形成备份。如果某台设备与所有上级时间服务器的通信出现故障,则该设备仍然可以从同级的时间服务器获得时间同步 |
| 如果主动对等体可以从多个时间服务器获取时间同步,则主动对等体收到应答报文后,进行时钟过滤和选择,并与优选的时钟进行时间同步 | ·如果双方的时钟都处于同步状态,则层数大的时钟与层数小的时钟的时间同步 | ||
| 广播模式 | 广播服务器周期性地向广播地址255.255.255.255发送NTP时间同步报文。广播客户端侦听来自广播服务器的广播报文,根据接收的广播报文将设备的时间与广播服务器的时间进行同步 | ·广播客户端能够与广播服务器的时间同步 | 广播服务器广播发送时间同步报文,可以同时同步同一个子网中多个广播客户端的时间。如图1-2所示,使用同一个时间服务器为同一个子网中的大量设备提供时间同步时,可以使用广播模式,以简化网络配置 |
| 广播客户端接收到广播服务器发送的第一个NTP报文后,会与广播服务器进行报文交互,以获得报文的往返时延,为时间同步提供必要的参数。之后,只有广播服务器单方向发送报文 | · 广播服务器无法与广播客户端的时间同步 | 由于只有广播服务器单方向发送报文,广播模式的时间准确度不如客户端/服务器模式和对等体模式 | |
| 组播模式 | 组播服务器周期性地向指定的组播地址发送NTP时间同步报文。客户端侦听来自服务器的组播报文,根据接收的组播报文将设备的时间与组播服务器的时间进行同步 | · 组播客户端能够与组播服务器的时间同步 | 组播模式对广播模式进行了扩展,组播服务器可以同时为同一子网、不同子网的多个组播客户端提供时间同步 |
| · 组播服务器无法与组播客户端的时间同步 | 组播模式的时间准确度不如客户端/服务器模式和对等体模式 |
#
ntp server disable
ntp ipv6 server disable
ntp server source-interface all disable
ntp ipv6 server source-interface all disable
ntp unicast-server 192.168.1.1
ntp unicast-server 192.168.1.2
ntp access query 2001
ntp source-interface LoopBack1
#
acl number 2001
rule 0 deny
#
<ASW-208-H05-2.AM13>dis ntp status
clock status: unsynchronized
clock stratum: 16
reference clock ID: none
nominal frequency: 100.0000 Hz
actual frequency: 100.0000 Hz
clock precision: 2^16
clock offset: 0.0000 ms
root delay: 0.00 ms
root dispersion: 0.00 ms
peer dispersion: 0.00 ms
reference time: 00:00:00.000 UTC Jan 1 1900(00000000.00000000)
<ASW-204-D06-1.AM12> dis ntp statistics packet
……
[2024-07-09 20:36:13+08:00]From Peer 192.168.1.1(Local mode: client/configured) Access denied.
ntp access query 2001
#
acl number 2001
rule 0 deny
该命令拒绝了server 回复报文。
下面详细介绍:
display ntp-service sessions命令用来查看本地NTP维护的所有会话信息。
display ntp-service sessions [ verbose ]
查看本地设备的NTP会话信息。
<Huawei> display ntp-service sessions
clock source: 127.127.1.0
clock stratum: 1
clock status:configured, insane, invalid
reference clock ID: LOCAL(0)
reach: 377
poll: 64
now: 2
offset: 0.0000 ms
delay: 0.00 ms
disper: 0.94 ms
| 项目 | 描述 |
|---|---|
| clock source | 时钟源地址。 |
| clock stratum | 时钟源的层数。时钟层数决定了时钟的准确度,取值范围为1~16,层数为1的时钟准确度最高,准确度从1到16依次递减,层数为16的时钟处于未同步状态,不能作为参考时钟。 |
| clock status | 指定时钟状态。其中: configured:表示该会话是配置命令所建立的 master:表示该会话对应的时钟源是当前系统的主时钟源 selected:表示该会话对应的时钟源通过了时钟选择算法 candidate:表示该会话对应的时钟源为候选时钟源 sane:表示该会话对应的时钟源通过健全验证 insane:表示该会话对应的时钟源未通过健全验证 valid:表示该会话对应的时钟源是有效的(通过验证、处于同步状态、层数有效、根延时/离差未越界等) invalid:表示该会话对应的时钟源是无效的 unsynced:表示该会话对应的时钟源未同步或层数非法 |
| reference clock ID | 当本地系统已被同步到一个远程NTP服务器或某个时钟源时,指示远程服务器的地址或时钟源的标识。 |
| reach | 时钟源的可达性计数,0表示时钟源不可达。 |
| poll | NTP报文轮询间隔,即两个连续NTP报文之间的时间间隔,单位为秒。 |
| now | 最后一次同步到现在的时间间隔。 |
| offset | 表示相对上一级时钟源的时差。 |
| delay | 表示相对上一级时钟源的延时。 |
| disper | 表示相对于上一级时钟源的离差。 |
查看本地设备NTP会话的详细信息。
<Huawei> display ntp-service sessions verbose
clock source: 127.127.1.0
clock stratum: 1
clock status:configured, master, sane, valid
reference clock ID: LOCAL(0)
local mode: client, local poll: 6
peer mode: server, peer poll: 6
offset: 0.0000 ms,delay: 0.00 ms, disper: 0.88 ms
root delay: 0.00 ms, root disper: 10.00 ms
reach: 37, sync dist: 0.011, sync state: 3
precision: 2^18, version: 3, peer interface: InLoopBack0
reftime: 16:46:51.054 UTC Jul 28 2012(D3BE95FB.0DFB3FA6)
orgtime: 16:46:51.054 UTC Jul 28 2012(D3BE95FB.0DFB3FA6)
rcvtime: 16:46:51.054 UTC Jul 28 2012(D3BE95FB.0DFBF833)
xmttime: 16:46:51.054 UTC Jul 28 2012(D3BE95FB.0DFADAFD)
filter delay : 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
filter offset: 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
filter disper: 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
reference clock status: working abnormally
timecode:
display ntp-service status命令用来查看NTP的状态信息。
display ntp-service status
查看NTP运行状态信息。
<Huawei> display ntp-service status
clock status: synchronized
clock stratum: 2
reference clock ID: LOCAL(0)
nominal frequency: 60.0002 Hz
actual frequency: 60.0002 Hz
clock precision: 2^18
clock offset: 0.0000 ms
root delay: 0.00 ms
root dispersion: 0.00 ms
peer dispersion: 10.00 ms
reference time: 15:51:36.259 UTC Apr 25 2012(C6179088.426490A3)
| 项目 | 描述 |
|---|---|
| clock status | 表示时钟状态。 synchronized:本地时钟被同步到一个NTP服务器或时钟源。 unsynchronized:本地时钟未被同步到任何一个NTP服务器。 |
| clock stratum | 表示本地时钟所处的NTP层数。 |
| reference clock ID | 表示时钟源。 当本地时钟已被同步到一个远程NTP服务器或某个时钟源时,指示远程NTP服务器的地址或时钟源的标识。 如果采用本地时钟作为参考时钟,将显示Local。 如果时钟状态是unsynchronized,将显示None。 |
| nominal frequency | 表示本地时钟的标称频率。 |
| actual frequency | 表示本地时钟的实际频率。 |
| clock precision | 表示本地时钟的精度。 |
| clock offset | 表示本地时钟相对参考时钟的偏差。 |
| root delay | 表示本地时钟相对主参考时钟总的系统延迟。 |
| root dispersion | 表示本地时钟相对主参考时钟的系统离差。 |
| peer dispersion | 表示本地时钟和远程NTP对等体时钟的离差。 |
| reference time | 表示参考时间戳。 |
display ntp-service trace命令用来查看从本地设备到参考时钟源的路径。
display ntp-service trace
查看从本地设备回溯到参考时钟源的各个NTP服务器的简要信息。
<Huawei> display ntp-service trace
server 10.0.0.1,stratum 5, offset 0.024099, synch distance 0.06337
server 172.16.1.1,stratum 4, offset 0.028786, synch distance 0.04575
server 192.168.1.1,stratum 3, offset 0.035199, synch distance 0.03075
server 192.168.20.1,stratum 2, offset 0.039855, synch distance 0.01096
refid 127.127.1.0
ntp-service access命令用来设置本地设备NTP的访问控制权限。
undo ntp-service access命令用来取消设置的访问控制权限。
缺省情况下,没有配置访问控制权限。
ntp-service access { peer | query | server | synchronization | limited } { acl-number | ipv6 acl6-number } *
undo ntp-service access { peer | query | server | synchronization | limited } [ ipv6 | all ]
undo ntp-service access { peer | query | server | synchronization | limited } [ acl-number | ipv6 acl6-number ] *
设置允许匹配ACL 2000的peer可以对本地设备进行时间请求、查询控制、时间同步。
<Huawei> system-view
[Huawei] ntp-service access peer 2000
设置允许匹配ACL 2002的server对本地设备进行时间请求、查询控制。
<Huawei> system-view
[Huawei] ntp-service access server 2002
ntp-service authentication complexity-check disable命令用来去使能NTP认证密码复杂度检查功能。
undo ntp-service authentication complexity-check disable命令用来使能NTP认证密码复杂度检查功能。
缺省情况下,NTP认证密码复杂度检查功能处于使能状态。
ntp-service authentication complexity-check disable
undo ntp-service authentication complexity-check disable
去使能NTP认证密码复杂度检查功能。
<Huawei> system-view
[Huawei] ntp-service authentication complexity-check disable
ntp-service authentication enable命令用来设置NTP身份认证功能。
undo ntp-service authentication enable命令用来取消身份认证功能。
缺省情况下,没有配置身份认证功能。
ntp-service authentication enable
undo ntp-service authentication enable
使能NTP身份认证功能。
<Huawei> system-view
[Huawei] ntp-service authentication enable
ntp-service authentication-keyid命令用来设置NTP认证密钥。
undo ntp-service authentication-keyid命令用来取消NTP认证密钥。
缺省情况下,没有配置验证密钥。
ntp-service authentication-keyid key-id authentication-mode { md5 | hmac-sha256 } [ cipher ] password
undo ntp-service authentication-keyid key-id
设置HMAC-SHA256身份验证密钥,密钥ID号为10,密钥为BetterKey。
<Huawei> system-view
[Huawei] ntp-service authentication-keyid 10 authentication-mode hmac-sha256 BetterKey
ntp-service broadcast-client命令用来配置NTP广播客户模式。
undo ntp-service broadcast-client命令用来取消NTP广播客户模式。
缺省情况下,未配置NTP广播客户端模式。
ntp-service broadcast-client
undo ntp-service broadcast-client
配置在GE1/0/0接口上接收NTP广播消息。
<Huawei> system-view
[Huawei] interface gigabitethernet 1/0/0
[Huawei-GigabitEthernet1/0/0] ip address 10.1.1.1 24
[Huawei-GigabitEthernet1/0/0] ntp-service broadcast-client
ntp-service broadcast-server命令用来配置NTP广播服务器模式。
undo ntp-service broadcast-server命令用来取消NTP广播服务器模式。
缺省情况下,未配置广播服务器模式。
ntp-service broadcast-server [ version number | authentication-keyid key-id ] *
undo ntp-service broadcast-server
在GE1/0/0接口上发送NTP广播消息包,NTP版本号为2,用4号密钥加密。
<Huawei> system-view
[Huawei] interface gigabitethernet 1/0/0
[Huawei-GigabitEthernet1/0/0] ip address 10.1.1.1 24
[Huawei-GigabitEthernet1/0/0] ntp-service broadcast-server version 2 authentication-keyid 4
ntp-service cwmp unicast-server命令用来配置CWMP协议定义的NTP服务器。
undo ntp-service cwmp unicast-server命令用来删除CWMP协议定义的NTP服务器。
缺省情况下,未配置CWMP协议定义的NTP服务器。
ntp-service cwmp unicast-server unicast-server { first | second }
undo ntp-service cwmp unicast-server unicast-server
配置服务器10.10.1.1为第一NTP服务器。
<Huawei> system-view
[Huawei] ntp-service cwmp unicast-server 10.10.1.1 first
ntp-service discard命令用来配置NTP发送报文的最小时间间隔和平均时间间隔。
undo ntp-service discard命令用来取消NTP发送报文的最小时间间隔和平均时间间隔。
缺省情况下,NTP发送报文的最小时间间隔为2的1次方秒(2秒),平均时间间隔为2的5次方秒(32秒)
ntp-service discard { min-interval min-interval-val | avg-interval avg-interval-val } *
undo ntp-service discard
将发送NTP报文的最小时间间隔和平均时间间隔设置为4,即最小时间间隔和平均时间间隔均设置为16秒。
<Huawei> system-view
[Huawei] ntp-service discard min-interval 4 avg-interval 4
ntp-service enable命令用来使能本地设备的NTP功能。
undo ntp-service enable命令用来去使能本地设备的NTP功能。
缺省情况下,本地设备的NTP功能处于使能状态。
ntp-service enable
undo ntp-service enable
使能本地设备的NTP功能。
<Huawei> system-view
[Huawei] ntp-service enable
ntp-service in-interface disable命令用于禁止接口接收NTP报文。
undo ntp-service in-interface disable命令用于使能接口接收NTP报文。
缺省情况下,接口可以接收NTP报文。
ntp-service [ ipv6 ] in-interface disable
undo ntp-service [ ipv6 ] in-interface disable
禁止GE1/0/0接口接收NTP报文。
<Huawei> system-view
[Huawei] interface gigabitethernet 1/0/0
[Huawei-GigabitEthernet1/0/0] ntp-service in-interface disable
ntp-service kod-enable命令用来使能KOD功能。
undo ntp-service kod-enable命令用来去使能KOD功能。
缺省情况下,没有使能KOD功能。
ntp-service kod-enable
undo ntp-service kod-enable
使能KOD。
<Huawei> system-view
[Huawei] ntp-service kod-enable
ntp-service manycast-client命令用来配置NTP多播客户端模式。
undo ntp-service manycast-client命令用来取消NTP多播客户端模式。
缺省情况下,没有使能NTP多播客户端模式。
ntp-service manycast-client [ ip-address | ipv6 [ ipv6-address ] ] [ authentication-keyid key-id | ttl ttl-number ] *
undo ntp-service manycast-client [ ip-address | ipv6 [ ipv6-address ] ]
配置GE1/0/0接口接收NTP多播报文。多播IPv6报文的组播地址为FF0E::111。
<Huawei> system-view
[Huawei] interface gigabitethernet 1/0/0
[Huawei-GigabitEthernet1/0/0] ntp-service manycast-client ipv6 FF0E::111 authentication-keyid 6
ntp-service manycast-server命令用来配置多播服务器模式。
undo ntp-service manycast-server命令用来取消NTP多播服务器模式。
缺省情况下,没有配置NTP多播服务器模式。
ntp-service manycast-server [ ip-address | ipv6 [ ipv6-address ] ]
undo ntp-service manycast-server [ ip-address | ipv6 [ ipv6-address ] ]
配置GE1/0/0接口为服务器上的接口。该接口用来回应组播地址的多播客户端请求。
<Huawei> system-view
[Huawei] interface gigabitethernet 1/0/0
[Huawei-GigabitEthernet1/0/0] ntp-service manycast-server ipv6 FF0E::111
ntp-service max-dynamic-sessions命令用来设置本地允许建立的最大NTP动态会话数。
undo ntp-service max-dynamic-sessions命令用来恢复本地允许建立的最大NTP动态会话数为缺省值。
缺省情况下,最多允许建立100个NTP动态会话。
ntp-service max-dynamic-sessions number
undo ntp-service max-dynamic-sessions
设置本地最多允许建立50个NTP动态会话。
<Huawei> system-view
[Huawei] ntp-service max-dynamic-sessions 50
ntp-service multicast-client命令用来配置NTP组播客户模式。
undo ntp-service multicast-client命令用来取消NTP组播客户模式。
缺省情况下,没有配置组播客户模式。
ntp-service multicast-client [ ip-address | ipv6 [ ipv6-address ] ]
undo ntp-service multicast-client [ ip-address | ipv6 [ ipv6-address ] ]
配置在GE1/0/0接口上接收NTP组播消息包,组播消息包对应的组播组地址为224.0.1.2。
<Huawei> system-view
[Huawei] interface gigabitethernet 1/0/0
[Huawei-GigabitEthernet1/0/0] ip address 10.1.1.1 24
[Huawei-GigabitEthernet1/0/0] ntp-service multicast-client 224.0.1.2
ntp-service multicast-server命令用来配置在当前接口发送NTP组播消息包,以及本地设备运行在NTP组播服务器模式。
undo ntp-service multicast-server命令用来取消NTP组播服务器的配置。
缺省情况下,没有配置组播服务器模式。
ntp-service multicast-server [ ip-address ] [ version number | authentication-keyid key-id | ttl ttl-number ] *
ntp-service multicast-server [ ipv6 [ ipv6-address ] ] [ authentication-keyid key-id | ttl ttl-number ] *
undo ntp-service multicast-server [ ip-address | ipv6 [ ipv6-address ] ]
配置在GE1/0/0接口上发送NTP组播消息包,组播组地址为224.0.1.1,进行加密的密钥ID是4,NTP版本号是3。
<Huawei> system-view
[Huawei] interface gigabitethernet 1/0/0
[Huawei-GigabitEthernet1/0/0] ip address 10.1.1.1 24
[Huawei-GigabitEthernet1/0/0] ntp-service multicast-server 224.0.1.1 authentication-keyid 4 version 3
ntp-service refclock-master命令用来设置本地时钟作为NTP主时钟,为其它设备提供同步时间。
undo ntp-service refclock-master命令用来取消NTP主时钟设置。
缺省情况下,没有设置NTP主时钟。
ntp-service refclock-master [ ip-address ] [ stratum ]
undo ntp-service refclock-master [ ip-address ]
设置本地设备时钟作为NTP主时钟,层数为3。
<Huawei> system-view
[Huawei] ntp-service refclock-master 3
ntp-service reliable authentication-keyid命令用来指定密钥是可信的。
undo ntp-service reliable authentication-keyid命令用来取消指定密钥为可信的配置。
缺省情况下,没有设置可信验证密钥。
ntp-service reliable authentication-keyid key-id
undo ntp-service reliable authentication-keyid key-id
设置启用NTP身份验证,采用HMAC-SHA256加密,密钥ID为37,密钥为BetterKey,指定该密钥为可信密钥。
<Huawei> system-view
[Huawei] ntp-service authentication enable
[Huawei] ntp-service authentication-keyid 37 authentication-mode hmac-sha256 BetterKey
[Huawei] ntp-service reliable authentication-keyid 37
ntp-service source-interface命令用来指定本地发送NTP报文的源接口。
undo ntp-service source-interface命令用来取消本地发送NTP报文的源接口。
缺省情况下,没有指定本地发送NTP报文源接口,即根据路由选择NTP报文的源接口。
ntp-service [ ipv6 ] source-interface { interface-type interface-number | interface-name } [ vpn-instance vpn-instance-name ]
undo ntp-service [ ipv6 ] source-interface { interface-type interface-number | interface-name } [ vpn-instance vpn-instance-name ]
指定NTP所有输出报文的源IP地址都用接口GE1/0/0的IP地址。
<Huawei> system-view
[Huawei] ntp-service source-interface gigabitethernet 1/0/0
ntp-service unicast-peer命令用来配置NTP对等体模式。
undo ntp-service unicast-peer命令用来取消NTP对等体模式。
缺省情况下,未配置NTP对等体模式。
ntp-service unicast-peer ip-address [ version number | authentication-keyid key-id | maxpoll max-number | minpoll min-number | source-interface { interface-type interface-number | interface-name } | vpn-instance vpn-instance-name | preference ] *
ntp-service unicast-peer ipv6 ipv6-address [ authentication-keyid key-id | maxpoll max-number | minpoll min-number | source-interface { interface-type interface-number | interface-name } | vpn-instance vpn-instance-name | preference ] *
undo ntp-service unicast-peer { ip-address | ipv6 ipv6-address } [ vpn-instance vpn-instance-name ]
本地被配置成由peer 10.10.1.1提供同步时间,本地对等体也能为peer提供同步时间,版本号为3。NTP报文的IP地址从GE1/0/0获得。
<Huawei> system-view
[Huawei] ntp-service unicast-peer 10.10.1.1 version 3 source-interface gigabitethernet 1/0/0
ntp-service unicast-server命令用来配置NTP服务器模式。
undo ntp-service unicast-server命令用来取消NTP服务器模式。
缺省情况下,未配置NTP服务器模式。
ntp-service unicast-server server [ version number | authentication-keyid key-id | maxpoll max-number | minpoll min-number | source-interface { interface-type interface-number | interface-name } | vpn-instance vpn-instance-name | preference ] *
ntp-service unicast-server ipv6 ipv6-address [ authentication-keyid key-id | maxpoll max-number | minpoll min-number | source-interface { interface-type interface-number | interface-name } | vpn-instance vpn-instance-name | preference ] *
undo ntp-service unicast-server { server | ipv6 ipv6-address } [ vpn-instance vpn-instance-name ]
使用实例
本地设备被配置成由服务器10.10.1.1提供同步时间,版本号为3。
<Huawei> system-view
[Huawei] ntp-service unicast-server 10.10.1.1 version 3
本地设备被配置成由属于VPN实例“abc”的服务器10.10.1.1提供同步时间。
<Huawei> system-view
[Huawei] ntp-service unicast-server 10.10.1.1 vpn-instance abc