双网卡bond0 自动化脚本

#!/bin/bash
#write: davis
# description: build dubble netcard bond0 on CentOS 6.4  mode 1
# args: $0 eth0 eth1 

#[ -z $2 ] && [ -z $1 ] &&  echo "please input two network name"&&exit 0 ||echo "args ok"
#### check args
#if [ -z $2 ]
#then
#echo -e "please input two network name \n for example:11.sh eth0 eth1"
#exit 1
#fi

patth="/etc/sysconfig/network-scripts"
### test netcard
#cd $patth && cp ifcfg-$1{,bak} && cp ifcfg-$2{,bak}
cd $patth
grep -Eq "(em1|em2)" ifcfg-*
if [ $? -eq 0 ]
then
i1=em1
i2=em2
else
i1=eth0
i2=eth1
fi
echo $i1  $i2

# 0 bak
cp ifcfg-${i1} /opt/ && cp ifcfg-${i2}  /opt/

##1 add bonding file
[ -f /etc/modprobe.d/bonding.conf ] && echo "bonding file exist" ||
cat > /etc/modprobe.d/bonding.conf  << EOF
alias  bond0 bonding
options bond0 miimon=100 mode=1
EOF

##2 bond0 configure file
aip=`cat $patth/ifcfg-$i1|grep IPADDR` 
agw=`cat $patth/ifcfg-$i1|grep GATEWAY` 
amk=`cat $patth/ifcfg-$i1|grep NETMASK` 

cat > /etc/sysconfig/network-scripts/ifcfg-bond0 << GOF
DEVICE=bond0
BOOTPROTO=static
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
GOF
echo -e "$aip\n$agw\n$amk" >> $patth/ifcfg-bond0


##3 eth0 eth1 configure file
for j in $i1 $i2
do
cat > $patth/ifcfg-$j << AOF
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
USERCTL=no
AOF
echo "DEVICE=$j" >> $patth/ifcfg-$j
done

#4 modprobe
modprobe  bonding

#5 network restart
/etc/init.d/network restart

#6 check bond0

num=`cat /proc/net/bonding/bond0 |grep "Slave Interface"|wc -l`
if [ $num -eq 2 ]
then
echo "bond0 success!"
else 
echo $num
echo "bond0 failed"
fi

##7 reboot
#reboot

bond mode 请自行查阅。 我用的是mode=1 active-backup  mode=0 LB rr 

brctl  addif  br100 eth0
brctl  delif  br0 eth0


bond之后,可以选择reboot或/etc/init.d/network restart 

如果涉及到宿主机,必须要重启,如果重启network 有可能由于异常问题,如arp刷新、广播风暴等导致宿主机网络中断,仍需要重启。亲身体会呀。还是以系统稳定为大局。bond重启吧。

不涉及到宿主机的,也尽量重启一下。 稳定是我们的根本。


转载于:https://my.oschina.net/davisqi/blog/206177

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
双网卡绑定(bonding)是一种在Linux中将多个物理网卡绑定成一个逻辑接口的技术。通过绑定,可以实现负载均衡和冗余故障转移的功能。 在Linux中,双网卡绑定可以通过bond0和bond1两个接口来实现。其中,bond0是第一个绑定的接口,而bond1是第二个绑定的接口。 要配置双网卡绑定,你需要先安装并加载bonding内核模块,然后进行相关的配置。以下是一个简单的配置示例: 1. 安装并加载bonding内核模块: ``` modprobe bonding ``` 2. 创建bond0和bond1的配置文件(例如:/etc/sysconfig/network-scripts/ifcfg-bond0和/etc/sysconfig/network-scripts/ifcfg-bond1),并进行相应的配置。示例配置如下: ifcfg-bond0: ``` DEVICE=bond0 TYPE=Bond BOOTPROTO=none IPADDR=<IP地址> NETMASK=<子网掩码> GATEWAY=<默认网关> ONBOOT=yes BONDING_OPTS="mode=0 miimon=100" ``` ifcfg-bond1: ``` DEVICE=bond1 TYPE=Bond BOOTPROTO=none IPADDR=<IP地址> NETMASK=<子网掩码> GATEWAY=<默认网关> ONBOOT=yes BONDING_OPTS="mode=0 miimon=100" ``` 注意:你需要根据实际情况修改IP地址、子网掩码、默认网关等参数。 3. 配置每个物理网卡的配置文件(例如:/etc/sysconfig/network-scripts/ifcfg-eth0和/etc/sysconfig/network-scripts/ifcfg-eth1)。示例配置如下: ifcfg-eth0: ``` DEVICE=eth0 TYPE=Ethernet BOOTPROTO=none ONBOOT=yes MASTER=bond0 SLAVE=yes ``` ifcfg-eth1: ``` DEVICE=eth1 TYPE=Ethernet BOOTPROTO=none ONBOOT=yes MASTER=bond1 SLAVE=yes ``` 4. 重启网络服务,使配置生效: ``` systemctl restart network ``` 以上是一个简单的双网卡绑定的配置示例,你可以根据自己的需求进行适当的调整和完善。请确保在进行任何网络配置更改之前,备份相关配置文件,并确保你对网络环境具有足够的了解和权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值