現在的機器常裝了多張網卡,但只要其中一張故障,整條網路就斷了。在 Linux 裡把實體網卡合成虛擬網卡(bonding),就能同時取得容錯與流量分散的效果。

以下以 eth0 與 eth1 合併為 bond0 為例。前提條件是晶片組型號一致,且網卡具備獨立 BIOS 晶片。
vi /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
BOOTPROTO=static
IPADDR=10.0.11.10
NETMASK=255.255.252.0
GATWAY=10.0.11.1
ONBOOT=yes
USERCTL=no
TYPE=Ethernet
ifcfg-eth0 與 ifcfg-eth1 都不再設定 IP、閘道與遮罩,只標明自己從屬於 bond0:
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
USERCTL=no
MASTER=bond0
SLAVE=yes
TYPE=Ethernet
eth1 內容相同,只把 DEVICE 換成 eth1。
虛擬網卡由核心模組實作,需在 /etc/modules.conf 加入:
alias bond0 bonding
options bond0 miimon=100 mode=0
miimon 是鏈路偵測間隔(毫秒),100 代表每 0.1 秒檢查一次,某條線路不通就切到另一條。mode 共有 0 到 3 四種,常用的是 0 與 1:mode=0 為 round-robin 負載均衡,兩張卡都 active;mode=1 為 active-backup 容錯,僅一張工作、另一張待命,可寫成 options bond0 miimon=100 mode=1 primary=eth0。
要注意 bonding 只能偵測主機到交換器這一段。若交換器對外線路中斷而交換器本身正常,bonding 仍會認為鏈路無礙而繼續使用。
在 /etc/rc.local 補上:
modprobe bonding miimon=100 mode=0
重啟後看到 bond0、eth0、eth1 依序啟動成功即完成。此後只要還有一張網卡可用,連線就不會中斷。