如何配置具有不同网关的 2 个网络接口

如何配置具有不同网关的 2 个网络接口

以下是我的文件中的内容 /etc/network/interfaces

auto lo    
iface lo inet loopback

auto eth0    
iface eth0 inet static    
address 172.168.10.252    
netmask 255.255.255.0    
network 172.168.10.0    
broadcast 172.168.10.255    
gateway 172.168.10.1    

iface eth1 inet static    
address 172.168.10.251    
netmask 255.255.255.0    
network 172.168.10.0    
broadcast 172.168.10.255    
gateway 172.168.10.10

我想使用 eth0 作为本地网络,eth1 作为互联网。谢谢

答案1

要配置两个接口(例如 eth0 和 eth1)以使用两个网络192.168.0.0/24, 可以使用10.10.0.0/24一个工具来实现这一点。iproute2

脚步:

  1. 编辑您的/etc/network/interfaces

    auto lo
    iface lo inet loopback
    
    # The primary network interface
    
    allow-hotplug eth0
    iface eth0 inet static
        address 192.168.0.10
        netmask 255.255.255.0
        gateway 192.168.0.1
    
    # The secondary network interface
    allow-hotplug eth1
        iface eth1 inet static
        address 10.10.0.10
        netmask 255.255.255.0
    
  2. 通过编辑“/etc/iproute2/rt_tables”添加第二个路由表:

    #
    # reserved values
    #
    255     local
    254     main
    253     default
    0       unspec
    #
    # local
    #
    #1      inr.ruhep
    1 rt2
    
  3. 填充新路由表:

    ip route add 10.10.0.0/24 dev eth1 src 10.10.0.10 table rt2
    ip route add default via 10.10.0.1 dev eth1 table rt2
    
    # The first command says that the network, 10.10.0.0/24, can be reached through the eth1 interface.
    # The second command sets the default gateway.
    
  4. 添加路由规则:

    ip rule add from 10.10.0.10/32 table rt2
    ip rule add to 10.10.0.10/32 table rt2
    
    # These rules say that both traffic from the IP address, 10.10.0.10, as well as traffic directed to 
    # or through this IP address, should use the rt2 routing table
    
  5. 将配置添加到以下位置以使配置永久生效/etc/network/interfaces

    iface eth1 inet static
        address 10.10.0.10
        netmask 255.255.255.0
        post-up ip route add 10.10.0.0/24 dev eth1 src 10.10.0.10 table rt2
        post-up ip route add default via 10.10.0.1 dev eth1 table rt2
        post-up ip rule add from 10.10.0.10/32 table rt2
        post-up ip rule add to 10.10.0.10/32 table rt2
    

来源:

https://www.thomas-krenn.com/en/wiki/Two_Default_Gateways_on_One_System

相关内容