如何使用不同的网关配置 2 个网络接口

aim*_*ots 8 server network-manager networking dhcp ethernet

这是我的文件中的内容 /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
Run Code Online (Sandbox Code Playgroud)

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

Geo*_*sen 8

要配置两个接口(例如 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
    
    Run Code Online (Sandbox Code Playgroud)
  2. 通过编辑`/etc/iproute2/rt_tables 添加第二个路由表:

    #
    # reserved values
    #
    255     local
    254     main
    253     default
    0       unspec
    #
    # local
    #
    #1      inr.ruhep
    1 rt2
    
    Run Code Online (Sandbox Code Playgroud)
  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.
    
    Run Code Online (Sandbox Code Playgroud)
  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
    
    Run Code Online (Sandbox Code Playgroud)
  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
    
    Run Code Online (Sandbox Code Playgroud)

来源:

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