Linux:三个默认网关?

Dan*_*iel 2 linux routing

我的服务器有三个默认网关,怎么可能?不应该有一个默认的 gw 吗?

我有三个 NIC,每个都连接到一个单独的子网:

server1:~#  route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.5.0.0        *               255.255.255.224 U     0      0        0 eth3
localnet        *               255.255.255.224 U     0      0        0 eth0
192.168.8.0     *               255.255.255.192 U     0      0        0 eth1
default         10.5.0.1        0.0.0.0         UG    0      0        0 eth3
default         192.168.8.1     0.0.0.0         UG    0      0        0 eth1
default         10.1.0.1        0.0.0.0         UG    0      0        0 eth0
Run Code Online (Sandbox Code Playgroud)

有时,我无法ping通互联网上的主机,有时我可以。我想要的是通过特定 NIC 路由到 Internet (0.0.0.0) 的流量。我可以只为 0.0.0.0 和默认 gw 添加一个路由到 eth0-3 接口之一吗?它会断开我的连接吗?

我正在使用 Debian,这是我的 /etc/network/interfaces:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
iface eth0 inet static
    address 10.1.0.4
    netmask 255.255.255.224
    network 10.1.0.0
    broadcast 10.1.0.31
    gateway 10.1.0.1

allow-hotplug eth1
iface eth1 inet static
        address 192.168.8.4
        netmask 255.255.255.192
        network 192.168.8.0
        broadcast 192.168.8.63
        gateway 192.168.8.1

allow-hotplug eth3
iface eth3 inet static
        address 10.5.0.4
        netmask 255.255.255.224
        network 10.5.0.0
        broadcast 10.5.0.31
        gateway 10.5.0.1
Run Code Online (Sandbox Code Playgroud)

Sac*_*acx 5

您在接口文件上配置了所有网关。只留下您真正需要的网关。

Linux 支持多个网关……但这取决于您想做什么。

  1. 您可以选择在每个 gw 上配置不同的指标,它会像故障转移一样工作
  2. 可以使用基于iproute2和内核策略路由的源路由
  3. 您可以使用 BGP 来平衡您的流量。

这是您的选择,但如果您只需要 1 gw,则从您的接口文件中删除其他 2 个(删除以网关 xxx.yyy.zzzz.wwww 开头的行,只保留网关所需的内容)。