Linux implementation of dual NIC binding and automatic binding shell script

Source: Internet
Author: User
Tags time interval

One, hand-bound network card


Dual network card binding implementation is to use two network card virtual to become a network card, the aggregation of equipment appears to be a separate Ethernet interface equipment, popular point is that two network cards have the same IP address and parallel link aggregation into a logical link work. Depending on the functionality that the switch can support, the most common is the dual NIC bindings that are set as the primary standby mode.
First, new Ifcfg-bond0 file

Under the/etc/sysconfig/network-scripts vim ifcfg-bond0, the contents are as follows


Device=bond0
Bootproto=none
Onboot=yes
Type=ethernet
Ipaddr=ip
Netmask= Mask

Ii. edit Ifcfg-eth0 and ifcfg-eth1 documents separately
Eth0

Device=eth0
Userctl=no
Onboot=yes
Master=bond0
Slave=yes
Bootproto=none
Eth1


Device=eth1
Userctl=no
Onboot=yes
Master=bond0
Slave=yes
Bootproto=none

Third, modify the/etc/modprobe.conf file, add the following two lines

Alias Bond0 Bonding
Options bond0 miimon=100 mode=1 [max_bonds=2]
Ps:
A, Miimon is the link monitoring time interval unit is the millisecond, miimon=100 meaning is every 100ms, monitoring a link connection state, if one line does not pass to another line;
B, mode=0 means load balancing, two network cards are working, need switch to support
Mode=1 means redundancy, the NIC has only one job, one problem enables the other
Mode=6 means load balancing, both cards are working and no switches are required to support
There is a problem with the current 0 mode test, if there is no relevant configuration on the switch network will be very serious loss of packets, and in 0 mode he is just a rotating mode instead of the so-called will increase the bandwidth of the network, in turn send packets so that the speed of the network is reduced (because to make a choice to go that network card), Now it is commonly used in 1 mode redundancy mode is safe and reliable, speed is also fast.
C, bonding can only provide link monitoring, that is, the link from the host to the switch is connected. If only the switch to the external link down, and the switch itself does not fail, then bonding will think that the link is not a problem and continue to use;
D, max_bonds=2 NIC binding if Bond in more than one of the best plus this parameter limit bond maximum load several, if not loaded this parameter will network restart will be similar to the "BOND1 parameter, BOND1 load failed error"

Four, uninstall BOND0 equipment
1. Delete bond0 file


#rm-F/etc/sysconfig/network-scripts/ifcfg-bond0

2, modify the/etc/modprobe.conf file, delete the two lines added above

Alias Bond0 Bonding

Options bond0 miimon=100 mode=1 [max_bonds=2]

3, finally reconfigure eth0 and eth1 IP, and restart the network.

Two, two card binding automation script

In fact, careful observation of the above steps, are fixed. So we can write a script to automate the configuration.

#!/bin/bash
If [$#-lt 6];then
echo "Usage: $ <bond*> <eth*> <eth*> <ipaddress> <netmask> <gateway>"
echo "Eg: $ bond0 eth0 eth1 192.168.0.1 255.255.255.0 192.168.0.254″
Exit 1
Fi

#修改 ifcfg-bond* File
echo "device=$1
Ipaddr=$4
Netmask=$5
Gateway=$6
Onboot=yes
Bootproto=none
Userctl=no ">/tmp/ifcfg-$1
Mv-f/tmp/ifcfg-$1/etc/sysconfig/network-scripts/

#修改 ifcfg-eth0 File
echo "device=$2
Userctl=no
Onboot=yes
Master=$1
Slave=yes
Bootproto=none ">/tmp/ifcfg-$2
Mv-f/tmp/ifcfg-$2/etc/sysconfig/network-scripts/

#修改 ifcfg-eth1 File
echo "device=$3
Userctl=no
Onboot=yes
Master=$1
Slave=yes
Bootproto=none ">/tmp/ifcfg-$3
Mv-f/tmp/ifcfg-$3/etc/sysconfig/network-scripts/

# #修改 Network File
#sed/gateway/d/etc/sysconfig/network >/tmp/network
#echo "gateway=\" $6\ ">>/tmp/network
#mv-F/tmp/network/etc/sysconfig/

#修改 MODULES.COF/MODPROBE.COF
Modconf=/null
tempfile1=/tmp/mod1.$$
tempfile2=/tmp/mod2.$$
Bakfile=/etc/.modconf

#选择绑定的模式是轮询还是冗余, general recommendation option 1
echo "Please Select Your Bond Mode: (balance-rr/active-backup) or (0/1)?"
Read MODE

if [-f/etc/modprobe.conf]; Then
Modconf=/etc/modprobe.conf
Else
Modconf=/etc/modules.conf
Fi

CP $MODCONF $BAKFILE

Sed '/alias[\t]* ' $1′[\t]*bonding/d;/options[\t]* ' $1′[\t]*/d;/install.* ' $1′/d ' $MODCONF > $TEMPFILE 1
CP $TEMPFILE 1 $TEMPFILE 2

If ["$ (grep" alias.*bonding "$TEMPFILE 1)"!= "]; Then
bondcount=$ (grep "alias.*bonding" $TEMPFILE 1 | wc-l)
elif ["$ (grep" install.*bond.* "$TEMPFILE 1)"!= "]; Then
bondcount=$ (grep "install.*bond.*" $TEMPFILE 1 | wc-l)
Else
Bondcount=0
Fi

If ["$bondcount"-ge 1]; Then
Sed '/alias.*bonding/d;s/\ (options[\t]*\) \ (bond[0-9]*\)/install\ \2\ \/sbin\/modprobe\–ignore-install\ bonding\-o\ \2/' $TEMPFILE 1 > $TEMPFILE 2

echo "Install $1/sbin/modprobe–ignore-install Bonding-o $ miimon=100 mode= $MODE" >> $TEMPFILE 2
Else

echo "Alias $ bonding" >> $TEMPFILE 2
echo "Options $ miimon=100 mode= $MODE" >> $TEMPFILE 2
Fi
Mv-f $TEMPFILE 2 $MODCONF

#重启网络
echo "System would restart network continue (y/n)?"
Read BB
If ["$bb" = "y"] | | ["$BB" = "yes"] | | ["$bb" = "Y"];then
For Tempmod in $ (lsmod | grep-i Bond | awk ' {print $} ')
Todo
Modprobe-r bonding-o "$tempmod"
Done

/etc/init.d/network restart
Fi
echo "ok!"
Exit 0

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.