Linux路由

作者: Liang_JC | 来源:发表于2020-03-08 23:07 被阅读0次

    实现A与B跨三网段互相访问

    • 环境说明:
    • 五台机器,客户端:client1、client2,充当路由:centos6.10、srv1、srv2
    • centos6.10:vmnet3、4,srv1:vmnet4、5,srv2:vmnet5、6
    • client1:vmnet3、client2:vmnet6


      最终实现
    虚拟机网卡信息

    实现配置

    #R1
    [root@R1 ~]$vim /etc/sysconfig/network-scripts/ifcfg-eth2
    DEVICE=eth2
    NAME=vmnet3
    BOOTPROTO=static
    IPADDR=192.168.1.254
    PREFIX=24
    ONBOOT=yes
    [root@R1 ~]$vim /etc/sysconfig/network-scripts/ifcfg-eth1
    DEVICE=eth1
    NAME=vmnet4
    BOOTPROTO=static
    IPADDR=192.168.10.1
    PREFIX=24
    ONBOOT=yes
    [root@R1 ~]$ service NetworkManager stop
    [root@R1 ~]$ service network restart
    [root@R1 ~]$ route add -net 172.16.1.0/24 gw 192.168.10.254 dev eth1
    [root@R1 ~]$ route add -net 1.1.1.0/24 gw 192.168.10.254 dev eth1
    [root@R1 ~]$ sysctl -w net.ipv4.ip_forward=1
    [root@R1 ~]$ route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth2
    172.16.1.0      192.168.10.254  255.255.255.0   UG    0      0        0 eth1
    192.168.139.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
    1.1.1.0         192.168.10.254  255.255.255.0   UG    0      0        0 eth1
    192.168.10.0    192.168.10.254  255.255.255.0   UG    0      0        0 eth1
    192.168.10.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 eth2
    169.254.0.0     0.0.0.0         255.255.0.0     U     1004   0        0 eth1
    0.0.0.0         192.168.139.2   0.0.0.0         UG    0      0        0 eth0
        
    #R2
    [root@R2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1
    DEVICE=eth1
    NAME=vmnet4            
    BOOTPROTO=none
    IPADDR=172.16.1.253
    PREFIX=24
    ONBOOT=yes
    [root@R2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth2
    DEVICE=eth2
    NAME=vmnet5            
    BOOTPROTO=none
    IPADDR=172.16.1.253
    PREFIX=24
    ONBOOT=yes
    [root@R2 ~]# systemctl stop NetworkManager
    [root@R2 ~]# systemctl restart network
    [root@R2 ~]# route add -net 192.168.1.0/24 gw 192.168.10.1 dev eth1
    [root@R2 ~]# route add -net 1.1.1.0/24 gw 172.16.1.254 dev eth2
    [root@R2 ~]$ sysctl -w net.ipv4.ip_forward=1
    [root@R2 ~]#route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         192.168.139.2   0.0.0.0         UG    100    0        0 eth0
    1.1.1.0         172.16.1.254    255.255.255.0   UG    0      0        0 eth2
    172.16.1.0      0.0.0.0         255.255.255.0   U     102    0        0 eth2
    192.168.1.0     192.168.10.1    255.255.255.0   UG    0      0        0 eth1
    192.168.10.0    0.0.0.0         255.255.255.0   U     101    0        0 eth1
    192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
    192.168.139.0   0.0.0.0         255.255.255.0   U     100    0        0 eth0
    
    #R3
    [root@R3 ~]$ vim /etc/sysconfig/network-scripts/ifcfg-eth1
    DEVICE=eth1
    NAME=vmnet5
    BOOTPROTO=none
    IPADDR=172.16.1.254
    PREFIX=24
    ONBOOT=yes
    [root@R3 ~]$ vim /etc/sysconfig/network-scripts/ifcfg-eth2
    DEVICE=eth2
    NAME=vmnet6
    BOOTPROTO=none
    IPADDR=1.1.1.254
    PREFIX=24
    ONBOOT=yes
    [root@R3 ~]$ systemctl stop NetworkManager
    [root@R3 ~]$ systemctl restart network
    [root@R3 ~]$ route add -net 192.168.10.0/24 gw 172.16.1.253 dev eth1
    [root@R3 ~]$ route add -net 192.168.1.0/24 gw 172.16.1.253 dev eth1
    [root@R3 ~]$ sysctl -w net.ipv4.ip_forward=1
    [root@R3 ~]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         192.168.139.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 eth2
    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
    169.254.0.0     0.0.0.0         255.255.0.0     U     1004   0        0 eth2
    172.16.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth1
    192.168.1.0     172.16.1.253    255.255.255.0   UG    0      0        0 eth1
    192.168.10.0    172.16.1.253    255.255.255.0   UG    0      0        0 eth1
    192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
    192.168.139.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
    

    测试

    #client1:
    ip addr add 192.168.1.7/24 dev eth0
    route add default gw 192.168.1.254
    ping -c3 1.1.1.1
    
    image.png
    #client2:
    ip addr add 1.1.1.1/24 dev eth0
    route add default gw 1.1.1.1
    ping -c3 192.168.1.7
    
    image.png

    相关文章

      网友评论

        本文标题:Linux路由

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