首页 > 技术文章 > 同台机器2个网卡配置同段IP

angelfzt 2016-03-09 15:30 原文

看个例子:
1.on server
ifconfig eth4 192.168.1.10/24 up
ifconfig eth5 192.168.1.11/24 up
2.on client
ifconfig eth4 192.168.1.20/24 up

3.on client
ping 192.168.1.10 -c 3
ping 192.168.1.11 -c 3

Actual results:
[root@client ~]# arp
Address HWtype HWaddress Flags Mask Iface
192.168.1.11 ether 00:1B:21:4A:FE:98 C eth4 <===== 错了吧?
192.168.1.10 ether 00:1B:21:4A:FE:98 C eth4

Expected results:
[root@client ~]# arp
Address HWtype HWaddress Flags Mask Iface
192.168.1.11 ether 00:1B:21:4A:FE:99 C eth4
192.168.1.10 ether 00:1B:21:4A:FE:98 C eth4

Additional info:
23:28:23.272726 arp reply 192.168.1.10 is-at 00:1b:21:4a:fe:99 (oui Unknown)
23:28:23.272783 arp reply 192.168.1.10 is-at 00:1b:21:4a:fe:98 (oui Unknown)

23:28:46.750823 arp reply 192.168.1.11 is-at 00:1b:21:4a:fe:99 (oui Unknown)
23:28:46.750850 arp reply 192.168.1.11 is-at 00:1b:21:4a:fe:98 (oui Unknown)
这个问题是是由Linux的路由和arp机制造成的,主要是路由。
在Linux中,IP地址是主机的属性,而不是接口的属性。
这就造成在反向路由查找时,可以考虑包进入的接口,也可以不考虑。
此时arp请求包会有两个应答,client会选择后一个。
关键是server会接着发一个arp请求,并且始终从eth4发出来(因为eth4的路由在前边),
这样client在应该该arp请求时,又会更新arp表。
所以我们在client上看到arp表中始终是eth4的mac。

解决方法:
1 echo 1 > /proc/sys/net/ipv4/conf/all/arp_filter 
2 ip route add 192.168.1.20/32 nexthop via 192.168.1.10 weight 1 nexthop via 192.168.1.11 weight 1

这里,添加的新路由的作用是以192.168.1.20作为目标地址进行反向路由检查时,依次循环选择eth4和eth5。
而arp_filter的作用是,只有通过了反向路由检查的包才会发出去。

推荐阅读