我有几个Linux的盒子,每个2x网卡连接到同一个networking10.0.0.0/8但每个都有一个其他的网关。
我希望能够使用NIC和IP进行通信,所以如果应用程序X绑定到IP .187,那么应该使用eth0。 如果应用程序Y绑定到IP .189,则应使用eth1。
现在只有.187正在回应请求。 (如果我将默认路由更改为eth1 .189,则出站stream量正确使用eth1)
Box 1: eth0 = 10.5.68.187/31 eth0_gw = 10.5.68.186/31 eth1 = 10.5.68.189/31 eth1_gw = 10.5.68.188/31
基于我的研究, 在这里 ,我在这里创build了一个bash脚本,它应该添加路由,所以可以通过两个nics进行通信。
#/bin/bash # My Vars with IP and GW for eth0 eth0_ip=$(ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1) eth0_gw=$(ip route list dev eth0 | awk '{print $1}' | tail -1 | cut -d'/' -f1) eth1_ip=$(ip -o -4 addr list eth1 | awk '{print $4}' | cut -d/ -f1) eth1_gw=$(ip route list dev eth1 | awk '{print $1}' | tail -1 | cut -d'/' -f1) #ip route add 10.0.0.0/8 dev eth0 table 1 priority 100 #ip route add ${eth0_ip} dev eth0 table 1 ip route add default via ${eth0_gw} dev eth0 table 1 ip rule add from ${eth0_ip}/32 table 1 #ip route add 10.0.0.0/8 dev eth1 table 2 priority 110 #ip route add ${eth1_ip} dev eth1 table 2 ip route add default via ${eth1_gw} dev eth1 table 2 ip rule add from ${eth1_ip}/32 table 2
ip路由刷新caching
我做了一些脚本的变化,但没有工作
[node]# ip route default via 10.1.229.186 dev eth0 10.1.229.186/31 dev eth0 proto kernel scope link src 10.1.229.187 10.1.229.188/31 dev eth1 proto kernel scope link src 10.1.229.189 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 172.18.0.0/16 dev docker_gwbridge proto kernel scope link src 172.18.0.1 [node]# ip route show table 1 10.1.229.187 dev eth0 scope link [node]# ip route show table 2 10.1.229.189 dev eth1 scope link
[]]# ip route get 10.5.68.187 from 10.1.229.187 10.5.68.187 from 10.1.229.187 via 10.1.229.186 dev eth0 cache []# ip route get 10.5.68.187 from 10.1.229.189 10.5.68.187 from 10.1.229.189 via 10.1.229.188 dev eth1 cache
从另一台机器。
ping 10.1.229.187 # OK ping 10.1.229.189 # NOK nmap 10.1.229.187 -p 22 # OK nmap 10.1.229.189 -p 22 # NOK
那么我怎样才能设置路由,使其工作,同时与.187和.189进行通信。
有了这个设置,我可以获得某种成功。
eth0_ip=$(ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1) eth0_gw=$(ip route list dev eth0 | awk '{print $1}' | tail -1 | cut -d'/' -f1) eth1_ip=$(ip -o -4 addr list eth1 | awk '{print $4}' | cut -d/ -f1) eth1_gw=$(ip route list dev eth1 | awk '{print $1}' | tail -1 | cut -d'/' -f1) #ip route add 10.0.0.0/8 dev eth0 table 1 #ip route add 169.254.0.0/16 dev eth0 table 1 #ip route add ${eth0_ip} dev eth0 table 1 ip route add default via ${eth0_gw} dev eth0 table 1 #ip rule add to ${eth0_ip} table 1 ip rule add from ${eth0_ip} table 1 #ip route add 10.0.0.0/8 dev eth1 table 2 #ip route add 169.254.0.0/16 dev eth1 table 2 #ip route add ${eth1_ip} dev eth1 table 2 ip route add default via ${eth1_gw} dev eth1 table 2 #ip rule add to ${eth1_ip} table 2 ip rule add from ${eth1_ip} table 2
在我应用上面的脚本之后,我修改了默认路由,切换到eth1,然后返回,之后我就可以ping到.187和.189。 (在另一个例子中,我也完全删除它)我不知道这里的问题是什么。
# remove and add route ip route change default via ${eth1_gw} dev eth1 ip route change default via ${eth0_gw} dev eth0 ip route flush cache
检查,有答复(也许答复是通过其他接口出去)或答复丢失。
检查反向pathfilter的设置(检查'nstat -az'或'netstat -S'输出中的计数器 – 对于由rp_filter丢弃的数据包,存在TcpExtIPReversePathFilter)。 禁用它或设置为松散模式(请参阅sysctl设置说明 )。 查找传入数据包的反向路由以确认假设。
我想你应该为直接连接的networking添加路由到路由表中,因为它需要通过对应的网关的arpparsing和与直接连接的networking中的其他主机的通信。 这些设置应该足以解决你的情况:
ip route add 10.0.0.0/8 dev eth0表1
ip route 0/0通过$ {eth0_gw} dev eth0表1
ip route add 10.0.0.0/8 dev eth1表1
ip route 0/0通过$ {eth1_gw} dev eth1表2
ip规则从$ {eth0_ip}查找1中添加
ip规则从$ {eth1_ip}查找2添加
此外,你应该知道,这个设置是什么情况,这些接口上的IP地址重叠寻址是不同的。 否则,你应该使用更复杂的scheme与CONNMARK和防火墙标记。