285 lines
8.4 KiB
Markdown
285 lines
8.4 KiB
Markdown
|
# 路由
|
|||
|
|
|||
|
### 一、路由概述
|
|||
|
|
|||
|
1. **什么是路由**
|
|||
|
|
|||
|
> 数据包从一个网络到另外一个网络转发数据包的过程
|
|||
|
|
|||
|
2. **支持路由功能的设备**
|
|||
|
|
|||
|
> 路由器、三层交换机、防火墙
|
|||
|
|
|||
|
3. **路由器转发数据包的依据**
|
|||
|
|
|||
|
- 每一台路由器都维护着一张路由表
|
|||
|
- 路由器是依靠这张路由表来转发数据的
|
|||
|
- 这张路由表就类似于我们生活中的地图
|
|||
|
|
|||
|
4. **查看路由表**
|
|||
|
|
|||
|
```
|
|||
|
<Huawei>display ip routing-table //查看路由表
|
|||
|
目的地址 /掩码 协议 优先级 开销值 下一跳 出接口
|
|||
|
Destination/Mask Proto Pre Cost NextHop Interface
|
|||
|
```
|
|||
|
|
|||
|
5. **路由分类**
|
|||
|
|
|||
|
- 直连路由:路由器接口所直连的网段形成的路由信息 ,配置路由器接口IP并为UP状态自动生成
|
|||
|
|
|||
|
- 非直连路由:
|
|||
|
静态路由:由管理员手动为路由器指定的路由信息
|
|||
|
动态路由:路由器通过路由协议学习到的路由信息
|
|||
|
|
|||
|
- 静态路由配置格式:
|
|||
|
|
|||
|
```
|
|||
|
ip route-static 目标网络号 子网掩码 下一跳
|
|||
|
```
|
|||
|
|
|||
|
### 二、静态路由实验
|
|||
|
|
|||
|
1. **拓扑**
|
|||
|
|
|||
|
![image-20240826163459889](https://picgo-noriu.oss-cn-beijing.aliyuncs.com/Images/image-20240826163459889.png)
|
|||
|
|
|||
|
2. **需求**
|
|||
|
|
|||
|
> 实现PC1可以和PC2 互联互通
|
|||
|
|
|||
|
3. **分析**
|
|||
|
|
|||
|
> PC1在192.168.1.0/24网段
|
|||
|
> PC2在192.168.4.0/24网段
|
|||
|
> 因为是跨网段通信,也叫非直连网段通信,所以我们使用静态路由
|
|||
|
> PC把目的地和自己不再同一网段的数据包,统统的交给网关,让网关帮忙转发数据
|
|||
|
|
|||
|
4. **配置步骤**
|
|||
|
|
|||
|
- 给PC配置IP地址
|
|||
|
- 给路由器R1和R2配置接口IP地址
|
|||
|
- 在路由器R1中配置去往192.168.4.0/24网段的路由
|
|||
|
- 在路由器R2中配置回程路由,去往192.168.1.0/24网段的路由
|
|||
|
|
|||
|
5. **配置命令**
|
|||
|
|
|||
|
- PC1的IP地址配置:192.168.1.1,网关地址192.168.1.254
|
|||
|
PC2的IP地址配置:192.168.2.1,网关地址192.168.2.254
|
|||
|
|
|||
|
- 第一步:配置R1路由器(接口IP地址和静态路由 )
|
|||
|
|
|||
|
- R1的配置:
|
|||
|
|
|||
|
```
|
|||
|
<R1>u t m
|
|||
|
<R1>sys
|
|||
|
[R1]int g0/0/0 //进入接口g0/0/0
|
|||
|
[R1-GigabitEthernet0/0/0]ip address 192.168.2.1 24 //配置接口IP地址
|
|||
|
[R1-GigabitEthernet0/0/0]int g0/0/1 //进入接口g0/0/1
|
|||
|
[R1-GigabitEthernet0/0/1]ip address 192.168.1.254 24 //配置接口IP地址
|
|||
|
[R1-GigabitEthernet0/0/1]quit //退回到系统视图
|
|||
|
[R1]ip route-static 192.168.4.0 24 192.168.2.2 //配置静态路由,目的地:192.168.4.0/24 网段,下一跳地址:192.168.2.2
|
|||
|
```
|
|||
|
|
|||
|
- 第二步:配置R2路由器(接口IP地址和静态路由)
|
|||
|
|
|||
|
- R2的配置:
|
|||
|
|
|||
|
```
|
|||
|
<R2>un t m
|
|||
|
<R2>sys
|
|||
|
[R2]int g0/0/0
|
|||
|
[R2-GigabitEthernet0/0/0]ip address 192.168.4.254 24
|
|||
|
[R2-GigabitEthernet0/0/0]int g0/0/1
|
|||
|
[R2-GigabitEthernet0/0/1]ip address 192.168.2.2 24
|
|||
|
[R2-GigabitEthernet0/0/1]quit
|
|||
|
[R2]ip route-static 192.168.1.0 24 192.168.2.1
|
|||
|
```
|
|||
|
|
|||
|
- 第三步:验证测试
|
|||
|
|
|||
|
```
|
|||
|
<R1>display ip routing-table 192.168.4.1 //验证R1有没有去往4.1的路由
|
|||
|
Destination/Mask Proto Pre Cost NextHop Interface
|
|||
|
192.168.4.0/24 Static 60 0 192.168.2.2 GigabitEthernet0/0/0
|
|||
|
<R2>display ip routing-table 192.168.1.1 //验证R2有没有去往1.1的回程路由
|
|||
|
Destination/Mask Proto Pre Cost NextHop Interface
|
|||
|
192.168.1.0/24 Static 60 0 192.168.2.1 GigabitEthernet0/0/1
|
|||
|
```
|
|||
|
|
|||
|
- 重要字段解析
|
|||
|
|
|||
|
- Destination:表示这条路由的目的地址
|
|||
|
- Mask:表示目的地址的子网掩码长度
|
|||
|
- Proto:表示这条路由的路由协议
|
|||
|
- Pre:表示这条路由的路由协议优先级,优先级数值越小越优先
|
|||
|
- NextHop:表示这条路由的下一跳地址, 下一跳地址就是指数据转发的下一个设备
|
|||
|
- Interface:表示这条路由的出接口,指明数据将从本地路由器哪个接口转发出去
|
|||
|
|
|||
|
> 备注: 不同的路由协议优先级不一致,比如:
|
|||
|
> Direct:直连路由优先级为 0
|
|||
|
> Static:静态路由优先级默认为 60
|
|||
|
> OSPF: OSPF优先级默认为 10
|
|||
|
|
|||
|
### 三、路由的配置
|
|||
|
|
|||
|
1. **路由器转发数据包IP地址与MAC地址的封装变化**
|
|||
|
|
|||
|
> 不同网络的主机通信,IP地址始终保持不变,MAC地址一直在变。
|
|||
|
> 数据包在网络中传输时,源IP地址和目的IP地址一直不变,但是源MAC地址和目的MAC地址每经过一个网段,就会发生一次变化
|
|||
|
|
|||
|
2. **多路由器-配置静态路由实现全网互通**
|
|||
|
|
|||
|
1. 拓扑
|
|||
|
|
|||
|
![image-20240826164202232](https://picgo-noriu.oss-cn-beijing.aliyuncs.com/Images/image-20240826164202232.png)
|
|||
|
|
|||
|
2. 需求
|
|||
|
|
|||
|
> 全网互通
|
|||
|
|
|||
|
3. 配置
|
|||
|
|
|||
|
> 配置路由器的接口IP地址,掩码
|
|||
|
>
|
|||
|
> 配置PC的IP地址,掩码、网关
|
|||
|
>
|
|||
|
> 配置静态路由(非直连网段通信)
|
|||
|
|
|||
|
- 步骤1:配置R1和R2和R3的接口IP地址
|
|||
|
|
|||
|
```
|
|||
|
[R1]interface GigabitEthernet 0/0/0
|
|||
|
[R1-GigabitEthernet0/0/0]IP address 192.168.1.254 24
|
|||
|
[R1-GigabitEthernet0/0/0]QUIT
|
|||
|
[R1]interface GigabitEthernet 0/0/1
|
|||
|
[R1-GigabitEthernet0/0/1]IP address 192.168.2.1 24
|
|||
|
```
|
|||
|
|
|||
|
```
|
|||
|
[R2]interface GigabitEthernet 0/0/1
|
|||
|
[R2-GigabitEthernet0/0/1]IP address 192.168.2.2 24
|
|||
|
[R2-GigabitEthernet0/0/1]QUIT
|
|||
|
[R2]interface GigabitEthernet 0/0/0
|
|||
|
[R2-GigabitEthernet0/0/0]ip address 192.168.3.1 24
|
|||
|
```
|
|||
|
|
|||
|
```
|
|||
|
[R3]interface GigabitEthernet 0/0/1
|
|||
|
[R3-GigabitEthernet0/0/1]ip address 192.168.3.2 24
|
|||
|
[R3-GigabitEthernet0/0/1]quit
|
|||
|
[R3]interface GigabitEthernet 0/0/0
|
|||
|
[R3-GigabitEthernet0/0/0]ip address 192.168.4.254 24
|
|||
|
```
|
|||
|
|
|||
|
- 步骤2:配置PC1和PC2的IP地址及网关
|
|||
|
|
|||
|
- 步骤3:配置静态路由
|
|||
|
|
|||
|
```
|
|||
|
[R1]ip route-static 192.168.3.0 24 192.168.2.2
|
|||
|
[R1]ip route-static 192.168.4.0 24 192.168.2.2
|
|||
|
```
|
|||
|
|
|||
|
```
|
|||
|
[R3]ip route-static 192.168.2.0 24 192.168.3.1
|
|||
|
[R3]ip route-static 192.168.1.0 24 192.168.3.1
|
|||
|
```
|
|||
|
|
|||
|
```
|
|||
|
[R2]ip route-static 192.168.4.0 24 192.168.3.2
|
|||
|
[R2]ip route-static 192.168.1.0 24 192.168.2.1
|
|||
|
```
|
|||
|
|
|||
|
- 验证与测试
|
|||
|
|
|||
|
```
|
|||
|
PC1 ping 192.168.4.1
|
|||
|
192.168.3.2
|
|||
|
192.168.2.2
|
|||
|
```
|
|||
|
|
|||
|
3. **默认路由实验**
|
|||
|
|
|||
|
- 默认路由配置格式:
|
|||
|
|
|||
|
> ip route-static 0.0.0.0 0 下一跳
|
|||
|
> 0.0.0.0表示任意网络ID
|
|||
|
> 0表示任意子网掩码
|
|||
|
|
|||
|
- 拓扑
|
|||
|
|
|||
|
![image-20240826164630278](https://picgo-noriu.oss-cn-beijing.aliyuncs.com/Images/image-20240826164630278.png)
|
|||
|
|
|||
|
> 接前一个实验环境
|
|||
|
|
|||
|
- 删除R1与R3的明细静态路由
|
|||
|
|
|||
|
```
|
|||
|
[R1]undo ip route-static 192.168.3.0 24 192.168.2.2
|
|||
|
[R1]undo ip route-static 192.168.4.0 24 192.168.2.2
|
|||
|
```
|
|||
|
|
|||
|
```
|
|||
|
[R3]undo ip route-static 192.168.2.0 24 192.168.3.1
|
|||
|
[R3]undo ip route-static 192.168.1.0 24 192.168.3.1
|
|||
|
```
|
|||
|
|
|||
|
- R1与R3配置置认路由
|
|||
|
|
|||
|
```
|
|||
|
[R1]ip route-static 0.0.0.0 0 192.168.2.2
|
|||
|
[R3]ip route-static 0.0.0.0 0 192.168.3.1
|
|||
|
```
|
|||
|
|
|||
|
- 验证与测试
|
|||
|
|
|||
|
```
|
|||
|
PC1 ping 192.168.4.1
|
|||
|
192.168.3.2
|
|||
|
```
|
|||
|
|
|||
|
4. **默认路由的隐患**
|
|||
|
|
|||
|
- 拓扑
|
|||
|
|
|||
|
![image-20240826164856306](https://picgo-noriu.oss-cn-beijing.aliyuncs.com/Images/image-20240826164856306.png)
|
|||
|
|
|||
|
- 需求
|
|||
|
|
|||
|
> 全网互联互通
|
|||
|
|
|||
|
- 配置命令
|
|||
|
|
|||
|
- R4的配置
|
|||
|
|
|||
|
```
|
|||
|
[R4]int g0/0/0
|
|||
|
[R4-GigabitEthernet0/0/0]ip address 192.168.1.254 24
|
|||
|
[R4-GigabitEthernet0/0/0]int g0/0/1
|
|||
|
[R4-GigabitEthernet0/0/1]ip address 192.168.2.1 24
|
|||
|
[R4]ip route-static 0.0.0.0 0 192.168.2.2 //配置默认路由
|
|||
|
```
|
|||
|
|
|||
|
- R5的配置
|
|||
|
|
|||
|
```
|
|||
|
[R5]int g0/0/1
|
|||
|
[R5-GigabitEthernet0/0/1]ip address 192.168.2.2 24
|
|||
|
[R5-GigabitEthernet0/0/1]int g0/0/0
|
|||
|
[R5-GigabitEthernet0/0/0]ip address 192.168.3.254 24
|
|||
|
[R5]ip route-static 0.0.0.0 0 192.168.2.1 //配置默认路由
|
|||
|
```
|
|||
|
|
|||
|
- 验证测试
|
|||
|
|
|||
|
```
|
|||
|
PC1 ping PC2 发现可以互联互通
|
|||
|
在做别的验证:PC1 ping 192.168.5.1(这个网段在网络中不存在)
|
|||
|
这个时候就会出现三层路由环路
|
|||
|
所以,两台直连的设备不能相对的配置默认路由,一边是默认路由,那么另外一边要配置明细路由
|
|||
|
```
|
|||
|
|
|||
|
|