您的位置:首页 > 运维架构 > Linux

Linux双网卡设置IP属于同一网段的问题

2013-10-18 14:56 537 查看
写在前面:经常遇到客户将板卡上的两个网络设备的IP地址设置为同一网段的情况,我的解释是,不能设置为同一网段,但是我不知道这个说法到底是根据什么,查了很多资料也没找到明确的说法,但是下面这个实验能够说明一些现象的原因。初步做了一下实验:

  服务器为RHEL5 双网卡,eth0为234,eth1为233,我本地客户机为172。RHEL:ifconfig如下:[root@server1 ~]# ifconfigeth0      Link encap:Ethernet HWaddr 00:0C:29:A5:D5:A3           inet addr:60.232.83.233 Bcast:60.232.83.255 Mask:255.255.255.128略。。。。。。。 eth1      Link encap:Ethernet HWaddr 00:0C:29:A5:D5:AD           inet addr:60.232.83.234 Bcast:60.232.83.255 Mask:255.255.255.128          略。。。。。。。。。lo        Link encap:Local Loopback           inet addr:127.0.0.1 Mask:255.0.0.0          略。。。。。。。。。在客户机XP上ping两个地址,都是通的。C:\ >ping 60.232.83.233Pinging 60.232.83.233 with 32 bytes of data:Reply from 60.232.83.233: bytes=32 time=9ms TTL=64Reply from 60.232.83.233: bytes=32 time<1ms TTL=64 C:\ >ping 60.232.83.234Pinging 60.232.83.234 with 32 bytes of data:Reply from 60.232.83.234: bytes=32 time<1ms TTL=64Reply from 60.232.83.234: bytes=32 time<1ms TTL=64此时用arp –a在本地查看C:\ >arp -aInterface: 60.232.83.172 --- 0x20005Internet Address      Physical Address      Type60.232.83.129         00-04-96-1a-ca-60     dynamic60.232.83.233         00-0c-29-a5-d5-a3     dynamic60.232.83.234         00-0c-29-a5-d5-a3     dynamic 发现两个网卡的MAC地址一样,也就是eth0的mac地址。现在我们[root@server1 ~]# ifconfig eth1 down来禁止eth1,结果ping 233和234的地址都是通的。这里可以这样理解,arp –a看到本地解析到得两块网卡mac地址是一样的,局域网是通过2层寻址的不可能涉及到ip这样的3层协议,因此mac地址相同ping哪个都应该是通的,但是为什么本地会出现不同的IP相同的mac地址呢?在Linux上看到的明明是不同的MAC地址00:0C:29:A5:D5:A3,00:0C:29:A5:D5:AD 继续测试,现在把eth0禁止。[root@server1 ~]# ifconfig eth0 down [root@server1 ~]# ifconfigeth1      Link encap:Ethernet HWaddr 00:0C:29:A5:D5:AD           inet addr:60.232.83.234 Bcast:60.232.83.255 Mask:255.255.255.128          inet6 addr: fe80::20c:29ff:fea5:d5ad/64 Scope:Link          略。。。。。lo        Link encap:Local Loopback           inet addr:127.0.0.1 Mask:255.0.0.0          inet6 addr: ::1/128 Scope:Host          略。。。。 本地先arp –d清除一下缓存。然后再ping两块网卡的IP地址,依然可以ping通两个地址。Arp –a后发现:C:\>arp -aInterface: 60.232.83.172 --- 0x20005Internet Address      Physical Address      Type60.232.83.129         00-04-96-1a-ca-60     dynamic60.232.83.233         00-0c-29-a5-d5-ad     dynamic60.232.83.234         00-0c-29-a5-d5-ad     dynamic Mac地址成为另一块也就是eth1的mac地址。然后开启eth0,然后再ping测试一下。结果如下:C:\>arp -aInterface: 60.232.83.172 --- 0x20005Internet Address      Physical Address      Type60.232.83.129         00-04-96-1a-ca-60     dynamic60.232.83.233         00-0c-29-a5-d5-a3     dynamic60.232.83.234         00-0c-29-a5-d5-ad     dynamic 这才是一张正常的地址表。这时候再关闭eth0,结果就ping不通了,因为这时候缓存里的233的mac地址已经关闭。Arp –d之后就可以ping通了,此时eth0依然处于关闭状态,只是清楚了一下缓存表,这样再起ping233地址,可以ping通了,得出的结果是234的mac地址。C:\>arp -aInterface: 60.232.83.172 --- 0x20005Internet Address      Physical Address      Type60.232.83.129         00-04-96-1a-ca-60     dynamic60.232.83.233         00-0c-29-a5-d5-ad     dynamic60.232.83.234         00-0c-29-a5-d5-ad     dynamic此时开启eth0,现在本地缓存中的两个IP依然对应的还是234的mac地址。Arp –d清除一下,这次先ping 234再ping 233,结果两个都是233的既eth0的mac地址。 实验做到这里脑子有点乱,把这台Linux服务器换成Windows Server 2003,就没有这问题出现。  C:\Documents and Settings\Administrator>ipconfig /allWindows IP Configuration   Host Name . . . . . . . . . . . . : newxyz-yz5l2clv   Primary Dns Suffix . . . . . . . :   Node Type . . . . . . . . . . . . : Unknown   IP Routing Enabled. . . . . . . . : No   WINS Proxy Enabled. . . . . . . . : NoEthernet adapter 本地连接3:   Connection-specific DNS Suffix . :   Description . . . . . . . . . . . : VMware Accelerated AMD PCNet Adapter #2   Physical Address. . . . . . . . . : 00-0C-29-68-03-AF   DHCP Enabled. . . . . . . . . . . : No   IP Address. . . . . . . . . . . . : 60.232.83.251   Subnet Mask . . . . . . . . . . . : 255.255.255.128   Default Gateway . . . . . . . . . :Ethernet adapter 本地连接2:   Connection-specific DNS Suffix . :   Description . . . . . . . . . . . : VMware Accelerated AMD PCNet Adapter   Physical Address. . . . . . . . . : 00-0C-29-68-03-A5   DHCP Enabled. . . . . . . . . . . : No   IP Address. . . . . . . . . . . . : 60.232.83.250   Subnet Mask . . . . . . . . . . . : 255.255.255.128   Default Gateway . . . . . . . . . :本地ping两块网卡之后显示如下:C:\Documents and Settings\Administrator>arp -a Interface: 60.232.83.198 --- 0x10005Internet Address      Physical Address      Type60.232.83.129         00-04-96-1a-ca-60     dynamic60.232.83.250         00-0c-29-68-03-a5     dynamic60.232.83.251         00-0c-29-68-03-af     dynamic关闭任何一个网卡都是无法ping的。所以在Linux下网卡可能采用一些机制,比如说,首先,问题属于双网卡同一网段linux系统网卡自动路由的问题。 2、如果系统有两个独立网卡,并且这两个网卡的IP属于同一个子网,那么后面这个网卡的IP将自动路由到前面一个网卡上。 也就是说数据将自动路由到前面的那个网卡上。在不加任何设置的情况下,如果前面的网卡断线或者故障了(拔掉网线或者网卡指坏了),必须用命令down调后 面的网卡然后再up,这时后面的网卡才能启用。此时两个IP又同时路由到后面的网卡上,也就是说后面的网卡又具有两个IP地址。当然两块网卡设置成同一网段是不合理的。我的技术实在有限,这些都是实验后的猜测,就当抛砖引玉,希望有人能给解释一下。谢谢改天实验一下bond~~~~ ====================结论:这种现象正常。我们将其理解为在Linux系统下双网卡设置同样网段没有正确更新路由表的能力。因此会造成上述情况出现。所以最好的方法是避免这样操作。====================
linux双网卡服务器配置同一网段IP的ping问题 
问题是这样的,我有一台服务器,两个网口分别配置了同一网段的IP,然后将一个网口的网线拔掉,发现该网卡的IP仍然能够ping通,但是arp -a却发现该IP地址返回的确是另一块网卡的mac。百思不得其解。最后发现居然是linux内核的问题。解释如下。

arp_announce/arp_ignore sysctl

The arp_announce/arp_ignore sysctl on interfaces is available at the Linux official kernel since 2.6.4 and 2.4.26. The description about arp_announce/arp_ignore taken from kernel documentation is as follows: 
Linux 官方内核自2.6.4和2.4.26开始,interface上的arp_announce/arp_ignore系统调用就可用了。下面是内核文档中关于arp_announce/arp_ignore的描述:arp_announce - INTEGER
Define different restriction levels for announcing the local source IP address from IP packets in ARP requests sent on interface:

0 - (default) Use any local address, configured on any interface

1 - Try to avoid local addresses that are not in the target's subnet for this interface. This mode is useful when target hosts reachable via this interface require the source IP address in ARP requests to be part of their logical network configured on the receiving interface. When we generate the request we will check all our subnets that include the target IP and will preserve the source address if it is from such subnet. If there is no such subnet we select source address according to the rules for level 2.

2 - Always use the best local address for this target. In this mode we ignore the source address in the IP packet and try to select local address that we prefer for talks with the target host. Such local address is selected by looking for primary IP addresses on all our subnets on the outgoing interface that include the target IP address. If no suitable local address is found we select the first local address we have on the outgoing interface or on all other interfaces, with the hope we will receive reply for our request and even sometimes no matter the source IP address we announce. The max value from conf/{all,interface}/arp_announce is used. Increasing the restriction level gives more chance for receiving answer from the resolved target while decreasing the level announces more valid sender's information.

arp_ignore - INTEGER
Define different modes for sending replies in response to received ARP requests that resolve local target IP addresses:

0 - (default): reply for any local target IP address, configured on any interface

1 - reply only if the target IP address is local address configured on the incoming interface

2 - reply only if the target IP address is local address configured on the incoming interface and both with the sender's IP address are part from same subnet on this interface

  3 - do not reply for local addresses configured with scope host, only resolutions for global and link addresses are replied
4-7 - reserved
8 - do not reply for all local addresses

The max value from conf/{all,interface}/arp_ignore is used when ARP request is received on the {interface}

Disable ARP for VIP

To disable ARP for VIP at real servers, we just need to set arp_announce/arp_ignore sysctls at the interface connected to the VIP network. For example, real servers have eth0 connected to the VIP network with the VIP at interface lo, we will have the following commands.echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
Or, if /etc/sysctl.conf is used in the system, we have this config in /etc/sysctl.confnet.ipv4.conf.eth0.arp_ignore = 1
net.ipv4.conf.eth0.arp_announce = 2
Note that the arp_announce/arp_ignore sysctls must be setup correctly, before the VIP address is brought up at a logical interface at real servers.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: