随笔-网络-Linux多网卡同网段解决方法
问题描述
默认的配置下,服务器多网卡同网段ip,外部访问任一ip,服务器返回的mac地址是某一张网卡的mac地址。
实验:
服务器配置:
enp0s3 08:00:27:e3:f8:67 192.168.76.29
enp0s8 08:00:27:50:46:54 192.168.76.85
enp0s9 08:00:27:5a:bd:4a 192.168.76.86
enp0s10 08:00:27:68:46:06 192.168.76.87
[root@localhost ~]# ip r
default via 192.168.76.1 dev enp0s10 proto static metric 100
default via 192.168.76.1 dev enp0s3 proto static metric 101
default via 192.168.76.1 dev enp0s8 proto static metric 102
default via 192.168.76.1 dev enp0s9 proto static metric 103
192.168.76.0/24 dev enp0s10 proto kernel scope link src 192.168.76.87 metric 100
192.168.76.0/24 dev enp0s3 proto kernel scope link src 192.168.76.29 metric 101
192.168.76.0/24 dev enp0s8 proto kernel scope link src 192.168.76.85 metric 102
192.168.76.0/24 dev enp0s9 proto kernel scope link src 192.168.76.86 metric 103
陪测机器发起arp:
$ sudo arping -I enp2s0 192.168.76.29
ARPING 192.168.76.29
60 bytes from 08:00:27:68:46:06 (192.168.76.29): index=0 time=2.894 msec
...
$ sudo arping -I enp2s0 192.168.76.85
ARPING 192.168.76.85
60 bytes from 08:00:27:68:46:06 (192.168.76.85): index=0 time=2.955 msec
...
$ sudo arping -I enp2s0 192.168.76.86
ARPING 192.168.76.86
60 bytes from 08:00:27:68:46:06 (192.168.76.86): index=0 time=3.276 msec
...
$ sudo arping -I enp2s0 192.168.76.87
ARPING 192.168.76.87
60 bytes from 08:00:27:68:46:06 (192.168.76.87): index=0 time=2.804 msec
解决方案1-配置IP路由
系统配置:
开启ip转发规则:
sysctl -w net.ipv4.ip_forward=1 # 开启ip转发规则
net.ipv4.conf.X.rp_filter = 0 # X 填 all default 各个网卡
路由设置:
- 假设默认网卡eth0,现在想利用eth1,ip为192.168.xx.xx
- 添加策略路由规则,将源ip为192.168.xx.xx的数据包使用table 1
ip rule add from 192.168.xx.xx table 1
- 在table 1中添加默认路由,将数据包发送到eth1口:
ip route add default dev eth1 table 1
示例:
假设两张网卡eth0 eth1, ip 分别配置为192.168.78.191 192.168.78.120
-
编辑网卡配置文件,注释掉GATEWAY 配置
-
编辑/etc/iproute2/rt_tables 添加路由表
echo "191 net_191" >> /etc/iproute2/rt_tables
-
重启服务器
-
添加路由规则
# echo "191 net_191" >> /etc/iproute2/rt_tables
ip route add 192.168.78.0/24 dev eth0 src 192.168.78.191 table net_191
ip route add default dev eth0 table net_191
ip rule add from 192.168.78.191 table net_191
# echo "120 net_120" >> /etc/iproute2/rt_tables
ip route add 192.168.78.0/24 dev eth1 src 192.168.78.120 table net_120
ip route add default dev eth1 table net_120
ip rule add from 192.168.78.120 table net_120
陪测服务器,使用arping,如果返回对应网卡的mac地址正确即OK
解决方案2-arp_announce
# 禁用接口转发
net.ipv4.ip_forward=0
# 控制内核在发送ARP请求或响应ARP请求时,选择哪个IP地址作为ARP包的源IP地址 2使得能够根据ip选择当前网卡mac返回
net.ipv4.conf.all.arp_announce=2
net.ipv4.conf.default.arp_announce=2
# arp包中的目的地址是必须是该网卡,否则丢包
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.default.arp_ignore=1
一般需要同时设置rp_filter=0,允许流量非对称路由(入接口和回包出接口可以不同),解决由此导致的合法入站数据包被内核丢弃:
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.lo.rp_filter = 0
net.ipv4.conf.enp0s3.rp_filter = 0
net.ipv4.conf.enp0s8.rp_filter = 0
net.ipv4.conf.enp0s9.rp_filter = 0
net.ipv4.conf.enp0s10.rp_filter = 0
另外一个问题-出网口
上述问题为解决入网口问题(外部访问服务器),还有出网口问题(服务器发起访问)需要确认。如果没有改路由表,多网卡同网段,由于默认路由存在,服务器根据远端地址选择本地网口时都会命中默认路由(指定本地ip地址也不起作用)。
解决方式:
-
方案1:sock发送时绑定网卡;
-
方案2:配置静态路由规则(route add -net 远端地址 netmask 255.255.255.255 gw 下一跳地址 dev 网口) 关于下一跳地址,如果两边都是同网段,那么直接填写远端地址(因为是同网段设备,下一跳就是目标设备本身),如果不是填写真实网关地址
本文来自博客园,作者:LiYanbin,转载请注明原文链接:https://www.cnblogs.com/stellar-liyanbin/p/18190079