Linux中同一子网上有两个networking接口和两个IP地址

我最近遇到了一种情况,我需要将两个IP地址分配给一个Linux主机,以便我们可以运行两个SSL / TLS站点。 我的第一个方法是使用IP别名,例如使用eth0:0,eth0:1等,但是我们的networkingpipe理员有一些相当严格的安全设置来压扁这个想法:

  1. 他们使用DHCP监听,通常不允许静态IP地址。 静态寻址是通过使用静态DHCP条目来完成的,所以相同的MAC地址总是获得相同的IP分配。 这个function可以在每个交换机端口被禁用,如果你问,你有一个原因(谢天谢地,我有一个良好的关系networking家伙,这是不难做的)。
  2. 在交换机端口禁用了DHCP监听function的情况下,他们必须在交换机上设置一条规则,允许MAC地址X的IP地址为Y.不幸的是,这也有一个副作用,就是只允许MAC地址X IP地址Y. IP别名要求MAC地址X分配了两个IP地址,所以这不起作用。

在交换机configuration上可能有解决这些问题的方法,但为了保持与networkingpipe理员的良好关系,我试图find另一种方式。 有两个networking接口似乎是下一个合乎逻辑的步骤。 幸运的是,这个Linux系统是一个虚拟机,所以我能够轻松地添加第二个networking接口(无需重新启动,我可以添加 – 非常酷)。 几个击键后,我有两个networking接口启动并运行,并从DHCP拉动IP地址。

但问题出现了:networkingpipe理员可以看到(在交换机上)两个接口的ARP条目,但是只有我提出的第一个networking接口会响应ping或任何types的TCP或UDPstream量。

大量的挖掘和戳,这是我想出了。 它似乎工作,但似乎是很多工作,似乎应该是简单的东西。 有什么其他想法?


步骤1:在所有接口上启用ARP过滤:

# sysctl -w net.ipv4.conf.all.arp_filter=1 # echo "net.ipv4.conf.all.arp_filter = 1" >> /etc/sysctl.conf 

从Linux内核文档中的文件networking / ip-sysctl.txt:

 arp_filter - BOOLEAN 1 - Allows you to have multiple network interfaces on the same subnet, and have the ARPs for each interface be answered based on whether or not the kernel would route a packet from the ARP'd IP out that interface (therefore you must use source based routing for this to work). In other words it allows control of which cards (usually 1) will respond to an arp request. 0 - (default) The kernel can respond to arp requests with addresses from other interfaces. This may seem wrong but it usually makes sense, because it increases the chance of successful communication. IP addresses are owned by the complete host on Linux, not by particular interfaces. Only for more complex setups like load- balancing, does this behaviour cause problems. arp_filter for the interface will be enabled if at least one of conf/{all,interface}/arp_filter is set to TRUE, it will be disabled otherwise 

步骤2:实现基于源的路由

我基本上只是遵循http://lartc.org/howto/lartc.rpdb.multiple-links.html的指导 ,虽然这个页面是用不同的目标写的(处理两个ISP)。

假设子网为10.0.0.0/24,网关为10.0.0.1,eth0的IP地址为10.0.0.100,eth1的IP地址为10.0.0.101。

在/ etc / iproute2 / rt_tables中定义两个名为eth0和eth1的新路由表:

 ... top of file omitted ... 1 eth0 2 eth1 

定义这两个表的路由:

 # ip route add default via 10.0.0.1 table eth0 # ip route add default via 10.0.0.1 table eth1 # ip route add 10.0.0.0/24 dev eth0 src 10.0.0.100 table eth0 # ip route add 10.0.0.0/24 dev eth1 src 10.0.0.101 table eth1 

定义何时使用新的路由表的规则:

 # ip rule add from 10.0.0.100 table eth0 # ip rule add from 10.0.0.101 table eth1 

主路由表已经被DHCP(在这种情况下甚至没有明确表示它是必须的),但基本上等同于:

 # ip route add default via 10.0.0.1 dev eth0 # ip route add 130.127.48.0/23 dev eth0 src 10.0.0.100 # ip route add 130.127.48.0/23 dev eth1 src 10.0.0.101 

瞧! 一切似乎工作得很好。 发送ping到两个IP地址工作正常。 从该系统发送ping到其他系统,并强制ping使用特定的接口工作正常( ping -I eth0 10.0.0.1ping -I eth1 10.0.0.1 )。 而且最重要的是,来自/来自任何IP地址的所有TCP和UDPstream量按预期工作。


所以我的问题是:有没有更好的方法来做到这一点? 这似乎是一个看似简单的问题很多工作。


更新:上面的解决scheme结束了不完整。 如果stream量停留在同一个子网上,事情就可以正常工作,但使用第二个接口与其他子网的通信将无法正常工作。 而不是挖一个更大的洞,我最终与networkingpipe理员交谈,让他们允许一个接口的多个IP地址,并使用IP别名(如eth0和eth0:0)。

是的,更好的方法是build立一个适当的业务案例,让他们放松交换机上的规则,以便在一个NIC上拥有多个IP。