Linux双网卡绑定和解除绑定的实现
双网卡绑定实现就是使用两块网卡虚拟成为一块网卡,这个聚合起来的设备看起来是一个单独的以太网接口设备,通俗点讲就是两块网卡具有相同的IP地址而并行链接聚合成一个逻辑链路工作。根据交换机可支持的功能不同,最常见的是设定为主备方式的双网卡绑定。linux有七种网卡绑定模式:0. round robin,1.active-backup,2.load balancing (xor),3.fault-tolerance (broadcast),4.lacp,5.transmit load balancing,6.adaptive load balancing。
一、操作步骤
这里以绑定两个网卡为示例描述。配置文件都在/etc/sysconfig/network-scripts/目录下。
1、编辑新的ifcfg-bond0文件
# vim /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
IPADDR=192.168.216.252
NETMASK=255.255.255.0
# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
3、修改/etc/modprobe.d/dist.conf文件
# vi /etc/modprobe.d/dist.conf
添加如下内容:
alias bond0 bonding
options bond0 miimon=100 mode=1
注:由于ifcfg-bond0中已经写了这条,这条在这里可添加也可以不添加)
说明:
二、卸载bond0设备
如需删除双网卡绑定系统,可执行以下操作:
#rm -f /etc/sysconfig/network-scripts/ifcfg-bond0
#vi /etc/etc/modprobe.d/dist.conf
删除以下两行后,保存退出
echo "Usage: $0 <bond*> <eth*> <eth*> <ipaddress> <netmask> <gateway>"
echo "eg: $0 bond0 eth0 eth1 10.0.0.1 255.255.255.0 10.0.0.254"
mv -f /tmp/ifcfg-$1 /etc/sysconfig/network-scripts/
BOOTPROTO=none" >/tmp/ifcfg-$2
mv -f /tmp/ifcfg-$2 /etc/sysconfig/network-scripts/
BOOTPROTO=none" >/tmp/ifcfg-$3
mv -f /tmp/ifcfg-$3 /etc/sysconfig/network-scripts/
#sed /GATEWAY/d /etc/sysconfig/network >/tmp/network
#echo "GATEWAY=\"$6\"">>/tmp/network
#mv -f /tmp/network /etc/sysconfig/
#modify modules.cof/modprobe.cof
echo "Please Select Your Bond Mode:(balance-rr/active-backup)or(0/1)?"
if [ -f /etc/modprobe.conf ]; then
if [ "$(grep "alias.*bonding" $TEMPFILE1)" != "" ]; then
bondcount=$(grep "alias.*bonding" $TEMPFILE1 | wc -l)
elif [ "$(grep "install.*bond.*" $TEMPFILE1)" != "" ]; then
bondcount=$(grep "install.*bond.*" $TEMPFILE1 | wc -l)
if [ "$bondcount" -ge 1 ]; then
echo "install $1 /sbin/modprobe --ignore-install bonding -o $1 miimon=100 mode=$MODE" >> $TEMPFILE2
echo "alias $1 bonding" >> $TEMPFILE2
echo "options $1 miimon=100 mode=$MODE" >> $TEMPFILE2
echo "System will restart network continue(y/n)?"
if [ "$bb" = "y" ] || [ "$bb" = "yes" ] || [ "$bb" = "Y" ];then
for tempmod in $(lsmod | grep -i bond | awk '{print $1}')