多个Nics联网

我有一个Ubuntu 12.04服务器,里面有4个网络适配器。 我需要将每个NIC用于单独的function。 以下是我的设置说明:

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 

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

我认为这个问题是一个路由问题,但我对Linux的新知识还不足以完全理解路由function的细节。

这是我的/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 

为了缓解混乱,我已经禁用了最后两个网络。

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

在完成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 

但是一旦我提出了eth1,默认路由语句的顺序决定了总是使用哪个接口。 如下所示,它恰好选择到192.168.1.65网关的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.1.65 dev eth1 metric 100 default via 192.168.0.97 dev eth0 metric 100 

只有一个网关声明

第一个问题可能是额外的’via 192.168.1.65’默认路由。 如果/ etc / network / interfaces中的eth1定义具有“gateway 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 

设置新的路由表

创建一个新的单独路由表,其中包含适用于流出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 

反弹eth1。 主路由表不变,如果eth1启动,表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 

新的路由规则

添加路由规则以使用表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 

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

 post-up ip rule add from 192.168.1.64/27 lookup 101 

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

 post-down ip rule del from 192.168.1.64/27 post-down ip route del default via 192.168.1.65 table 101 

[编辑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 

或者,您将在ifdown-ifup命令后输出错误消息@ifdown命令(标准消息说外围设备未正确配置),以及@ifup表101中没有路由。