美文网首页
静态路由和默认路由

静态路由和默认路由

作者: 小吉头 | 来源:发表于2020-05-21 22:36 被阅读0次

    路由器作用

    用不同的接口连接不同的网络范围,实现不同网段之间的数据转发。转发的依据是路由表。

    做个实验

    R1 f0/0 10.1.12.1/24 f0/1 10.1.13.1/24
    R2 f0/0 10.1.12.2/24
    R3 f0/0 10.1.13.3/24

    示例配置R1的ip,R2、R3省略

    R1#conf t
    Enter configuration commands, one per line.  End with CNTL/Z.
    R1(config)#int f0/0
    R1(config-if)#ip add 10.1.12.1 255.255.255.0 
    R1(config-if)#no sh
    R1(config-if)#exit
    R1(config)#int f0/1
    R1(config-if)#ip add 10.1.13.1 255.255.255.0 
    R1(config-if)#no sh
    R1(config)#end
    //查看ip信息
    R1#show ip int br
    Interface                  IP-Address      OK? Method Status                Protocol
    FastEthernet0/0            10.1.12.1       YES manual up                    up      
    FastEthernet0/1            10.1.13.1       YES manual up                    up 
    //查看路由信息
    R1#show ip route
         10.0.0.0/24 is subnetted, 2 subnets
    C       10.1.13.0 is directly connected, FastEthernet0/1
    C       10.1.12.0 is directly connected, FastEthernet0/0
    

    直连路由

    以上面的R2为例,在没有配置路由协议和静态路由的情况下,没法将数据发给R3。只要路由器接口配置了ip并且up,就能自动获取路由器接口所在的网段(即直连路由),R2 f0/0配置了ip并且设置up,就能自动获取10.1.12.0这条路由。
    配置路由协议或者静态路由的目的就是完善路由器的路由表,使数据被正确转发。

    静态路由

    人为判断的数据走向添加到路由表中。R2本来只有一条直连路由,要把数据发给R3由于不知道路由直接丢弃。下面给R2添加静态路由。

    R2(config)#ip route 10.1.13.0 255.255.255.0 f0/0 10.1.12.1    //f0/0是出接口,10.1.12.1是下一跳ip,一般省略出接口,只写下一跳即可
    R2(config)#do show ip route
    #查看路由信息
         10.0.0.0/24 is subnetted, 2 subnets
    S       10.1.13.0 [1/0] via 10.1.12.1, FastEthernet0/0
    C       10.1.12.0 is directly connected, FastEthernet0/0
    

    再给R3添加静态路由

    R3(config)#ip route 10.1.12.0 255.255.255.0 10.1.13.1
    
    R3#ping 10.1.12.2
    >>Sending 5, 100-byte ICMP Echos to 10.1.12.2, timeout is 2 seconds:
    .!!!!
    Success rate is 80 percent (4/5), round-trip min/avg/max = 16/63/92 ms
    

    默认路由

    以R2为例,下一跳只能发给R1的f0/0,可以设置一个路由默认转发给10.1.12.1

    R2(config)#ip route 0.0.0.0 0.0.0.0 10.1.12.1 //0.0.0.0表示任意网络
    R2#show ip route
    //查看路由信息
         10.0.0.0/24 is subnetted, 2 subnets
    S       10.1.13.0 [1/0] via 10.1.12.1
    C       10.1.12.0 is directly connected, FastEthernet0/0
    S*   0.0.0.0/0 [1/0] via 10.1.12.1
    

    S*表示默认路由,也是一种静态路由。ping 10.1.13.1时,可以匹配到S 10.1.13.0 [1/0] via 10.1.12.1和S* 0.0.0.0/0 [1/0] via 10.1.12.1,路由器发包的时候只能选择一条路由条目,这时根据最长掩码匹配原则,掩码最长说明网络越精确,默认路由掩码是0,所以选择第一条静态路由。

    设置指定路由器为默认

    如下场景,访问公网只能通过R1出去



    设置R1作为默认路由器

    R1(config)#router rip
    R1(config-router)#default-information originate 
    

    设置完后,查看R2和R3的路由表如下:

    R1:
         10.0.0.0/24 is subnetted, 2 subnets
    C       10.1.13.0 is directly connected, FastEthernet0/1
    C       10.1.12.0 is directly connected, FastEthernet0/0
    R2:
         10.0.0.0/24 is subnetted, 2 subnets
    R       10.1.13.0 [120/1] via 10.1.12.1, 00:00:08, FastEthernet0/0
    C       10.1.12.0 is directly connected, FastEthernet0/0
    R*      0.0.0.0/0 [120/1] via 10.1.12.1, 00:00:02, FastEthernet0/0
    R3:
         10.0.0.0/24 is subnetted, 2 subnets
    C       10.1.13.0 is directly connected, FastEthernet0/0
    R       10.1.12.0 [120/1] via 10.1.13.1, 00:00:15, FastEthernet0/0
    R*      0.0.0.0/0 [120/1] via 10.1.13.1, 00:00:10, FastEthernet0/0
    

    R2和R3都多了一条默认路由,下一跳接口都指向了R1路由器的接口。
    默认路由对现有的数据转发没有影响,因为是匹配掩码最长原则,默认路由掩码是0。当没有路由匹配时会走默认路由。

    相关文章

      网友评论

          本文标题:静态路由和默认路由

          本文链接:https://www.haomeiwen.com/subject/ijlxahtx.html