1.原理:
现在一般的企业都会使用双网卡接入,这样既能添加网络带宽,同时 又能做相应的冗余,可以说是好处多多。而一般企业都会使用linux操作系统下自带的网卡绑定模式,当然现在网卡产商也会出一些针对windows操作系 统网卡管理软件来做网卡绑定(windows2012支持,其余版本需要第三方厂商支持)。
linux有七种网卡绑定模式:
round robin
active-backup
load balancing (xor)
fault-tolerance (broadcast)
lacp
transmit load balancing
adaptive load balancing
2.配置
[root@slave2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0ONBOOT=yesBOOTPROTO=dhcp[root@slave2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1DEVICE=eth1ONBOOT=yesBOOTPROTO=dhcp
[root@slave2 ~]# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-bond0 #生成bond0配置文件
vim /etc/modprobe.d/dist.conf #表示系统在启动时加载bonding模块,对外虚拟网络接口设备为 bond0;miimon=100表示系统每100msalias bond0 bondingoptions bond0 miimon=100 mode=0
[root@slave2 ~]# vim /etc/rc.local #编辑该开机脚本,将eth0和eth1网卡进行绑定ifenslave bond0 eth0 eth1
[root@slave2 ~]# /etc/init.d/network restart #重启网络服务
3.网卡绑定脚本
#!/bin/bash cd /etc/sysconfig/network-scripts cp ifcfg-eth0 /tmp/ifcfg-eth0.bak cp ifcfg-eth1 /tmp/ifcfg-eth1.bak #cp ifcfg-eth0 ifcfg-bond0 #vim eth0 cat ifcfg-eth0 | grep -i hwaddr > ifcfg-eth0.txt #在HWADDR匹配行前插入SLAVE=yes,以此类推,此处也可以用echo输入来完成 sed -i '/HWADDR/ i SLAVE=yes' ifcfg-eth0.txt sed -i '/SLAVE/ i MASTER=bond0' ifcfg-eth0.txt sed -i '/MASTER/ i BOOTPROTO=none' ifcfg-eth0.txt sed -i '/BOOTPROTO/ i DEVICE=eth0' ifcfg-eth0.txt sed -i '/HWADDR/ a IPV6INIT=no' ifcfg-eth0.txt sed -i '/IPV6INIT/ a ONBOOT=yes' ifcfg-eth0.txt sed -i '/ONBOOT/ a TYPE=Ethernet' ifcfg-eth0.txt sed -i '/TYPE/ a PEERDNS=yes' ifcfg-eth0.txt sed -i '/PEERDNS/ a USERCTL=no' ifcfg-eth0.txt cat ifcfg-eth0.txt > ifcfg-eth0 #vim eth1 #在匹配DEVICE行,将(y指所有)0替换成1,即eth0替换为eth1 sed -i '/DEVICE/y/0/1/' ifcfg-eth0.txt #变量linehwaddr为取出eth1的MAC地址``为通配符 linehwaddr=`cat /etc/sysconfig/network-scripts/ifcfg-eth1 | grep -i hwaddr` #将eth0的MAC的地址替换为eth1的MAC地址 sed -i '/HWADDR/c\'"$linehwaddr"'' ifcfg-eth0.txt cat ifcfg-eth0.txt > ifcfg-eth1 #vim bond0 #脚本通用,只需改下列后五个网段,网址,网关,广播地址,子网掩码即可,按实际情况处理echo "DEVICE=bond0" > ifcfg-bond0 echo "BOOTPROTO=none" >> ifcfg-bond0 echo "IPV6INIT=no" >> ifcfg-bond0 echo "ONBOOT=yes" >> ifcfg-bond0 echo "TYPE=Ethernet" >> ifcfg-bond0 echo "PEERDNS=yes" >> ifcfg-bond0 echo "USERCTL=no" >> ifcfg-bond0 echo "NETMASK=255.255.255.0" >> ifcfg-bond0 echo "IPADDR=192.168.1.254" >> ifcfg-bond0 echo "GATEWAY=192.168.1.1" >> ifcfg-bond0 echo "BROADCAST=192.168.1.255" >> ifcfg-bond0 echo "NETWORK=192.168.1.0" >> ifcfg-bond0 #vim /etc/modprobe.d/dist.conf#追加 echo "alias bond0 bonding" >> /etc/modprobe.d/dist.confecho "options bond0 miimon=100 mode=0" >> /etc/modprobe.d/dist.conf #vim /etc/rc.d/rc.local echo "ifenslave bond0 eth0 eth1" >> /etc/rc.d/rc.local rm -rf /etc/sysconfig/network-scripts/ifcfg-eth0.txt #重启网络 ifconfig bond0 upservice network restart echo "double network cards bond complete!"