centos下多网卡做bond脚本

多网卡或者单网卡形式下的网卡bonding

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#! /bin/sh
#获取当前网卡数
ethnum=`lspci | grep Ethernet | wc -l`
echo $ethnum
#如果网卡数小于等于1则什么都不做
if [ $ethnum -le 1 ]
then
echo "do nothing!"
fi
#如果网卡数大于等于2则
if [ $ethnum -ge 2 ]
then


#------- 添加一个bond0的网卡
echo "DEVICE=bond0" > /etc/sysconfig/network-scripts/ifcfg-bond0
echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-bond0
echo "IPADDR=192.168.3.104" >> /etc/sysconfig/network-scripts/ifcfg-bond0
echo "NETMASK=255.255.0.0" >> /etc/sysconfig/network-scripts/ifcfg-bond0
echo "GATEWAY=192.168.1.1" >> /etc/sysconfig/network-scripts/ifcfg-bond0
echo "BOOTPROTO=static" >> /etc/sysconfig/network-scripts/ifcfg-bond0
echo "USERCTL=no" >> /etc/sysconfig/network-scripts/ifcfg-bond0
#--------
echo "ifenslave bond0" >> /etc/rc.local
#--------写其他网卡的配置文件
for i in $(seq $ethnum);
do
num=`expr $i - 1`
echo "BOOTPROTO=none" > /etc/sysconfig/network-scripts/ifcfg-eth$num
echo "DEVICE=eth$num" >> /etc/sysconfig/network-scripts/ifcfg-eth$num
echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth$num
echo "MASTER=bond0" >> /etc/sysconfig/network-scripts/ifcfg-eth$num
echo "USERCTL=no" >> /etc/sysconfig/network-scripts/ifcfg-eth$num
echo "SLAVE=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth$num
sed -i 's/ifenslave.*/& eth'"$num"'/g' /etc/rc.local
done;
echo "alias bond0 bonding" > /etc/modprobe.d/modprobe.conf
echo "options bond0 miimon=100 mode=balance-rr" >> /etc/modprobe.d/modprobe.conf
modprobe bonding
/etc/init.d/network restart
fi

直接配置方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[root@lab101 ~]# cat /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
NAME=bond0
TYPE=Bond
BONDING_MASTER=yes
IPADDR=192.168.0.101
PREFIX=24
GATEWAY=192.168.0.1
ONBOOT=yes
BOOTPROTO=none
BONDING_OPTS="mode=1 miimon=100"
NM_CONTROLLED="no"


[root@lab101 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
DEVICE=ens33
NAME=bond0-slave
TYPE=Ethernet
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
NM_CONTROLLED="no"


[root@lab101 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens37
DEVICE=ens37
NAME=bond0-slave
TYPE=Ethernet
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
NM_CONTROLLED="no"

还原

1
2
3
4
5
6
7
8
9
10
DEVICE=ens33
NAME=ens33
TYPE=Ethernet
BOOTPROTO=static
ONBOOT=yes
NM_CONTROLLED="no"
IPADDR=192.168.0.101
PREFIX=24
GATEWAY=192.168.0.1
ONBOOT=yes

更新历史

why when
创建 2013年08月06日
更新 2019年12月9日