首页 > 其他 > 详细

004 路由

时间:2021-04-07 23:25:57      阅读:31      评论:0      收藏:0      [点我收藏+]

路由介绍

linux主机当路由器的三个条件

  1. 必须开启路由转发功能

    echo 1 > /proc/sys/net/ipv4/ip_forward
    #想要永久生效应该写入配置文件中去
    echo net.ipv4.ip_forward>/etc/sysctl.d/ip_forward.conf
    
    #检查
    cat /proc/sys/net/ipv4/ip_forward
    
  2. 对方必须把自己当成网关

  3. 该linux主机必须有对应的路由转发条目

路由的三种分类

  • 1、主机路由:子网掩码为32位,直接把范围缩小到最小,就一个地址

    route add -host 172.16.13.11/32 dev eth0
    
  • 2、网络路由:子网掩码不足32位,所包含的地址是一个范围,子网掩码越大容纳的范围越小

    route add -net 172.16.11.0/24 dev eth0
    
  • 3、默认路由:目标地址为0.0.0.0/0,所包含的范围是最大的

    route add default dev eth0
    
  • 4、指定网关,让网关做后续转发,不指定就在局域网内发送

    route add -host 172.16.13.11/32 gw 192.168.15.2 dev eth0
    route add -net 172.16.11.0/24 gw 192.168.15.2 dev eth0
    route add default gw 192.168.15.2 dev eth0
    

    路由的优先级:

    越精确优先级越高,子网掩码越大越精确
    
    主机路由>网络路由>默认路由
    

实验

实验目标

技术分享图片

根据图片拓扑结构,依次在虚拟机1中ping通

2.2.2.2
2.2.2.3
3.3.3.3
3.3.3.4
4.4.4.3

环境准备

#创建虚拟交换机
打开虚拟网络编辑器,添加4个仅主机模式

技术分享图片

#创建4个虚拟机,虚拟机1添加一个仅主机网卡,其余两个

技术分享图片

#配置好网络
#个人编写的小脚本,更改起来快一点

#!bin/bash
#baimo
#配置虚拟机网卡与关闭服务
cat >/etc/sysconfig/network-scripts/ifcfg-eth0<<EOF
TYPE="Ethernet"
BOOTPROTO="static"
IPADDR=$1
NETMASK=255.255.255.0
 
NAME="eth0"
DEVICE="eth0"
ONBOOT="yes"
NM_CONTROLLED="no"
EOF

cat >/etc/sysconfig/network-scripts/ifcfg-eth1<<EOF
TYPE="Ethernet"
BOOTPROTO="static"
IPADDR=$2
NETMASK=255.255.255.0
 
NAME="eth1"
DEVICE="eth1"
ONBOOT="yes"
NM_CONTROLLED="no"
EOF

systemctl restart network

setenforce=0

iptables -F

ping 2.2.2.2

#查看路由条目
[root@test1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
1.1.1.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

#添加2.2.2.0/24条目并查看
[root@test1 ~]# route add -net 2.2.2.0/24 dev eth0
[root@test1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
1.1.1.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

#测试
[root@test1 ~]# ping 2.2.2.2
PING 2.2.2.2 (2.2.2.2) 56(84) bytes of data.
64 bytes from 2.2.2.2: icmp_seq=1 ttl=64 time=0.885 ms
64 bytes from 2.2.2.2: icmp_seq=2 ttl=64 time=0.726 ms
64 bytes from 2.2.2.2: icmp_seq=3 ttl=64 time=0.610 ms
64 bytes from 2.2.2.2: icmp_seq=4 ttl=64 time=0.735 ms

ping 2.2.2.3

#查看虚拟机1与虚拟机2
route -n

#2.2.2.3需要将1.1.1.2当做网关再次转发,这是过去的路
#虚拟机1
[root@test1 ~]# route add -net 2.2.2.0/24 gw 1.1.1.2 eth0
[root@test1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
1.1.1.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
2.2.2.0         1.1.1.2         255.255.255.0   UG    0      0        0 eth0
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

#开启路由功能并检测
#虚拟机2
[root@test1 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
[root@test1 ~]# cat /proc/sys/net/ipv4/ip_forward
1

#ping是一个发包和收包的相互过程,接下来是回去的路,也要将2.2.2.2当作网关转发
#虚拟机3
[root@test1 ~]# route add -net 1.1.1.0/24 gw 2.2.2.2 eth0
[root@test1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
1.1.1.0         2.2.2.2         255.255.255.0   UG    0      0        0 eth0
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
3.3.3.0         0.0.0.0         255.255.255.0   U     0      0        0 eth1

#测试
[root@test1 ~]# ping 2.2.2.3
PING 2.2.2.3 (2.2.2.3) 56(84) bytes of data.
64 bytes from 2.2.2.3: icmp_seq=1 ttl=63 time=2.81 ms
64 bytes from 2.2.2.3: icmp_seq=2 ttl=63 time=1.14 ms
64 bytes from 2.2.2.3: icmp_seq=3 ttl=63 time=1.13 ms
64 bytes from 2.2.2.3: icmp_seq=4 ttl=63 time=1.15 ms

ping 3.3.3.3

#各虚拟机查看
route -n

#虚拟机1增加3.3.3.0条目并指定网关发送
[root@test1 ~]# route add -net 3.3.3.0/24 gw 1.1.1.2 eth0
[root@test1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
1.1.1.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
2.2.2.0         1.1.1.2         255.255.255.0   UG    0      0        0 eth0
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
3.3.3.0         1.1.1.2         255.255.255.0   UG    0      0        0 eth0

#虚拟机2增加3.3.3.0条目发送
[root@test1 ~]# route add -net 3.3.3.0/24 dev eth1
[root@test1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
1.1.1.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth1
3.3.3.0         0.0.0.0         255.255.255.0   U     0      0        0 eth1

#虚拟机3增加一条回给1.1.1.0/24的条目,由eth0回去
#因为ping2.2.2.3的时候已经做过了,这里免

#测试
[root@test1 ~]# ping 3.3.3.3
PING 3.3.3.3 (3.3.3.3) 56(84) bytes of data.
64 bytes from 3.3.3.3: icmp_seq=1 ttl=63 time=1.91 ms
64 bytes from 3.3.3.3: icmp_seq=2 ttl=63 time=1.99 ms
64 bytes from 3.3.3.3: icmp_seq=3 ttl=63 time=1.45 ms
64 bytes from 3.3.3.3: icmp_seq=4 ttl=63 time=1.16 ms

ping 3.3.3.4

#查看各机的路由条目
route -n

#虚拟机1增加给3.3.3.0的条目
#由于ping3.3.3.3的时候增加了,这里免

#虚拟机2增加3.3.3.0/24条目,并指定网关2.2.2.3去转发
[root@test1 ~]# route add -net 3.3.3.0/24 gw 2.2.2.3 eth1
[root@test1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
1.1.1.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth1
3.3.3.0         2.2.2.3         255.255.255.0   UG    0      0        0 eth1
3.3.3.0         0.0.0.0         255.255.255.0   U     0      0        0 eth1

#开启虚拟机3的路由功能并检查
[root@test1 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
[root@test1 ~]# cat /proc/sys/net/ipv4/ip_forward
1

#测试
[root@test1 ~]# ping 3.3.3.4
PING 3.3.3.4 (3.3.3.4) 56(84) bytes of data.
64 bytes from 3.3.3.4: icmp_seq=1 ttl=64 time=1.00 ms
64 bytes from 3.3.3.4: icmp_seq=2 ttl=64 time=0.415 ms
64 bytes from 3.3.3.4: icmp_seq=3 ttl=64 time=0.510 ms
64 bytes from 3.3.3.4: icmp_seq=4 ttl=64 time=0.593 ms

ping 4.4.4.3

#查看各虚拟机
route -n

#虚拟机1增加4.4.4.0/24并指定1.1.1.2网关转发
[root@test1 ~]# route add -net 4.4.4.0/24 gw 1.1.1.2 eth0
[root@test1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
1.1.1.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
2.2.2.0         1.1.1.2         255.255.255.0   UG    0      0        0 eth0
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
3.3.3.0         1.1.1.2         255.255.255.0   UG    0      0        0 eth0
4.4.4.0         1.1.1.2         255.255.255.0   UG    0      0        0 eth0

#虚拟机2增加4.4.4.0/24并指定2.2.2.3网关转发
[root@test1 ~]# route add -net 4.4.4.0/24 gw 2.2.2.3 eth1
[root@test1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
1.1.1.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth1
3.3.3.0         2.2.2.3         255.255.255.0   UG    0      0        0 eth1
3.3.3.0         0.0.0.0         255.255.255.0   U     0      0        0 eth1
4.4.4.0         2.2.2.3         255.255.255.0   UG    0      0        0 eth1

#虚拟机3开启路由功能,由于ping3.3.3.4的时候开过,这里省略
#虚拟机3增加到4.4.4.0/24的条款
[root@test1 ~]# route add -net 4.4.4.0/24 eth1
[root@test1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
1.1.1.0         2.2.2.2         255.255.255.0   UG    0      0        0 eth0
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
3.3.3.0         0.0.0.0         255.255.255.0   U     0      0        0 eth1
4.4.4.0         0.0.0.0         255.255.255.0   U     0      0        0 eth1

#虚拟机4增加回1.1.1.0/24的条款,并指定3.3.3.3网关转发
[root@test1 ~]# route add -net 1.1.1.0/24 gw 3.3.3.3 eth0
[root@test1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
1.1.1.0         3.3.3.3         255.255.255.0   UG    0      0        0 eth0
3.3.3.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
4.4.4.0         0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1

#虚拟机3回去也要指定去1.1.1.0的由2.2.2.2网关转发,由于上面做过,这里省略

#测试
[root@test1 ~]# ping 4.4.4.3
PING 4.4.4.3 (4.4.4.3) 56(84) bytes of data.
64 bytes from 4.4.4.3: icmp_seq=1 ttl=62 time=3.08 ms
64 bytes from 4.4.4.3: icmp_seq=2 ttl=62 time=1.60 ms
64 bytes from 4.4.4.3: icmp_seq=3 ttl=62 time=1.60 ms
64 bytes from 4.4.4.3: icmp_seq=4 ttl=62 time=1.70 ms

路由优化

查看虚拟机1

[root@test1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
1.1.1.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
2.2.2.0         1.1.1.2         255.255.255.0   UG    0      0        0 eth0
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
3.3.3.0         1.1.1.2         255.255.255.0   UG    0      0        0 eth0
4.4.4.0         1.1.1.2         255.255.255.0   UG    0      0        0 eth0

这里可以看出很多都是由1.1.1.2转发的,所以可以合并成0.0.0.0给1.1.1.2转发,将其他条目删除从而优化

#删除
[root@test1 ~]# route del -net 2.2.2.0/24 gw 1.1.1.2 eth0
[root@test1 ~]# route del -net 3.3.3.0/24 gw 1.1.1.2 eth0
[root@test1 ~]# route del -net 4.4.4.0/24 gw 1.1.1.2 eth0

#添加0.0.0.0
[root@test1 ~]# route add default gw 1.1.1.2 dev eth0

#查看
[root@test1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         1.1.1.2         0.0.0.0         UG    0      0        0 eth0
1.1.1.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

查看虚拟机2

[root@test1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
1.1.1.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth1
3.3.3.0         2.2.2.3         255.255.255.0   UG    0      0        0 eth1
3.3.3.0         0.0.0.0         255.255.255.0   U     0      0        0 eth1
4.4.4.0         2.2.2.3         255.255.255.0   UG    0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1

合并3和4,右0.0.0.0通过2.2.2.3去转发

#删除
[root@test1 ~]# route del -net 3.3.3.0/24 gw 2.2.2.3
[root@test1 ~]# route del -net 4.4.4.0/24 gw 2.2.2.3

#添加
[root@test1 ~]# route add default gw 2.2.2.3 eth1

#查看
[root@test1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         2.2.2.3         0.0.0.0         UG    0      0        0 eth1
1.1.1.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth1
3.3.3.0         0.0.0.0         255.255.255.0   U     0      0        0 eth1

查看虚拟机3

[root@test1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
1.1.1.0         2.2.2.2         255.255.255.0   UG    0      0        0 eth0
2.2.2.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
3.3.3.0         0.0.0.0         255.255.255.0   U     0      0        0 eth1
4.4.4.0         0.0.0.0         255.255.255.0   U     0      0        0 eth1

#没啥可以优化的,嘻嘻嘻

查看虚拟机4

[root@test1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
1.1.1.0         3.3.3.3         255.255.255.0   UG    0      0        0 eth0
3.3.3.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
4.4.4.0         0.0.0.0         255.255.255.0   U     0      0        0 eth1

#也没啥可以优化的,嘻嘻嘻
#测试一下ping4.4.4.3
[root@test1 ~]# ping 4.4.4.3
PING 4.4.4.3 (4.4.4.3) 56(84) bytes of data.
64 bytes from 4.4.4.3: icmp_seq=1 ttl=62 time=1.76 ms
64 bytes from 4.4.4.3: icmp_seq=2 ttl=62 time=1.70 ms
64 bytes from 4.4.4.3: icmp_seq=3 ttl=62 time=1.47 ms
64 bytes from 4.4.4.3: icmp_seq=4 ttl=62 time=1.68 ms

#下面是之前ping的用时
[root@test1 ~]# ping 4.4.4.3
PING 4.4.4.3 (4.4.4.3) 56(84) bytes of data.
64 bytes from 4.4.4.3: icmp_seq=1 ttl=62 time=3.08 ms
64 bytes from 4.4.4.3: icmp_seq=2 ttl=62 time=1.60 ms
64 bytes from 4.4.4.3: icmp_seq=3 ttl=62 time=1.60 ms
64 bytes from 4.4.4.3: icmp_seq=4 ttl=62 time=1.70 ms

#其实没啥卵用,效率层面并没有提高多少,更何况谁不用专业的路由去,闲的蛋疼去用虚拟机,只是条目看着清楚美观了一点而已。

004 路由

原文:https://www.cnblogs.com/zhaokunhao/p/14629583.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!