在 Azure 中使用第二个(或多个)NIC 访问互联网

Access internet with second (or multiple) NIC in Azure

我在 Azure 中有一个 VM,带有 Ubuntu 19.04 和 4 个 NIC,每个都有一个 public IP 附加到它,例如,这是第二个 NIC:

如您所见,它有 public IP 191.234.186.19。

我的 objective 是能够从每个具有不同 IP 地址的 NIC 访问互联网,但在 VM 中我只能通过第一个网络 eth0 访问它。

查看 # route -n 我得到以下输出:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.1        0.0.0.0         UG    100    0        0 eth0
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth2
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth3
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1
168.63.129.16   10.0.0.1        255.255.255.255 UGH   100    0        0 eth0
169.254.169.254 10.0.0.1        255.255.255.255 UGH   100    0        0 eth0

我尝试使用以下命令为其他 NIC 添加路由:

sudo ip route add default via 10.0.0.1 dev eth1 metric 101
sudo ip route add default via 10.0.0.1 dev eth2 metric 102
sudo ip route add default via 10.0.0.1 dev eth3 metric 103

但是我仍然无法使用这些 NIC 连接到互联网,只能 eth0

我错过了什么吗?我还需要 change/configure 其他任何东西才能完成这项工作吗?

为了完整起见,我使用此命令来测试连接性:

curl --interface eth1 api.ipify.org?format=json -w "\n"

而我的虚拟机与本文中的相同:article

好像只有 eth0 有默认网关。如果没有默认网关,您无法在本地网络之外进行通信

我怀疑你可能需要 1) 为第二个、第三个和第四个接口定义新的路由 tables, 2) 将默认路由添加到 tables 3) 并定义规则以确保 return 数据包始终路由回原始数据包发送的同一接口。

这里有几个示例供您参考(例如 this and this)。

我只是指出几点,您在针对具体情况进行设置时可能会觉得有帮助: iproute allows you to maintain multiple routeing tables. To instruct the OS to utilise different routing tables you need to define rules 在最简单的形式中,将强制使用与发起连接的接口相对应的路由 table。