CentOS 5.x 6.x 网卡绑定(Bonding)脚本

工作中经常要为客户调整网卡绑定,为此制作了此脚本(比较粗糙,请见谅)。使用时请查看脚本帮助

bond.sh

#!/bin/bash

usage()
{
	cat << EOF
	create by [email protected]
	usage: $0 options

	OPTIONS:
	    -h 		Show this message
	    -i		Bonding IP,NETMASK,GATEWAY(optional)
	    -q		Show Bonding Parameter Info
	    -d		Define NIC Name: eth1,eth2 or em1,em2(default primary is first,only mode 1)
	    -m		Bonding Mode:0,1,2,3,4,5,6
	    
EOF
}

show()
{
cat << EOF
	Do not place parameters for the bonding kernel module in the /etc/modprobe.conf file. Instead, specify them as a space-separated list in the BONDING_OPTS="<bonding parameters>" directive in the ifcfg-bond<N> interface file.
	The only exception is the debug parameter, which cannot be used on a per-device basis, and which should therefore be specified in /etc/modprobe.conf as follows:
	options bonding debug=1
	For further instructions and advice on configuring the bonding module, as well as to view the list of bonding parameters

	Parameters for the bonding kernel module must be specified as a space-separated list in the BONDING_OPTS="bonding parameters" directive in the ifcfg-bondN interface file. Do not specify options for the bonding device in /etc/modprobe.d/bonding.conf, or in the deprecated /etc/modprobe.conf file. For further instructions and advice on configuring the bonding module and to view the list of bonding parameters

	mode=<value> 
	Allows you to specify the bonding policy. The <value> can be one of:
	balance-rr or 0 — Sets a round-robin policy for fault tolerance and load balancing. Transmissions are received and sent out sequentially on each bonded slave interface beginning with the first one available.
	active-backup or 1 — Sets an active-backup policy for fault tolerance. Transmissions are received and sent out via the first available bonded slave interface. Another bonded slave interface is only used if the active bonded slave interface fails.
	balance-xor or 2 — Sets an XOR (exclusive-or) policy for fault tolerance and load balancing. Using this method, the interface matches up the incoming request‘s MAC address with the MAC address for one of the slave NICs. Once this link is established, transmissions are sent out sequentially beginning with the first available interface.
	broadcast or 3 — Sets a broadcast policy for fault tolerance. All transmissions are sent on all slave interfaces.
	802.3ad or 4 — Sets an IEEE 802.3ad dynamic link aggregation policy. Creates aggregation groups that share the same speed and duplex settings. Transmits and receives on all slaves in the active aggregator. Requires a switch that is 802.3ad compliant.
	balance-tlb or 5 — Sets a Transmit Load Balancing (TLB) policy for fault tolerance and load balancing. The outgoing traffic is distributed according to the current load on each slave interface. Incoming traffic is received by the current slave. If the receiving slave fails, another slave takes over the MAC address of the failed slave.
	balance-alb or 6 — Sets an Active Load Balancing (ALB) policy for fault tolerance and load balancing. Includes transmit and receive load balancing for IPV4 traffic. Receive load balancing is achieved through ARP negotiation.

	MASTER=bond-interface
	where bond-interface is the channel bonding interface to which the Ethernet interface is linked.
	This directive is used in conjunction with the SLAVE directive.

	NM_CONTROLLED=answer
	where answer is one of the following:
	yes — NetworkManager is permitted to configure this device. This is the default behavior and can be omitted.
	no — NetworkManager is not permitted to configure this device.

	SLAVE=answer
	where answer is one of the following:
	yes — This device is controlled by the channel bonding interface specified in the MASTER directive.
	no — This device is not controlled by the channel bonding interface specified in the MASTER directive.
	This directive is used in conjunction with the MASTER directive.

	USERCTL=answer
	where answer is one of the following:
	yes — Non-root users are allowed to control this device.
	no — Non-root users are not allowed to control this device.

	primary=<interface_name> 
	Specifies the interface name, such as eth0, of the primary device. The primary device is the first of the bonding interfaces to be used and is not abandoned unless it fails. This setting is particularly useful when one NIC in the bonding interface is faster and, therefore, able to handle a bigger load.
	This setting is only valid when the bonding interface is in active-backup mode. Refer to  /usr/share/doc/kernel-doc-<kernel-version>/Documentation/networking/bonding.txt for more information.

	primary_reselect=<value> 
	Specifies the reselection policy for the primary slave. This affects how the primary slave is chosen to become the active slave when failure of the active slave or recovery of the primary slave occurs. This parameter is designed to prevent flip-flopping between the primary slave and other slaves. Possible values are:
	always or 0 (default) — The primary slave becomes the active slave whenever it comes back up.
	better or 1 — The primary slave becomes the active slave when it comes back up, if the speed and duplex of the primary slave is better than the speed and duplex of the current active slave.
	failure or 2 — The primary slave becomes the active slave only if the current active slave fails and the primary slave is up.
	The primary_reselect setting is ignored in two cases:
	If no slaves are active, the first slave to recover is made the active slave.
	When initially enslaved, the primary slave is always made the active slave.
	Changing the primary_reselect policy via sysfs will cause an immediate selection of the best active slave according to the new policy. This may or may not result in a change of the active slave, depending upon the circumstances

	miimon=<time_in_milliseconds> 
	Specifies (in milliseconds) how often MII link monitoring occurs. This is useful if high availability is required because MII is used to verify that the NIC is active.
EOF
}
set()
{
IP1=`echo $IP|cut -d "," -f1`
MASK=`echo $IP|cut -d "," -f2`
GATE=`echo $IP|cut -d "," -f3`
if [ -z $GATE ] ; then
	GATE=""
else
    GATE="GATEWAY=`echo $IP|cut -d "," -f3`"
fi
IF1=`echo $IF|cut -d "," -f1`
IF2=`echo $IF|cut -d "," -f2`
IF3=`echo $IF|cut -d "," -f3`
IF4=`echo $IF|cut -d "," -f4`

if [ $MODE = 1 ] ;then
	primary="primary=$IF1"
fi
echo "
DEVICE=bond0
ONBOOT=yes
BOOTPROTO=none
USERCTL=no
IPADDR=$IP1
NETMASK=$MASK
$GATE
BONDING_OPTS="miimon=100 mode=$MODE $primary"
">/etc/sysconfig/network-scripts/ifcfg-bond0

sed -i "s/BOOTPROTO.*$/BOOTPROTO=\"none\"/" /etc/sysconfig/network-scripts/ifcfg-$IF1
sed -i "s/BOOTPROTO.*$/BOOTPROTO=\"none\"/" /etc/sysconfig/network-scripts/ifcfg-$IF2
sed -i "s/ONBOOT.*$/ONBOOT=\"yes\"/" /etc/sysconfig/network-scripts/ifcfg-$IF1
sed -i "s/ONBOOT.*$/ONBOOT=\"yes\"/" /etc/sysconfig/network-scripts/ifcfg-$IF2

echo "
MASTER=bond0
SLAVE=yes
USERCTL=no
">>/etc/sysconfig/network-scripts/ifcfg-$IF1
echo "
MASTER=bond0
SLAVE=yes
USERCTL=no
">>/etc/sysconfig/network-scripts/ifcfg-$IF2

if [ -n $IF3 ] ;then
sed -i "s/BOOTPROTO.*$/BOOTPROTO=\"none\"/" /etc/sysconfig/network-scripts/ifcfg-$IF3
sed -i "s/ONBOOT.*$/ONBOOT=\"yes\"/" /etc/sysconfig/network-scripts/ifcfg-$IF3
echo "
MASTER=bond0
SLAVE=yes
USERCTL=no
">>/etc/sysconfig/network-scripts/ifcfg-$IF3
fi
if [ -n $IF4 ] ;then
sed -i "s/BOOTPROTO.*$/BOOTPROTO=\"none\"/" /etc/sysconfig/network-scripts/ifcfg-$IF4
sed -i "s/ONBOOT.*$/ONBOOT=\"yes\"/" /etc/sysconfig/network-scripts/ifcfg-$IF4
echo "
MASTER=bond0
SLAVE=yes
USERCTL=no
">>/etc/sysconfig/network-scripts/ifcfg-$IF4
fi

service network restart
cat /proc/net/bonding/bond0
}

if [ $# = 0 ] ;then
	usage
	exit
fi

while getopts hqi:d:m: OPTION
do
     case $OPTION in
         h)
	     usage
	     exit 1
	     ;;
	 q)
	     show
	     exit 1
	     ;;
	 i)
	     IP=$OPTARG
	     ;;
	 d)
	     IF=$OPTARG
	     ;;
	 m)
	     MODE=$OPTARG
	     ;;
         ?)
             usage
             exit 1
             ;;
         *)
             usage
             exit 1
             ;;
     esac
done
#shift $((OPTIND-1))
#echo "opts [email protected]"

if [ -z $IP ] ;then
	echo "
	Please Input IP,NETMASK,GATEWAY
	Example:bond.sh -i 192.168.1.2,255.255.255.0,192.168.1.1 -d eth0,eth1,eth3,eth4
"
	exit 1
fi
if [ -z $IF ] ;then
	echo "
	Please Input NIC Name
	Example:bond.sh -d eth0,eth1,eth2,eth3 or em1,em2,em3,em4
"
	exit 1
fi
if [ -z $MODE ] ;then
	MODE=1
fi
set

echo "cat /proc/net/bonding/bond0 show bond status"

CentOS 7 可以通过三种方式来实现

  1. nmtui #在界面里配置
  2. nmcli #请通过man nmcli查看帮助
  3. 通过修改网卡配置文件实现(理论上此脚本也可以,未做测试)
时间: 2024-08-26 19:53:57

CentOS 5.x 6.x 网卡绑定(Bonding)脚本的相关文章

centos 6.4系统双网卡绑定配置详解

Linux双网卡绑定实现就是使用两块网卡虚拟成为一块网卡(需要交换机支持),这个聚合起来的设备看起来是一个单独的以太网接口设备,通俗点讲就是两块网卡具有相同的IP地址而并行链接聚合成一个逻辑链路工作.本文详细描述下centos 6.4系统双网卡绑定操作步骤操作前需要确定NetworkManager 服务是否已经停止,否则容易报错.#service NetworkManager status显示NetworkManager 已停即可 具体绑定操作:1.在/etc/sysconfig/network

实现多网卡绑定bonding

centos6,7实现多网卡绑定bonding 介绍 Bonding 1.将多块网卡绑定同一IP地址对外提供服务; 2.可以实现高可用或者负载均衡; 3.直接给两块网卡设置同一IP地址是不可以的; 4.通过bonding,虚拟一块网卡对外提供连接,物理网卡的被修改为相同的MAC地址 工作模式 1.Mode 0---轮转(Round-robin)策略 从头到尾顺序的在每一个slave接口上面发送数据包: 本模式提供负载均衡和容错的能力 2.Mode 1---活动-备份(主备)策略 只有一个slav

Gnu/Linux网卡绑定bonding

系统:    CentOS或RHEL5系列系统配置文件:/etc/modprobe.conf----------------------------------------系统:    CentOS或RHEL6系列系统配置文件:/etc/modprobe.d/dist.conf Linux bonding提供将多个网络接口设备捆绑为单个网络接口设置来使用,用于网络负载均衡及网络冗余. 网卡绑定主要有0~6七种模式,常用的有3种: 0:负载均衡,两个网卡都工作,当一个出现问题后,另一个还继续工作,

linux多网卡绑定bonding

Linux  多网卡绑定概述 本文 os:6.4  这里测试是四块网卡绑定 1 块 bond 我们在这介绍的Linux 双 网卡绑定实现就是使用两块网卡虚拟成为一块网卡,这个聚合起来的设备看起来是一个单独的以太网接口设备,通俗点讲就是两块网卡具有相同的IP地址而并行链 接聚合成一个逻辑链路工作.其实这项技术在Sun和Cisco中早已存在,被称为Trunking和Etherchannel 技术,在Linux的2.4.x的内核中也采用这这种技术,被称为bonding.bonding技术的最早应用是在

CentOS 6.5 多块网卡绑定 bond0、bond1

linux常用的两种网卡绑定模式 mode=0:平衡负载模式,有自动备援,但需要"Switch"支援及设定. mode=1:自动备援模式,其中一条线若断线,其他线路将会自动备援. 需要说明的是如果想做成mode 0的负载均衡,仅仅设置这里options bond0 miimon=100 mode=0是不够的,与网卡相连的交换机必须做特殊配置(这两个端口应该采取聚合方式),因为做bonding的这两块网卡是使用同一个MAC地址.从原理分析一下(bond运行在mode 0下): mode

多网卡绑定bonding

bonding工作模式 mode 0 :论转策略,从头到尾顺序的在每个slave接口上面发送数据包,本模式提供了负载均衡和容错能力. mode 1 :备份(主备)策略,只有一个端口被激活,只有在被激活的端口失败时备用的端口才开始工作,为了避免交换机发送混乱此时绑定的MAC地址只有一个外部端口可见. mode 3 :广播策略,使用的接口上传送的报文都一样,提供了容错能力. 当然还有其他比较少用的模式,这里就不一一叙述了,需要注意的是模式0,3需要交换机的支持和设置,下面我们用2台虚拟机模拟bond

红帽7.2双网卡绑定bonding

添加2块网卡 vim /etc/sysconfig/network-scripts/ifcfg-bond0 ##新建 添加以下内容 DEVICE=bond0 BOOTPROTO="static" ONBOOT="yes" TYPE=bond IPADDR=192.168.1.12   ###IP地址自己配 NETMASK=255.255.255.0 GATEWAY=192.168.1.1    ####网关自己配 BONDING_OPTS="miimon=

Linux下双网卡绑定(bonding技术)

http://www.cnblogs.com/qmfsun/p/3810905.html Linux网卡绑定探析 2013-08-20 15:39:31 现在很多服务器都自带双千兆网口,利用网卡绑定既能增加网络带宽,同时又能做相应的冗余,目前应用于很多的场景.linux操作系统下自带的网卡绑定模式,Linux bonding驱动提供了一个把多个网络接口设备捆绑为单个网络接口设置来使用,用于网络负载均衡及网络冗余.当然现在网卡产商也会出一些针对windows操作系统网卡管理软件来做网卡绑定(win

linux设置网卡别名和网卡绑定

 网卡别名 | CentOS 6 bonding | CentOS 7 nmcli网络组 网卡别名: 1.命令实现: 皆为临时生效 ifconfig: ]# ifconfig eth0:0 202.204.235.5.24 ]# ifconfig eth0:0 down    撤销该别名 ip: ]# ip addr add 202.204.235.10/24 dev eth0 该地址对ifconfig命令不可见 ]# ip addr add 202.204.235.20/24 dev eth0