多网卡联网

use*_*645 16 networking 12.04

我有一个 Ubuntu 12.04 服务器,里面有 4 个网络适配器。我需要将每个 NIC 用于单独的功能。这是我的设置的描述:

etho = 10.234.0.2 netmask = 255.255.255.252 gw = 10.234.0.1 This is on vlan 234
eth1 = 10.235.0.2 netmask = 255.255.255.252 gw = 10.235.0.1 This is on vlan 235
eth2 = 10.236.0.2 netmask = 255.255.255.252 gw = 10.236.0.1 This is on vlan 236
eth3 = 10.237.0.2 netmask = 255.255.255.252 gw = 10.237.0.1 This is on vlan 237
Run Code Online (Sandbox Code Playgroud)

我需要能够为单独的 Web 服务引导进出各个 IP 地址的流量。即 10.235.0.2 是一个网站,10.236.0.2 是一个不同的网站,而 10.237.0.2 是第三个网站。第一个 IP 用于管理服务器。

我认为问题是路由问题,但我对 Linux 不够熟悉,无法完全了解路由功能的来龙去脉。

这是我的/etc/network/interfaces文件中的内容:

auto lo
iface lo inet loopback

# WWW Management
auto eth0
iface eth0 inet static
address 10.234.0.2
netmask 255.255.255.252
gateway 10.234.0.1
nameseervers 10.230.1.103, 10.230.70.70

# WWW
auto eth1
iface eth1 inet static
address 10.235.0.2
netmask 255.255.255.252
gateway 10.235.0.1

# WTB
#auto eth2
#iface eth2 inet static
#address 10.236.0.2
#netmask 255.255.255.252
#gateway 10.236.0.1

# Moodle
#auto eth3
#iface eth3 inet static
#address 10.237.0.2
#netmask 255.255.255.252
#gateway 10.237.0.1
Run Code Online (Sandbox Code Playgroud)

我禁用了最后两个网络只是为了缓解混乱。

在此先感谢所有的帮助、意见和建议。

Cra*_*ine 15

在对 eth0 进行正常配置后,我又回来添加了 eth1 的配置。仅当 eth0 启动时,路由表为:

# ip route show
192.168.0.96/27 dev eth0 proto kernel scope link src 192.168.0.126
default via 192.168.0.97 dev eth0 metric 100
Run Code Online (Sandbox Code Playgroud)

但是一旦我调出 eth1,默认路由语句的顺序就决定了始终使用哪个接口。如下图,正好选择eth1路由到192.168.1.65网关。

# ip route show
192.168.0.96/27 dev eth0 proto kernel scope link src 192.168.0.126
192.168.1.64/27 dev eth1 proto kernel scope link src 192.168.1.93
default via 192.168.1.65 dev eth1 metric 100
default via 192.168.0.97 dev eth0 metric 100
Run Code Online (Sandbox Code Playgroud)

只有一个网关声明

第一个问题可能是额外的“via 192.168.1.65”默认路由。如果 /etc/network/interfaces 中的 eth1 定义有“网关 192.168.1.65”语句,它就会出现在那里。所以删除任何额外的网关语句,并反弹接口:

# ifdown eth1
# ifup eth1
# ip route show
192.168.0.96/27 dev eth0 proto kernel scope link src 192.168.0.126
192.168.1.64/27 dev eth1 proto kernel scope link src 192.168.1.93
default via 192.168.0.97 dev eth0 metric 100
Run Code Online (Sandbox Code Playgroud)

设置新的路由表

创建一个新的、单独的路由表,其中包含适用于流出 eth1 的所有流量的默认路由。这里的表号并不重要;101 根本不是主要的路由表。在 /etc/network/interfaces 中的 eth1 配置上使用“post-up”命令执行此操作。在eth1上只添加一个post-up;不要将它添加到任何 eth1: 子接口。

post-up ip route add default via 192.168.1.65 dev eth1 table 101
Run Code Online (Sandbox Code Playgroud)

反弹eth1。主路由表没有变化,如果 eth1 是 up,表 101 将包含 via 192.168.1.65 默认路由。

# ifdown eth1
# ip route show
192.168.0.96/27 dev eth0 proto kernel scope link src 192.168.0.126
default via 192.168.0.97 dev eth0 metric 100

# ip route show table 101   (ie, table is empty, no output)
# ifup eth1
# ip route show
192.168.0.96/27 dev eth0 proto kernel scope link src 192.168.0.126
192.168.1.64/27 dev eth1 proto kernel scope link src 192.168.1.93
default via 192.168.0.97 dev eth0 metric 100

# ip route show table 101
default via 192.168.1.65 dev eth1
Run Code Online (Sandbox Code Playgroud)

新路由规则

添加路由规则以使用表 101 为应该出 eth1 的数据包选择默认路由。

# ip rule add from 192.168.1.64/27 lookup 101
# ip rule show
0:     from all lookup local
32765: from 192.168.1.64/27 lookup 101
32766: from all lookup main
32767: from all lookup default
Run Code Online (Sandbox Code Playgroud)

也将规则添加到/etc/network/interfaces文件中:

post-up ip rule add from 192.168.1.64/27 lookup 101
Run Code Online (Sandbox Code Playgroud)

现在确保添加清理以在接口关闭时删除路由和规则:

post-down ip rule del from 192.168.1.64/27
post-down ip route del default via 192.168.1.65 table 101
Run Code Online (Sandbox Code Playgroud)

[针对 ubuntu 16.04+ 进行编辑] 就像这里指出的那样并且根据我从这个帮助中所做的测试,ip route2 改变了他的命令结构。对于制作工作,您将不得不稍微调整一下,按照man ip 要点的顺序来做。

up ip route add default table 101 dev eth1 via 192.168.1.65
up ip rule add from 192.168.1.64/27 lookup 101
down ip rule del from 192.168.1.64/27
down ip route del default table 101 via 192.168.1.65
Run Code Online (Sandbox Code Playgroud)

或者,您将在 ifdown - ifup 命令之后结束,并带有错误消息 @ifdown 命令(表示外围设备未正确配置的标准消息),以及 @ifup 表 101 中缺少路由。