常用命令:
——ntp access-group: 该全局命令用于路由器N T P服务的访问控制。
——ntp authenticate: 是一个全局命令,它启用N T P身份验证。
——ntp authentication-key: 该全局命令用于定义N T P身份验证的键值。
——ntp broadcast: 是一个接口命令,用于指定一特定接口来发送N T P广播包。
——ntp broadcast client: 是一个接口命令,使路由器通过特定接口来接收N T P广播包。
——ntp broadcast delay: 是一个全局命令,它用于设定数据包在路由器和N T P服务器之间一个回程所需时间的估计值。
——ntp clock-period: 这条全局命令不必输入,当使用N T P进行系统时钟同步时,路由器将自动产生这条命令。
——ntp disable: 这条接口命令使特定接口不接收N T P包。
——ntp master: 这条全局命令用来配置路由器为N T P主时钟,只有当没有可用的外部N T P源或者为测试用途才使用该命令。
——ntp peer: 该全局命令使路由器的系统时钟与其对等体的时钟同步(或对对等体的时钟进行同步)。
——ntp server: 该全局命令使路由器的系统时钟由时间服务器进行同步。
——ntp source: 该全局命令强制路由器在其N T P包中使用特定的源地址。
——ntp trusted-key: 该全局命令用于确认路由器的特定身份验证键值。
——ntp update-calendar: 该全局命令使N T P周期性地更新Cisco 7XXX 系列路由器的日历。
——show ntp status: 是一执行模式命令,用于显示路由器的N T P信息,它可以表明该路由器是通过N T P对等体进行时钟同步还是通过N T P服务器进行同步。
——show ntp association[detail]: 这条执行模式命令显示与N T P有关的信息,如*询周期等。
1. 路由器日志显示时间戳
提问 在路由器 的日志和排错信息里面显示时间
回答
Router#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#service timestamps log datetime localtime
Router(config)#service timestamps debug datetime localtime
Router(config)#end
Router#
注释还可以在命令后面加上show-timezone, msec等参数让时间戳包含时区信息和毫秒级
2. 设置时间
提问 设置路由器时间
回答
内部时钟
Router#clock set 14:27:22 January 29 2006
Router#
高端路由器使用电池保存时间
Router#calendar set 14:34:39 January 29 2006
Router#
注释如果没有电池保护路由器重启时间配置消失,show calendar一方面可以显示目前时钟,也可以用来验证是否有电池保护,内部时钟
和calendar时钟不一致时可以使用clock update-calendar或者clock read-calendar来互相同步
3. 设置时区
提问 设置路由器时区
回答
Router#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#clock timezone EST 5  'clock timezone GMT 8 北京时间
Router(config)#end
Router#
注释 缺省路由器使用UTC就是以前的GMT
4. 夏时制调整
提问 路由器自动对时钟进行夏时制调整
回答
Router#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#clock summer-time EDT date 26 oct 2003 02:00 6 apr 2003 02:00
或者
Router(config)#clock summer-time AEDT recurring last sun oct 02:00 last sun mar 02:00
Router(config)#end
Router#
注释 缺省是没有夏时制的,启用后可以使用show clock detail来验证
5. 时钟同步(NTP)
提问 路由器自动同步网络时间
回答
Router#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#clock timezone EST -5
Router(config)#clock summer-time EDT recurring
Router(config)#ntp server 172.25.1.1
Router(config)#end
Router#
对于不支持NTP的路由器,使用SNTP
Router#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#clock timezone EST -5
Router(config)#clock summer-time EDT recurring
Router(config)#ntp server 172.25.1.1
Router(config)#end
Router#
注释可以使用ntp source loopback0 或者ntp server 10.1.1.1 source Serial 0/0 命令来指定NTP发送的源地址。由于NTP同步的是内
部时钟,所以需要配置ntp update-calendar 来同时同步其calendar时钟
6. 配置NTP 冗余
提问 配置多个NTP服务器的方式来提供冗余
回答
Router#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#clock timezone EST -5
Router(config)#clock summer-time EDT recurring
Router(config)#ntp server 172.25.1.1
Router(config)#ntp server 10.121.33.231
Router(config)#ntp peer 192.168.12.12
Router(config)#end
Router#
7. 设置路由器为网络NTP服务器
提问设置路由器为网络NTP服务器,成为网络的主时钟源
回答
Router#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#clock timezone EST 5
Router(config)#clock summer-time EDT recurring
Router(config)#clock calendar-valid
Router(config)#ntp master 8
Router(config)#end
Router#
注释 这里设置ntp master 8使其成为Stratum level 8,尽量不要配置其为1
8. 调整NTP同步周期
提问 调整多久路由器发送NTP数据包来验证同步
回答
NTP不允许手动修改同步频率,但是内置的算法可以自动调整此频率
注释开始为64秒一个周期,如果网络足够稳定此周期会逐渐增加,最长到1024秒,如下例
Router>show ntp associations
address     ref clock   st when poll reach delay offset  disp
*~172.25.1.1    130.207.244.240  2  440 1024 377 1.6  -3.23   5.6
+~172.25.1.3    204.152.184.72  2  829 1024 377 1.7  8.06   0.9
* master (synced), # master (unsynced), + selected, - candidate, ~ configured
Router>
9. NTP 发送周期性广播包保持更新
提问 工作于广播模式下,不需要周期性去查询
回答
服务器端
Router1#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router1(config)#clock timezone EST -5
Router1(config)#clock summer-time EDT recurring
Router1(config)#ntp server 172.25.1.1
Router1(config)#ntp server 172.25.1.2
Router1(config)#interface FastEthernet0/0
Router1(config-if)#ntp broadcast
Router1(config-if)#end
Router1#
客户端
Router2#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router2(config)#clock timezone EST -5
Router2(config)#clock summer-time EDT recurring
Router2(config)#ntp broadcastdelay 4
Router2(config)#interface Ethernet0
Router2(config-if)#ntp broadcast client
Router2(config-if)#end
Router2#
注释工作于广播模式时间数据包是单方向的,通过broadcastdelay来控制周期,广播模式不妨碍客户端工作于服务器客户端模式
10. NTP发送周期性组播包保持更新
提问 工作于组播模式下,不需要周期性去查询
回答
服务器端
Router1#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router1(config)#clock timezone EST -5
Router1(config)#clock summer-time EDT recurring
Router1(config)#ntp server 172.25.1.1
Router1(config)#ntp server 172.25.1.3
Router1(config)#interface FastEthernet 0/0
Router1(config-if)#ntp multicast 224.0.1.1 ttl 1
Router1(config-if)#end
Router1#
客户端
Router1#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router1(config)#clock timezone EST -5
Router1(config)#clock summer-time EDT recurring
Router1(config)#ntp server 172.25.1.1
Router1(config)#ntp server 172.25.1.3
Router1(config)#interface FastEthernet 0/0
Router1(config-if)#ntp multicast 224.0.1.1 ttl 1
Router1(config-if)#end
Router1#
注释组播相对于广播的好处不用多说了,并且在这个模式的初始客户端会先发送一些单播包来测量延迟,以使时间更准确,需要注意的是不
是所有的设备都支持这种组播模式
11. 基于接口开启NTP
提问路由器配置为NTP服务器,但是某些端口禁止NTP服务
回答
Router#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#interface Serial0/1
Router(config-if)#ntp disable
Router(config-if)#end
Router#
或者
Router#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#access-list 107 deny udp any eq 123 any eq 123
Router(config)#access-list 107 permit ip any any
Router(config)#interface Serial0/1
Router(config-if)#ip access-group 107 in
Router(config-if)#end
Router#
注释控制列表的方式更严格,第一种只是阻止了相应的associations,但阻止不了NTP数据包
12. NTP 认证
提问 鉴权NTP数据包保证安全
回答
服务器端
Router1#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router1(config)#ntp authentication-key 2 md5 neoshi
Router1(config)#ntp authenticate
Router1(config)#ntp trusted-key 2
Router1(config)#end
Router1#
客户端
Router2#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router2(config)#ntp authentication-key 2 md5 neoshi
Router2(config)#ntp authenticate
Router2(config)#ntp trusted-key 2
Router2(config)#ntp server 172.25.1.5 key 2
Router2(config)#end
Router2#
注释 对于广播或者组播模式key配置为ntp broadcast key 2 和ntp multicast key 2
13. 限制NTP Peers数目
提问 限制路由器可以接受的NTP Peers的数目
回答
Router#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#ntp max-associations 30
Router(config)#end
Router#
注释 无
14. 限制Peers
提问 对NTP服务进行更好粒度的控制
回答
Router#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#access-list 88 permit host 172.25.1.1
Router(config)#access-list 88 permit host 10.1.1.1
Router(config)#access-list 99 permit 172.25.0.0 0.0.255.255
Router(config)#access-list 99 permit 10.2.0.0 0.0.255.255
Router(config)#clock timezone EST -5
Router(config)#clock summer-time EDT recurring
Router(config)#ntp server 172.25.1.1 version 3
Router(config)#ntp server 10.1.1.1 version 3
Router(config)#ntp access-group peer 88
Router(config)#ntp access-group serve-only 99
Router(config)#end
Router#
注释路由器只允许内部时钟从ACL88定义的两个服务器中获得同步,同时只有ACL99定义的两个网段的客户端可以从本设备请求时间信息
Use the query-only option to allow only NTP control queries from the listed IP addresses. Control queries are used in lieu of an SNMP management station to monitor the NTP process.
The serve-only option allows only time requests from the IP addresses listed in the access list. This router will not synchronize its clock to the remote system.只允许别人跟自己同步
The serve option allows time requests and control queries. This system will still not synchronize its clock to the remote system.
The peer option allows both time requests and control queries, and it does allow this router to synchronize its clock to the remote system.
15. 设定时钟周期
提问 希望调整自动生成的ntp clock-period xxxxxx 数值
回答
路由器在重启之后会自动生成一个时钟周期来加速再同步,不建议删除或者修改
Router#show running-config | include clock-period
ntp clock-period 17180200
Router#
注释 无
16. 检查NTP状态
提问 查看当前NTP状态
回答
Router>show clock detail
Router>show ntp status
Router>show ntp associations
Router>show ntp associations detail
注释 Router>show clock detail
.15:54:33.079 EST Sun Jan 29 2006
Time source is NTP
此输出前面有个.代表此时钟没有同步
17. NTP排错
提问 解决NTP出错的问题
回答
NTP非常稳定,出问题很大可能性就是连接性的问题
Router#debug ntp packets
注释 Router#debug ntp packet
NTP packets debugging is on
.Mar 21 02:39:18: NTP: xmit packet to 172.25.1.5:
.Mar 21 02:39:18: leap 3, mode 3, version 3, stratum 0, ppoll 64
.Mar 21 02:39:18: rtdel 28C7 (159.286), rtdsp 2444 (141.663), refid AC190101
.Mar 21 02:39:18: ref C043C43F.47A9CD5C (21:30:23.279 EST Wed Mar 20 2003)
.Mar 21 02:39:18: org 00000000.00000000 (19:00:00.000 EST Thu Dec 31 1899)
.Mar 21 02:39:18: rec 00000000.00000000 (19:00:00.000 EST Thu Dec 31 1899)
.Mar 21 02:39:18: xmt C043C656.4DFC7394 (21:39:18.304 EST Wed Mar 20 2003)
.Mar 21 02:39:25: NTP: rcv packet from 172.25.1.5 to 172.16.2.2 on Fa0/0.1:
.Mar 21 02:39:25: leap 3, mode 3, version 3, stratum 0, ppoll 64
.Mar 21 02:39:25: rtdel 286E (157.928), rtdsp 0EC6 (57.709), refid AC190101
.Mar 21 02:39:25: ref C043C4D7.1D633CDE (21:32:55.114 EST Wed Mar 20 2003)
.Mar 21 02:39:25: org 00000000.00000000 (19:00:00.000 EST Thu Dec 31 1899)
.Mar 21 02:39:25: rec 00000000.00000000 (19:00:00.000 EST Thu Dec 31 1899)
.Mar 21 02:39:25: xmt C043C65D.1D0A6CBC (21:39:25.113 EST Wed Mar 20 2003)
.Mar 21 02:39:25: inp C043C65D.1296E3C7 (21:39:25.072 EST Wed Mar 20 2003)
上面是一个debug的输出,从中看到了来自server的数据包显示为stratum 0,代表服务器没有同步,既然上游服务器没有同步,本地服务
器就更不能同步了
18. NTP 日志
提问 记录重要的NTP事件
回答
Router2#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router2(config)#ntp logging
Router2(config)#end
Router2#
注释此命令来自12.3(7)T,下面是一个日志记录
Router2#show logging | include NTP
000019: Jan 29 10:57:52.633 EST: %NTP-5-PEERSYNC: NTP synced to peer 172.25.1.5
000020: Jan 29 10:57:52.637 EST: %NTP-6-PEERREACH: Peer 172.25.1.5 is reachable
000024: Jan 29 11:01:20.653 EST: %NTP-4-PEERUNREACH: Peer 172.25.1.5 is unreachable
000026: Jan 29 11:15:11.985 EST: %NTP-4-UNSYNC: NTP sync is lost
14.19. Extended Daylight Saving Time
注释美国为了节省能源从2007年开始调整了夏时制的设置,此略去
<!--[if !supportLists]-->14.20. <!--[endif]-->NTP 服务器配置
注释 主机配置暂略去
=====================================================================
在cisco 10.40.0.1上建立NTP服务器
NTP服务器上:
conf t
snmp-server community public ro
ntp autentication-key 1 md5 cxzhzx 7
ntp authenticate
ntp trusted-key 1
ntp source vlan40
ntp master 1
end
客户端:
conf t
no ntp
ntp authentication-key 1 md5 cxzhzx 7
ntp authenticate
ntp trusted-key 1
ntp server 10.40.0.1 key 1
end
================================================================
路由器的NTP(网络时钟协议)配置
Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化
,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议***。
配置NTP首先组要创建一个连接。使用下列命令初始化连接的配置:
ntp server ip _address [version number][key key_id][source interface][prefer]
ntp peer ip _address [version number][key key_id][source interface][prefer]
如果路由器将和另一个NTP时钟同步,则建立一个服务器连接。如果路由器不但与另一个设备
同步,而且允许其他设备和该路由器同步,则创建一个对等连接。缺省的版本时3,缺省情况下,没有配置认证key id,源IP地址发送端口
的IP地址,prefer告诉IOS该同步对等体的优先级。
为控制对路由器NTP服务的访问,使用下面的命令:
ntp access-group {query|serve-only|serve|peer} access-list-number
query-only:允许从列出的IP地址发出NTP控制查询。控制查询用在监视NTP进程的SNMP网络管理工作站。
serve-only:允许访问控制列表上的IP地址请求事件,路由器不向远程系统同步时间。
serve:允许时间请求和控制查询,路由器同样不向远程系统同步时间。
peer:允许时请求和控制查询,并且允许路由器从远程系统同步时间。
例:
Seattle
access-list 1 permit 172.16.0.0 0.0.255.255
access-list 2 permit 128.10.39.11
ntp access-group peer 2
ntp access-group serve 1
ntp server 128.105.39.11
Tacoma
ntp server 172.16.1.5
以上配置允许路由器:Settle从一个公共的二级时钟源同步,在与Seattle同一个网络上的另一个路由器Tacoma被允许从Seattle获得同步

NTP 时间时UTC.如果希望路由器保持在另一个时区,你可以使用下列命令保持本地时间:
clock timezone pst -8
clock summer-time PDT recurring
使用ntp update-calendar 命令使路由器用NTP获得的时间更新内部日历。
NTP的认证:
ntp authentication
ntp authentication-key number md5 key
ntp trusted-key number
ntp server ip-address key number
ntp authentication 必须配置在NTP服务器和请求时间同步的路由器。(在全局下)。
ntp authentication-key 需要配置在所有路由器,该命令定义的认证串并将其赋值。
请求时间同步的路由器使用ntp trusyed-key命令配置。该命令列出了ntp authentication-key
命令定义的认证串的数值,该数值必须包含在NTP 同步请求包中。所以ntp trusted-key只
配置在客户路由器端。
key number必须包含在客户的ntp server命令中,该命令将密码包含到客户发往服务器的NTP包中。当路由器看到密码,如果该密码在服务
器中有定义,则将密码包含到发往客户的NTP包中。
例:
Seattle
ntp authencation
ntp authencation-key 10 md5 ntpkey
Tacoma
ntp authencation
ntp authencation-key 10 md5 ntpkey
ntp trusted-key 10
ntp server seattle key 10
Tacoma的ntp server seattle key 10命令指定在Tacoma 与Seattle时钟同步以前,服务器Seattle必须在NTP包中提供10号密码。从Tacoma
的包中,Seattle看到10号密码。
Seattle 中的ntp authentication-key 10 md5 ntpkey命令可以使Seattle在对Tacoma的应答包中包含认证密码10.
排错:
show ntp status
sh ntp association detail