初始化命令:
no
en
conf t
no cdp run
interface range e0/0 -3
duplex full
no ip do lo
line co 0
no exec-timeout 0
logg sy
ho R
dhcp
- DHCP用于给设备自动分配IP地址,我们几乎天天用到
- DHCP给设备提供的内容如下
- IP地址
- 子网掩码
- 网关地址
- DNS服务器地址
- 租约时间
拓扑图
R1#conf t
R1(config)#int e0/0
R1(config-if)#ip add 192.168.1.1 255.255.255.0 //ip add dhcp
R1(config-if)#no sh
R1(config-if)#ip dhcp pool cisco # 创建一个地址池
R1(dhcp-config)#network 192.168.1.0 /24 # 在地址池中放一个网段
R1(dhcp-config)#default-router 192.168.1.1 # 设置默认网关
R1(dhcp-config)#dns-server 114.114.114.114 114.114.115.115 # 设置主备DNS
R1(dhcp-config)#lease 1 # 租约时间改为1天
R1(dhcp-config)#exit
R1(config)#ip dhcp excluded-address 192.168.1.1 # 设置排除地址
- 在路由器上查看地址池分配的情况
R1#sh ip dhcp binding
Bindings from all pools not associated with VRF:
IP address Client-ID/ Lease expiration Type
Hardware address/
User name
192.168.1.2 0050.0000.0300 Jan 22 2022 01:59 PM Automatic
192.168.1.3 0050.0000.0400 Jan 22 2022 01:59 PM Automatic
DHCP工作过程
通过对DHCP获取信息的过程抓包,可以得到如下4个数据包
-
Discover 消息是用于客户端向整个内网发送广播,期待DHCP服务器进行回应
这个数据包中的重要内容就是:消息类型,客户端ID,主机名,请求获得的信息
-
Offer 消息是DHCP服务器对客户的回应
这个消息中会回复对方所需要的所有信息
-
Request 这个是客户端确认DHCP服务器的消息
这个消息和第一个消息差不多,但是消息类别变为 Request ,并且会携带请求的IP地址
4.ACK DHCP服务器给客户端的最终确认
这个消息和第二个消息差不多,但是消息类型变为 ACK
DHCP续租
DHCP分配的信息是有有效期的,在DHCP服务器给客户端的数据包中可以明确看到
为了我们做实验方便,我们将DHCP的租约时间减少到只有两分钟,这样就可以清楚的看到续租的过程
并且我们在客户端获得IP之后,就关闭服务端,让客户端无法续租成功,看客户端是怎么去 Rebinding 的
DHCP服务端操作
R1(config)#ip dhcp pool cisco
R1(dhcp-config)#lease 0 0 2 # 租约时间改为2分钟
R1(config)#int e0/0
R1(config-if)#sh # 在客户端获得IP信息之后关闭接口
总结
- 客户端会在租约时间过去 的时候以单播方式请求续租
- 在租约时间过去 的时候以广播的方式请求续租
- 在租约时间过去100%的时候,以广播方式重新发现局域网中的DHCP服务器
DHCP地址绑定- 通过上面的学习,我们知道设备必须提供自己的身份信息才能在dhcp服务端获取地址信息
- 那么想要给设备绑定固定的IP地址,就必须知道设备的身份信息
路由器配置
R1#sh ip dhcp binding
Bindings from all pools not associated with VRF:
IP address Client-ID/ Lease expiration Type
Hardware address/
User name
192.168.1.3 010c.18a5.af90.00 Jun 18 2020 01:23 AM Automatic
R1(config)#ip dhcp pool windows
R1(dhcp-config)#host 192.168.1.100
R1(dhcp-config)#client-identifier 010c.18a5.af90.00
% A binding for this client already exists.
如果绑定的时候出现 % A binding for this client already exists. ,说明你要绑定的这个身份信息在dhcp地址池中已经被占用,先去清理一下
R1#clear ip dhcp binding *
清理完成后,再次绑定,就可以成功了,查看地址池,可以看到windows地址池只有一个IP地址
R1#sh ip dhcp pool
Pool cisco :
Utilization mark (high/low) : 100 / 0
Subnet size (first/next) : 0 / 0
Total addresses : 254
Leased addresses : 0
Pending event : none
1 subnet is currently in the pool :
Current index IP address range Leased addresses
192.168.1.4 192.168.1.1 - 192.168.1.254 0
Pool windows :
Utilization mark (high/low) : 100 / 0
Subnet size (first/next) : 0 / 0
Total addresses : 1
Leased addresses : 1
Pending event : none
0 subnet is currently in the pool :
Current index IP address range Leased addresses
192.168.1.100 192.168.1.100 - 192.168.1.100 1
DNS
查看dns
R1(config)#do sh run | sec dhcp
ip dhcp excluded-address 192.168.1.1
ip dhcp pool cisco
network 192.168.1.0 255.255.255.0
default-router 192.168.1.1
dns-server 114.114.114.114 114.114.115.115
设置dns
R1(config)#ip dhcp pool cisco
R1(dhcp-config)#no dns-server
R1(dhcp-config)#dns-s
R1(dhcp-config)#dns-server 192.168.1.1
域名绑定
R1(config)#ip dns server
R1(config)#ip hos
R1(config)#ip host baidu.com 192.168.1.1
NAT
R1(config)#ip do lo
R1(config)#ip name-server 114.114.114.114
R1(config)#ip nat inside source list 1 interface e0/1 overload
R1(config)#int e0/1
R1(config-if)#ip nat o
R1(config-if)#ip nat outside
R1(config-if)#int e0/0
R1(config-if)#ip nat in
R1(config-if)#ip nat inside
R1(config-if)#
进行静态nat端口映射,访问e0/1口实际访问到就是躲在外网背后的内网ip
R1(config)#ip nat inside source static tcp 192.168.1.100 80 interface e0/1 30081
R1(config)#ip nat inside source static tcp 192.168.1.101 80 interface e0/1 30080
删除这两条nat规则
R1(config)#do clear ip nat translation *
R1(config)#$ce static tcp 192.168.1.2 80 interface Ethernet0/1 30081
R1(config)#$ce static tcp 192.168.1.3 80 interface Ethernet0/1 30080
R1(config)#do sh run | sec ip nat
ip nat inside
ip nat outside
ip nat inside source list 1 interface Ethernet0/1 overload
R1(config)#
添加访问控制表,实现负载均衡策略,轮循访问两台机器。
R1(config)#access-list 1 permit 192.168.136.133
R1(config)#ip nat pool webserver 192.168.1.2 192.168.1.3 prefix-length 24 type rotary
R1(config)#ip nat inside destination list 1 pool webserver
如果列表中有了其它策略,防止出错,需要删除并添加到新的列表中。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mDmwiwPC-1662436191431)(E:\资料\我整理的笔记\image\access-list.jpg)]
防火墙策略
拓扑图
联网配置
R1(config-line)#int e0/0
R1(config-if)#ip add dhcp
R1(config-if)#no sh
R1(config-if)#int e0/1
R1(config-if)#ip add 192.168.1.1 255.255.255.0
R1(config-if)#no sh
R1(config-if)#ip dhcp pool cisco
R1(dhcp-config)#netw
R1(dhcp-config)#network 192.168.1.0 /24
R1(dhcp-config)#default-router 192.168.1.1
R1(dhcp-config)#dns-server 114.114.114.114 114.114.115.115
R1(dhcp-config)#exit
R1(config)#access-list 99 permit 192.168.1.0 /24
R1(config-if)#int e0/0
R1(config-if)#ip nat outside
R1(config-if)#int e0/1
R1(config-if)#ip nat in
防火墙做策略,控制列表
R1(config)#access-list ?
<1-99> IP standard access list //标准的,涉及三层
<100-199> IP extended access list //扩展的,涉及四层
<1100-1199> Extended 48-bit MAC address access list
<1300-1999> IP standard access list (expanded range)
<200-299> Protocol type-code access list
<2000-2699> IP extended access list (expanded range)
<2700-2799> MPLS access list
<300-399> DECnet access list
<700-799> 48-bit MAC address access list
compiled Enable IP access-list compilation
dynamic-extended Extend the dynamic ACL absolute timer
rate-limit Simple rate-limit specific access list
三层防火墙
R1(config)#access-list 1 deny 192.168.1.3 /32 //拒绝访问,精准匹配,匹配长度32位
R1(config)#access-list 1 permit any //允许访问
R1(config-if)#ip access-group 1 in //将规则加在管道接口,应用
R1#sh access-lists
Standard IP access list 1
10 deny 192.168.1.3 (282 matches)
20 permit any (19350 matches)
Standard IP access list 99
10 permit 192.168.1.0, wildcard bits 0.0.0.255 (576 matches)
四层防火墙
R1(config-if)#no ip access-group 1 in
R1(config-if)#end
R1(config)#access-list 100 deny ?
R1(config)#access-list 100 deny 100 deny tcp 192.168.1.3 0.0.0.0 180.101.49.11 0.0.0.0 eq 80
R1(config)#access-list 100 permit ip any any
R1(config-if)#ip access-group 100 in
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8RUskNp9-1662436191433)(E:\资料\我整理的笔记\image\image-20220122223735531.png)]
端口映射
R1(config)#int e0/1
R1(config-if)#no ip access-group 100 in
R1(config-if)#end
R1(config)#ip nat inside source static tcp 192.168.1.2 80 int e0/0 80
拒绝电脑访问
R1(config)#access-list 2 deny 192.168.136.1
R1(config)#access-list 2 permit any
R1(config-if)#ip access-group 2 in
R1(config-if)#end
拒绝所有访问
R1(config-if)#no ip access-group 2 in
R1(config-if)#end
R1(config)#access-list 3 deny any
R1(config)#int e0/1
R1(config-if)#ip access-group 3 in
R1(config-if)#
动态路由
- 通过在路由器上运行动态路由协议,使得路由器之间能够交互“用于路由计算的信息”,从而路由器动态的“学习”到网络中的路由
距离管理
-
不同的路由协议会有不同的管理距离
-
值越小的管理距离优先级越高
-
当路由器从不同的协议里学习到相同的路由的时候,优先选择优先级高的路由
-
常见的管理距离
静态路由
特点:
需要通过手工的方式进行添加及维护;
适用于组网规模较小的场景,如果网络规模较大,则配置及维护
的成本就会很高;
无法根据拓扑的变化进行动态的响应(各厂商开发了扩展特性,以便弥补静态路由在这点上的不
足);
在大型的网络中,往往采用动、静态路由结合的方式进行部署。
配置方式
R1(config)# ip route network-address subnet-mask {ip-add 1 | exit-interface}
环回接口
Loop_back接口,也叫环回口,是一个逻辑的、虚拟的接口;
使用全局配置命令interface loop_back 加上接口编号可创建一个Loop_back接口,创建完成后即可
为接口配置IP地址;
Loop_back接口在手工创建后,除非人为shutdown,否则不会DOWN掉;
Loop_back接口常用于:
模拟路由器的直连网段,可用于测试;
可用于设备管理(Loop_back接口比较稳定);
供其他协议使用,例如OSPF、BGP、MPLS等;
SNMPTraps消息的源地址;
其他用途(Loopback接口的用途十分广泛)
基础配置命令:
R1(config)#int lo0
R1(config-if)#ip add 1.1.1.1 255.255.255.0
R1(config-if)no sh
R1(config-if)#int e0/0
R1(config-if)#ip add 192.168.12.1 255.255.255.0
R1(config-if)#no sh
R1(config)#router rip
R1(config-router)#version 2
R1(config-router)#network 1.0.0.0
R1(config-router)#network 192.168.12.0
R1# run | sec rip
R2(config)#int lo0
R2(config-if)#ip add 2.2.2.2 255.255.255.0
R2(config-if)no sh
R2(config-if)#int e0/0
R2(config-if)#ip add 192.168.12.2 255.255.255.0
R2(config-if)no sh
R2(config-if)#int e0/1
R2(config-if)#ip add 192.168.23.2 255.255.255.0
R2(config-if)#no sh
R2(config)#router rip
R2(config-router)#version 2
R2(config-router)#network 2.0.0.0
R2(config-router)#network 192.168.12.0
R2(config-router)#network 192.168.23.0
R3(config)#int lo0
R3(config-if)#ip add 3.3.3.3 255.255.255.0
R3(config-if)no sh
R3(config-if)#int e0/0
R3(config-if)#ip add 192.168.23.3 255.255.255.0
R3(config-if)no sh
R3(config)#router rip
R3(config-router)#version 2
R3(config-router)#network 192.168.23.0
R3(config-router)#network 3.0.0.0
实验结果:
实验过程:
RIPV2的路由汇总
默认情况,rip version2 会将自己的路由条目进行汇总然后更新给其他路由器
当路由汇总后出现同样的条目时,会进行等价的负载均衡
如果想要进行人工汇总,建议首先关闭自动汇总,然后再进行手动汇总
R1(config)#router rip
R1(config-router)#version 2
R1(config-router)#no auto-summary # 关闭rip的自动路由汇总
R1(config-router)#int e0/0
R1(config-if)#ip summary-address rip 172.16.0.0 255.255.252.0 # 手动
添加路由汇总
R1(config)#int lo10
R1(config-if)# add 172.16.10.10 255.255.255.0
R1(config-if)#int lo20
R1(config-if)# add 172.16.10.20 255.255.255.0
R1(config-if)#int lo30
R1(config-if)# add 172.16.10.30 255.255.255.0
R1(config)#router rip
R1(config-router)#no auto-summary
R1(config-router)# network 172.16.10.0
R1(config-router)# network 172.16.20.0
R1(config-router)# network 172.16.30.0
R2(config)#router rip
R2(config-router)#version 2
R2(config-router)#no auto-summary
R2(config)int e0/1
R2(config-if)ip summary-address rip 10.10.0.0 255.255.0.0
R3(config)#router rip
R3(config-router)#version 2
R3(config-router)#no auto-summary
OSPF实验拓扑
OSPF简介
OSPF(Open Shortest Path First,开放最短路径优先)是一种链路状态路由协议,无路由循环
(全局拓扑),属于IGP。RFC 2328,“开放”意味着非私有的,对公众开放的。
OSPF的报文封装
OSPF协议包直接封装于IP,协议号89。
OSPF协议使用的组播地址
所有OSPF路由器——224.0.0.5;DR BDR——224.0.0.6
OSPF路由协议的管理距离:110
OSPF实验拓扑
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5KfzBdfp-1662436191437)(E:\资料\我整理的笔记\image\image-20210423021023978.png)]
- 配IP地址
- R2(config)#int range e0/0-1,lo0
R2(config-if-range)#ip ospf 1 area 0
# 配置ip地址,必须配环回接口
略
#配置ospf
方式一
R1(config)#router ospf 1
R1(config-router)#network 1.0.0.0 0.0.0.255 area 0
方式二
R2(config)#int range e0/0-1,lo0
R2(config-if-range)#ip ospf 1 area 0
双向重发步:
R2(config)#router rip
R2(config-router)#ver
R2(config-router)#version 2
R2(config-router)#redistribute ospf 1 metric 5
R2(config-router)#router ospf 1
R2(config-router)#redistribute rip subnets
R1#sh run //可以查看到历史操作
ospf多区域拓扑:
R1(config)#int lo0
R1(config-if)#ip add 1.1.1.1 255.255.255.0
R1(config-if)#int e0/0
R1(config-if)#ip add 192.168.12.1 255.255.255.0
R1(config-if)#no sh
R1(config)#int lo0
R1(config-if)#ip ospf 1 area 1
R1(config-if)#int e0/0
R1(config-if)#ip ospf 1 area 1
R2(config)#int lo0
R2(config-if)#ip add 2.2.2.2 255.255.255.0
R2(config-if)#int e0/0
R2(config-if)#ip add 192.168.12.2 255.255.255.0
R2(config-if)#no sh
R2(config-if)#int e0/1
R2(config-if)#ip add 192.168.23.2 255.255.255.0
R2(config-if)#no sh
R2(config)#int lo0
R2(config-if)#ip ospf 1 area 0
R2(config-if)#int e0/0
R2(config-if)#ip ospf 1 area 1
R2(config-if)#int e0/1
R2(config-if)#ip ospf 1 area 0
R3(config)#int lo0
R3(config-if)#ip add 3.3.3.3 255.255.255.0
R3(config-if)#no sh
R3(config-if)#int e0/0
R3(config-if)#ip add 192.168.23.3 255.255.255.0
R3(config-if)#int e0/1
R3(config-if)#ip add 192.168.34.3 255.255.255.0
R3(config-if)#no sh
R3(config)#int lo0
R3(config-if)#ip ospf 1 area 0
R3(config-if)#int e0/0
R3(config-if)#ip ospf 1 area 0
R3(config-if)#int e0/1
R3(config-if)#ip ospf 1 area 2
R3#sh run
interface Tunnel0
ip address 192.168.100.3 255.255.255.0
ip ospf 1 area 0
tunnel source Ethernet0/1
tunnel mode ipip
tunnel destination 192.168.34.4
R4(config)#int lo0
R4(config-if)#ip add 4.4.4.4 255.255.255.0
R4(config-if)#int e0/0
R4(config-if)#ip add 192.168.34.4 255.255.255.0
R4(config-if)#no sh
R4(config-if)#int e0/1
R4(config-if)#ip add 192.168.45.4 255.255.255.0
R4(config-if)#no sh
R4(config)#int lo0
R4(config-if)#ip ospf 1 area 2
R4(config-if)#int e0/0
R4(config-if)#ip ospf 1 area 2
R4(config-if)#int e0/1
R4(config-if)#ip ospf 1 area 4
R4#sh run
interface Tunnel0
ip address 192.168.100.4 255.255.255.0
ip ospf 1 area 0
tunnel source Ethernet0/0
tunnel mode ipip
tunnel destination 192.168.34.3
R5(config)#int lo0
R5(config-if)#ip add 5.5.5.5 255.255.255.0
R5(config-if)#no sh
R5(config-if)#int e0/0
R5(config-if)#ip add 192.168.45.5 255.255.255.0
R5(config-if)#no sh
R5(config)#int lo0
R5(config-if)#ip ospf 1 area 4
R5(config-if)#int e0/0
R5(config-if)#ip ospf 1 area 4
# 第一种方式,R3和R4上同时都需要配置以下相同的配置,以完成虚链路
R3(config)#router ospf 1
R3(config-router)#area 2 virtual-link 4.4.4.4
# 第二种方式
R3#sh run
interface Tunnel0
ip address 192.168.100.3 255.255.255.0
ip ospf 1 area 0
tunnel source Ethernet0/1
tunnel mode ipip
tunnel destination 192.168.34.4
LAS:
三张表:
R2#show ip ospf nei
Neighbor ID Pri State Dead Time Address Interface
3.3.3.3 1 FULL/DR 00:00:31 192.168.23.3 Ethernet0/1
1.1.1.1 1 FULL/BDR 00:00:39 192.168.12.1 Ethernet0/0
OSPF名词
Neighbor ID:邻居的id,这个id通常是由环回接口选的,并且是优先选取地址大的作为id
在一个OSPF域中,唯一地标识一台OSPF路由器
32bits,表现为IPv4地址形式。在未有手工指定的情况下,如果本地有激活的Loopback接口,则取Loopback接口IP最大值;如果没有LP接口,则取激活的物理接口IP中的最大值
为了提高路由器的RID的稳定性和网络的稳定性建议手动的设置路由器的Router-ID:在OSPF
的进程下修改:router-id <x.x.x.x>(建议id值以编号为准)
项目实施中,一般是建立loopback口,并且手工指定loopback口地址为router-id
Pri:优先级
Dead Time:死亡时间,每十秒钟会更新一次,如果40秒倒计时结束还没有收到邻居的hello包,
认为邻居死亡
State:邻居的状态
DR
DR的作用:多路访问中为了减少邻接关系(N平方的问题)和LSA的洪泛,采用DR机
制,BDR提供了备份
MA网络上的所有路由器均与DR、BDR建立邻居关系
BDR
选举规则
接口优先级数字越大越优先(优先级为0不能参与DR的选举)
Router ID越大越好
稳定压倒一切(非抢占)
通过控制接口优先级是控制DR选举的好办法
DR的选举是基于接口的,如果说某个路由器是DR,这种说法是错误的
在某些以太网中并不需要建立DR、BDR的过程,可以将模式修改为point-to-point来加速邻居
建立的过程
OSPF COST
OSPF接口COST=参考带宽(10的8次方)/ 接口带宽,单位/kb
接口带宽为接口逻辑带宽,可以使用bandwith命令调整,主要用于路由计算,而不是接口物
理带宽,但一般情况:接口逻辑带宽=接口物理带宽。
手工修改接口Cost的方法
Router(config)# int e0/0
Router(config-if)# ip ospf cost 100 !! 该命令在接收路由的入口上配置,也就是数据流量的出
口
可修改“参考带宽”,来保障OSPF在现如今的网络中正常运转
建议将参考带宽设置为整个网络中的最大带宽
auto-cost reference-bandwidth <参考带宽以Mbits为单位>
邻居建立过程
Down
路由器未开始运行OSPF
Init
路由器收到对方发过来的hello,但是却没有发现存在自己的Router-Id
Two-way
R2(config)#int e0/0
R2(config-if)#ip ospf network point-to-point
R1#sh ip ospf neighbor
Neighbor ID Pri State Dead Time Address Interface
2.2.2.2 0 FULL/ - 00:00:32 192.168.12.2 Ethernet0/0
状态中是没有DR或者BDR的
Down
路由器未开始运行OSPF
Init
路由器收到对方发过来的hello,但是却没有发现存在自己的Router-Id
Two-way
路由器收到对方发过来的hello中,是包含有自己的Router-ID
表示双方已经正常通信
如果是广播网络类型,在这个阶段会等待40s种,等所有路由器的Hello都充分交换,然后选
择Router-id最大的作为DR,过时不候。
Ex-Start
路由器发送空的DBD数据报文,用来选举由谁来主导整个过程,最终是选择Router-id较大的
L置位表示是否完成选举过程,M置位表示是否是最后一条消息,MS置位表示的是否是
MASTER
Exchange
路由器交换互相的DBD,可以理解为目录,从的节点率先发送给主节点
比对哪些LSA是自己没有的
Loading
向对方发送请求的LSA,接受对方的LSA,可以理解为书的内容
并且回复ACK
Full
完成j建立过程
OSPF消息类型
hello包
用来建立以及维护邻居关系的数据包
DBD
链路状态数据库描述信息
LSR
链路状态请求,向邻居请求自己没有的信息
LSU
链路状态更新的信息,可以包含一条或者多条
ACK
确认消息,表示收到了
int lo10
ip add 10.10.10.10 255.255.255.0
ospf 1
reddistribute connected subnet//重发步
Dead Time Address Interface
2.2.2.2 0 FULL/ - 00:00:32 192.168.12.2 Ethernet0/0
状态中是没有DR或者BDR的
Down
路由器未开始运行OSPF
Init
路由器收到对方发过来的hello,但是却没有发现存在自己的Router-Id
Two-way
路由器收到对方发过来的hello中,是包含有自己的Router-ID
表示双方已经正常通信
如果是广播网络类型,在这个阶段会等待40s种,等所有路由器的Hello都充分交换,然后选
择Router-id最大的作为DR,过时不候。
Ex-Start
路由器发送空的DBD数据报文,用来选举由谁来主导整个过程,最终是选择Router-id较大的
L置位表示是否完成选举过程,M置位表示是否是最后一条消息,MS置位表示的是否是
MASTER
Exchange
路由器交换互相的DBD,可以理解为目录,从的节点率先发送给主节点
比对哪些LSA是自己没有的
Loading
向对方发送请求的LSA,接受对方的LSA,可以理解为书的内容
并且回复ACK
Full
完成j建立过程
OSPF消息类型
hello包
用来建立以及维护邻居关系的数据包
DBD
链路状态数据库描述信息
LSR
链路状态请求,向邻居请求自己没有的信息
LSU
链路状态更新的信息,可以包含一条或者多条
ACK
确认消息,表示收到了
int lo10
ip add 10.10.10.10 255.255.255.0
ospf 1
reddistribute connected subnet//重发步