Linux雙網卡設定IP屬於同一網段的問題

來源:互聯網
上載者:User

今天在CU看到一個問題:http://linux.chinaunix.net/bbs/thread-1021489-1-1.html
關於雙網卡設定同一網段IP然後串連交換器的時候出現的奇怪現象。當時沒有怎麼思考、以為是產生樹的緣故,不過後來一想不太對。
初步做了一下實驗:

伺服器為RHEL5 雙網卡,eth0為234,eth1為233,我本地客戶機為172。
RHEL: ifconfig如下:
[root@server1 ~]# ifconfig
eth0      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.233
Pinging 60.232.83.233 with 32 bytes of data:
Reply from 60.232.83.233: bytes=32 time=9ms TTL=64
Reply from 60.232.83.233: bytes=32 time<1ms TTL=64

C:/ >ping 60.232.83.234
Pinging 60.232.83.234 with 32 bytes of data:
Reply from 60.232.83.234: bytes=32 time<1ms TTL=64
Reply from 60.232.83.234: bytes=32 time<1ms TTL=64
此時用arp –a在本地查看
C:/ >arp -a
Interface: 60.232.83.172 --- 0x20005
Internet Address      Physical Address      Type
60.232.83.129         00-04-96-1a-ca-60     dynamic
60.232.83.233         00-0c-29-a5-d5-a3     dynamic
60.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 ~]# ifconfig
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
          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 -a
Interface: 60.232.83.172 --- 0x20005
Internet Address      Physical Address      Type
60.232.83.129         00-04-96-1a-ca-60     dynamic
60.232.83.233         00-0c-29-a5-d5-ad     dynamic
60.232.83.234         00-0c-29-a5-d5-ad     dynamic

Mac地址成為另一塊也就是eth1的mac地址。
然後開啟eth0,然後再ping測試一下。結果如下:
C:/>arp -a
Interface: 60.232.83.172 --- 0x20005
Internet Address      Physical Address      Type
60.232.83.129         00-04-96-1a-ca-60     dynamic
60.232.83.233         00-0c-29-a5-d5-a3     dynamic
60.232.83.234         00-0c-29-a5-d5-ad     dynamic

這才是一張正常的地址表。
這時候再關閉eth0,結果就ping不通了,因為這時候緩衝裡的233的mac地址已經關閉。
Arp –d之後就可以ping通了,此時eth0依然處於關閉狀態,只是清楚了一下緩衝表,這樣再起ping233地址,可以ping通了,得出的結果是234的mac地址。
C:/>arp -a
Interface: 60.232.83.172 --- 0x20005
Internet Address      Physical Address      Type
60.232.83.129         00-04-96-1a-ca-60     dynamic
60.232.83.233         00-0c-29-a5-d5-ad     dynamic
60.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 /all
Windows IP Configuration
   Host Name . . . . . . . . . . . . : newxyz-yz5l2clv
   Primary Dns Suffix . . . . . . . :
   Node Type . . . . . . . . . . . . : Unknown
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No
Ethernet 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 --- 0x10005
Internet Address      Physical Address      Type
60.232.83.129         00-04-96-1a-ca-60     dynamic
60.232.83.250         00-0c-29-68-03-a5     dynamic
60.232.83.251         00-0c-29-68-03-af     dynamic
關閉任何一個網卡都是無法ping的。
所以在Linux下網卡可能採用一些機制,比如說,首先,問題屬於雙網卡同一網段linux系統網卡自動路由的問題。 
2、如果系統有兩個獨立網卡,並且這兩個網卡的IP屬於同一個子網,那麼後面這個網卡的IP將自動路由到前面一個網卡上。 
也就是說資料將自動路由到前面的那個網卡上。在不加任何設定的情況下,如果前面的網卡斷線或者故障了(拔掉網線或者網卡指壞了),必須用命令down調後 面的網卡然後再up,這時後面的網卡才能啟用。此時兩個IP又同時路由到後面的網卡上,也就是說後面的網卡又具有兩個IP地址。
當然兩塊網卡設定成同一網段是不合理的。
我的技術實在有限,這些都是實驗後的猜測,就當拋磚引玉,希望有人能給解釋一下。謝謝
改天實驗一下bond~~~~

====================
結論:

這種現象正常。我們將其理解為在Linux系統下雙網卡設定同樣網段沒有正確更新路由表的能力。因此會造成上述情況出現。
所以最好的方法是避免這樣操作。

====================

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.