(目录)

静态路由原理与配置

路由器的工作原理

路由概述

  • 路由

    从源主机到目标主机的转发过程

  • 路由器

    能够将数据包转发到正确的目的地,并在转发过程中选择最佳路径的设备

路由器的工作原理

  • 根据路由表(网段-接口)转发数据

路由表的形成

  • 路由表

    • 路由器中维护的路由条目的集合
    • 路由器根据路由表做路径选择
  • 路由表的形成

    • 直连网段

      配置IP地址,端口UP状态,形成直连路由

    • 非直连网段(路由表)

      ==静态添加==,动态学习

静态路由和默认路由

静态路由

  • 由管理员手工配置的,是单向的(配置下一跳)
  • 缺乏灵活性,数量增多时增加配置难度

默认路由

  • 当路由器在路由表中找不到目标网络的路由条目时,路由器把请求转发到默认路由接口(直接发给下一跳)
  • 多配置于末节路由(Stub Network-末节网络)
  • 相邻两个路由器之间不可以配置默认路由(容易踢皮球)

路由器转发数据包的封装过程

  • 源目地址变化

RBB~I~BVQS`D0TCH4UBBM0.png

Host A:先ARP广播解析出网关E0的MAC地址,进行了第一次封装

​ 源IP地址192.168.10.20 目的IP192.168.20.20

​ 源MAC 11-11 目的MAC 22-22

路由器A:解封装,查看数据包的目的IP,查询路由表,确定转发路径,由E1给路由器B

​ 先ARP广播解析出路由器B E1的MAC地址,进行第二次封装

​ 源IP地址192.168.10.20 目的IP192.168.20.20

​ 源MAC 33-33 目的MAC 44-44

路由器B:解封装,查看数据包的目的IP位自己的直连网段,通过E0发给Host B

​ 先ARP广播解析出Host B 的MAC地址,进行第三次封装

​ 源IP地址192.168.10.20 目的IP192.168.20.20

​ 源MAC 55-55 目的MAC 66-66

Host B:数据包送到主机B,一路解封送到应用层

  1. Host A在网络层将来自上层的报文数据封装成IP数据包,其首部包含了源地址和目的地址。源地址即本机IP地址192.168.10.20,目的地址位Host B的IP地址192.168.20.20。Host A会用本机配置的24位掩码与目的地址进行与运算,得出目的地址与本机地址不在同一网段,因此发往Host B的数据包需要经过网关路由器A转发。
  2. Host A通过ARP请求获得默认网关路由器A的E0接口MAC地址00-12-23-32-22-22。在数据链路层Host A将IP数据包封装成以太网数据帧,在以太网帧首部的源MAC地址为00-12-23-32-11-11,目的MAC地址为网关E0接口的MAC地址00-12-23-32-22-22。
  3. 路由器A从E0接口接收到数据帧,把数据链路层的封装去掉。路由器A认为这个IP数据包是要通过自己进行路由转发,所以路由器A会查找自己的路由表,寻找与目标192.168.20.20相匹配的路由选项,然后根据路由表的下一跳地址将数据包转发到E1接口。
  4. 在E1接口路由器A重新封装以太网帧,此时源MAC地址为路由器A的E1接口MAC地址00-12-23-32-33-33,目的MAC地址为与之相连的路由器B的E1接口MAC地址00-12-23-32-44-44。
  5. 路由器B从E1接口接收到数据帧,同样会把数据链路层的封装去掉,对目的IP进行检查,并与路由表进行匹配。然后根据路由表的下一跳信息将数据包转发到E0接口。路由器B发现目的网段于自己的E0接口直接相连,通过ARP广播,路由器B获得Host B以太口的MAC地址00-12-23-32-66-66。路由器B再将IP数据包装成以太网帧,源MAC地址为路由器B的E0接口的MAC地址00-12-23-32-55-55,目的MAC地址为Host B的MAC地址00-12-23-32-66-66。封装完毕,将以太网帧从E0接口发往Host B。
  6. 源目IP地址始终不变,源目MAC地址始终在变
  • 路由交换对比
    • 路由工作在网络层
      • 根据"路由表"转发数据
      • 路由选择
      • 路由转发
    • 交换工作在数据链路层
      • 根据"MAC地址表"转发数据
      • 硬件转发

静态路由和默认路由的配置

静态路由的配值

  • Cisco:Router(config)# ip route network mask {address | interface}
  • Huawei:[Huawei] ip route-static 192.168.10.0(目的网段) 24(子网掩码) 192.168.12.1(下一跳)

默认路由的配置

  • Cisco:Router(config)# ip route 0.0.0.0.0.0.0.0(代表人和网络) address
  • Huawei:[Huawei] ip route-static 0.0.0.0(目的网段) 0.0.0.0(子网掩码) 192.168.12.1(下一跳)

查看路由表

  • [Huawei]display ip routing-table
  • [R1]:ip route-static 192.168.10.0 24 11.0.0.2 //<主>优先级默认值60(越小越优先)
  • [R1]:ip route-static 192.168.10.0 24 12.0.0.2 preference 70 //配置浮动路由用于备份

实例演示(静态路由)

拓扑图.png

<Huawei>system-view                         ##进入系统视图
[Huawei]sysname R1                          ##修改名称
[R1]
[R1]undo info-center enable                 ##关闭提示信息,防止输入失误
Info: Information center is disabled.
[R1]user-interface console 0
[R1-ui-console0]idle-timeout 0 0            ##设置永不超时退出,至此三台路由器操作相同,略去两段
  • 路由器R1
[R1-ui-console0]interface g0/0/1                      ##进入GigabitEthernet0/0/1端口进行静态ip配置
[R1-GigabitEthernet0/0/1]ip address 192.168.10.1 24
[R1-GigabitEthernet0/0/1]display this                 ##检查配置是否成功
[V200R003C00]
#
interface GigabitEthernet0/0/1
 ip address 192.168.10.1 255.255.255.0 
#
return
[R1-GigabitEthernet0/0/1]interface g0/0/0             ##配置GigabitEthernet0/0/0端口
[R1-GigabitEthernet0/0/0]ip add 12.0.0.1 255.255.255.0
[R1-GigabitEthernet0/0/0]dis th                       ##检查
[V200R003C00]
#
interface GigabitEthernet0/0/0
 ip address 12.0.0.1 255.255.255.0 
#
return
[R1-GigabitEthernet0/0/0]undo shutdown                 ##Cisco此处为no shutdown,实体机必须进行的操作
Info: Interface GigabitEthernet0/0/0 is not shutdown.  ##以下省略
[R1-GigabitEthernet0/0/1]quit
[R1]display current	
[R1]display current-configuration                      ##检查配置(以下信息过多省略,展示部分)
~~~~~~~~~~~                                            ##也可使用display ip routing-table命令,dispaly 
                                                         ip interface brief
interface GigabitEthernet0/0/0
 ip address 12.0.0.1 255.255.255.0                     ##配置完成
#
interface GigabitEthernet0/0/1
 ip address 192.168.10.1 255.255.255.0                 ##配置完成
#
interface GigabitEthernet0/0/2
#
interface NULL0
#
user-interface con 0
  ---- More ----
[R1]ip route-Static 192.168.20.0 24 12.0.0.2           ##配置静态路由(检查过程略)
[R1]q
<R1>save
  The current configuration will be written to the device. 
  Are you sure to continue? (y/n)[n]:y                 ##键入y
  It will take several minutes to save configuration file, please wait.......
  Configuration file had been saved successfully
  Note: The configuration file will take effect after being activated
<R1>                                                   ##记得退出到用户视图保存!!!下同
  • 路由器R2(不多赘述)
[R2-ui-console0]int g0/0/0
[R2-GigabitEthernet0/0/0]ip add 12.0.0.2 24
[R2-GigabitEthernet0/0/0]un sh
Info: Interface GigabitEthernet0/0/0 is not shutdown.
[R2-GigabitEthernet0/0/0]int g0/0/1
[R2-GigabitEthernet0/0/1]ip add 23.0.0.2 24
[R2-GigabitEthernet0/0/1]un sh
Info: Interface GigabitEthernet0/0/1 is not shutdown.
[R2-GigabitEthernet0/0/1]q
[R2]ip route-static 192.168.20.0 24 23.0.0.1
[R2]ip route-static 192.168.10.0 24 12.0.0.1          ##返回的静态路由
  • 路由器R3
[R3-ui-console0]int g0/0/1
[R3-GigabitEthernet0/0/1]ip add 192.168.20.1 24
[R3-GigabitEthernet0/0/1]un sh
Info: Interface GigabitEthernet0/0/1 is not shutdown.
[R3-GigabitEthernet0/0/1]int g0/0/0
[R3-GigabitEthernet0/0/0]ip add 23.0.0.1 24
[R3-GigabitEthernet0/0/0]un sh
Info: Interface GigabitEthernet0/0/0 is not shutdown.
[R3-GigabitEthernet0/0/0]
[R3-GigabitEthernet0/0/0]q
[R3]ip route-static 192.168.10.0 24 23.0.0.2          ##配置返回静态路由
  • PC1和PC2配置记得点==应用== PC1.png PC2.png

PC1第一次ping.png PC2pingPC1.png

PC1第一次pingPC2时会有 time out是由于PC1在学习到ARP缓存表时会先进行三次ARP探测。

不能ping通非直连网段里没有配置的ip。

实例演示(默认路由,接上例)

  • 路由器R1
[R1]undo ip route-static 192.168.20.0 255.255.255.0 12.0.0.2   ##删除已配置的静态路由(检查过程略)
[R1]ip route-static 0.0.0.0 0.0.0.0 12.0.0.2                   ##配置默认路由,下一跳12.0.0.2
  • 路由器R3
[R3]undo ip route-static 192.168.10.0 255.255.255.0 23.0.0.2   ##同上
[R3]ip route-static 0.0.0.0 0.0.0.0 23.0.0.2                   

默认路由PC1pingPC2.png

默认路由PC2pingPC1.png 配置成功!

路由表.png

  • 使用display ip routing-table显示路由表可以看到默认路由默认优先级为60

实例演示(浮动静态路由)

浮动静态路由器拓扑1.png

  • 路由器R1配置(R1作备用路由器)

    <Huawei>sys
    Enter system view, return user view with Ctrl+Z.
    [Huawei]sys R1
    [R1]int g0/0/0
    [R1-GigabitEthernet0/0/0]ip add 12.0.0.2 24
    [R1-GigabitEthernet0/0/0]q
    [R1]int g0/0/1
    [R1-GigabitEthernet0/0/1]ip add 23.0.0.2 24
    [R1-GigabitEthernet0/0/1]q
    [R1]ip route-static 192.168.20.0 24 23.0.0.1      ##静态路由
    [R1]ip route-static 192.168.10.0 24 12.0.0.1      ##静态路由
    [R1]q
    <R1>save
      The current configuration will be written to the device. 
      Are you sure to continue? (y/n)[n]:y
      It will take several minutes to save configuration file, please wait.......
      Configuration file had been saved successfully
      Note: The configuration file will take effect after being activated
    
  • 路由器R2配置

    <Huawei>sys
    Enter system view, return user view with Ctrl+Z.
    [Huawei]sys R2
    [R2]int g0/0/0
    [R2-GigabitEthernet0/0/0]ip add 192.168.10.1 24
    [R2-GigabitEthernet0/0/0]q
    [R2]int g0/0/1
    [R2-GigabitEthernet0/0/1]ip add 13.0.0.1 24
    [R2-GigabitEthernet0/0/1]q
    [R2]int g0/0/2
    [R2-GigabitEthernet0/0/2]ip add 12.0.0.1 24
    [R2-GigabitEthernet0/0/2]q
    [R2]ip route-static 192.168.20.0 24 13.0.0.2               ##静态路由配置
    [R2]ip route-static 192.168.20.0 24 12.0.0.2 preference 70 ##浮动静态路由配置
    [R2]q
    <R2>save
      The current configuration will be written to the device. 
      Are you sure to continue? (y/n)[n]:y
      It will take several minutes to save configuration file, please wait.......
      Configuration file had been saved successfully
      Note: The configuration file will take effect after being activated
    
  • 路由器R3配置

    <Huawei>sys
    Enter system view, return user view with Ctrl+Z.
    [Huawei]sys R3
    [R3]int g0/0/2
    [R3-GigabitEthernet0/0/2]ip add 23.0.0.1 24
    [R3-GigabitEthernet0/0/2]q
    [R3]int g0/0/1
    [R3-GigabitEthernet0/0/1]ip add 13.0.0.2 24
    [R3-GigabitEthernet0/0/1]q
    [R3]int g0/0/0
    [R3-GigabitEthernet0/0/0]ip add 192.168.20.1 24
    [R3-GigabitEthernet0/0/0]q
    [R3]ip route-static 192.168.10.0 24 13.0.0.1               ##静态路由配置
    [R3]ip route-static 192.168.10.0 24 23.0.0.2 preference 70 ##浮动静态路由配置
    [R3]q
    <R3>save
      The current configuration will be written to the device. 
      Are you sure to continue? (y/n)[n]:y
      It will take several minutes to save configuration file, please wait.......
      Configuration file had been saved successfully
      Note: The configuration file will take effect after being activated
    
  • 测试PC1 - R2 - R3 - PC2 路线连通性

    浮动一直pingPC2.png

    对路由器R3的G0/0/1口进行抓包

    抓包.png

    证明该路线成功连通

  • 测试备用线路PC1 - R2 - R1 -R3 - PC2 路线连通性

    路由器R2

    <R2>sys
    Enter system view, return user view with Ctrl+Z.
    [R2]int g0/0/1
    [R2-GigabitEthernet0/0/1]shutdown  ##模拟原路线被切断
    

    浮动静态路由拓扑.png

    GWD7T9GOO2NCH1_5NJ3~LB.png

    该备用线路启用成功,浮动静态路由配置成功