An example of an Linux dual network card binding script method

  • 2020-12-19 21:22:00
  • OfStack

linux operational and configuration work, often use double nic binding, a few server configuration is also good, if it is need to be configured for ten or even hundreds of units, unavoidably boring, prone to error, I wrote the auxiliary of the double card binding script that can fool linux double nic binding work, of course, the script is mainly used for small batch system configuration, if you want to configure a large number of servers, can be extracted in the script bonding function, modify, you worth a try!

1. Scope of application

The shell script can create multiple bound network cards on the following linux systems for production use:

Redhat 5.x CentOS 5.x Kylin 3.x KUX 2.x

2. Script features


----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

The information you have entered:

First NIC   :eth0
Second NIC  :eth1
bond name   :bond1
IP address  :192.168.56.11
netmask    :255.255.255.0
bonding_mode :mode=1
primary NIC  :none
----------------------------------------------

Pls make sure its OK[y/n]:y

As above, the script in use process, will, in turn, prompt type in the name of the sub card need binding, binding the card name, IP address and subnet mask, whether binding mode, specify the network adapter, and actively to identify and list the available in the system network card for the user to choose, can judge the correctness of the input all the information at the same time, ensure double nic binding smoothly.

3. Instructions

First, upload the script to any directory of linux system and use root user to execute:


[root@node1 tmp]# sh bonding_v2.0.sh 

----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

 
4 network cards available:
eth0 eth1 eth2 eth3
----------------------------------------------

Please enter the First NIC:_

First, the script lists how many network cards are available on the system and prompts for the name of the first subnetwork card to bind to. Enter eth2 here.

There is no need to worry about typing errors, repetition, etc. The script will automatically determine if your input is correct.


----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

4 network cards available:
eth0 eth1 eth2 eth3

The information you have entered:
First NIC   : eth2
----------------------------------------------

Please select the second NIC:_

Then enter the name of the second network card to be bound, eth3. If the input is wrong, a corresponding prompt will appear. For example, if the input is wrong, it will be prompted to re-enter eth8:


----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

4 network cards available:
eth0 eth1 eth2 eth3

The information you have entered:
First NIC   : eth2
----------------------------------------------

eth8 is not available,Please enter another one.

Please select the second NIC:_

After correctly entering eth3, the second network card to be bound, if there is no binding network card in the linux system before, the default name of the first binding network card is bond0. If there is a binding network card in the system, the script will prompt bondN(N is the number) :


----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

The information you have entered:
First NIC   : eth2
Second NIC  : eth3
bond name   : bond0
----------------------------------------------

The default first bond name is -- bond0.

Please enter an IP address:_

Follow the prompts for the IP address to bind to, such as 10.1.1.1

The script automatically determines whether the IP address format and range entered are correct without worrying about typing errors


----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

The information you have entered:
First NIC   : eth2
Second NIC  : eth3
bond name   : bond0
IP address  : 10.1.1.1
----------------------------------------------

Pls enter the NETMASK[255.255.255.0]:_

When you enter the IP address, you will be prompted to enter the subnet mask. The default is 255.255.255.0. You can either enter the subnet mask or enter a custom subnet mask.


----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

The information you have entered:
First NIC   : eth2
Second NIC  : eth3
bond name   : bond0
IP address  : 10.1.1.1
netmask    : 255.255.255.0
----------------------------------------------

Pls enter the bonding_mode[default:mode=1,active-backup]:_

This step is to enter the binding level of the network card. The default mode is mode=1, enter directly, or you can enter the customized binding level (such as mode=6), and the format is:


mode= { 0,1,2,3,4,5,6 } 

----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

The information you have entered:

First NIC   :eth2
Second NIC  :eth3
bond name   :bond0
IP address  :10.1.1.1
netmask    :255.255.255.0
bonding_mode :mode=1
----------------------------------------------

1-eth2
2-eth3
3-none
choose whether to set up the primary NIC or not[default:3-none]:_

At this time the script prompts whether to set primary network card, primary network card's role is in the sub-network card are normal, primary network card priority in the active state, the Limo that do not set primary network card, can also enter the number 1 or 2 to choose a network card for primary card.


----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

The information you have entered:

First NIC   :eth2
Second NIC  :eth3
bond name   :bond0
IP address  :10.1.1.1
netmask    :255.255.255.0
bonding_mode :mode=1
primary NIC  :none
----------------------------------------------

Pls make sure its OK[y/n]:_

At this point, after all the information is entered, the script will let you determine whether all the input information is correct. If correct, enter y and proceed to the next step. Otherwise, enter n and re-enter the above information.


----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

Complete!
File backup directory: /etc/sysconfig/network-scripts/inspur_bak

you can check the file and then restart the network service.

1 ) service network restart
2 ) exit
----------------------------------------------
Please make your choice:_

When you see here, all the configuration files bound to the network card have been modified. Enter 1 to restart the network service. Type 2 to exit the script directly.

It is recommended that you choose to restart the network service. If you simply exit the script instead of restarting the network service, the modified configuration file will not take effect, and there will be an exception when using the script to create the next bound network card (because the script gets the current network card information through the ifconfig command).


[root@node1 tmp]# sh bonding_v2.0.sh 

----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

 
4 network cards available:
eth0 eth1 eth2 eth3
----------------------------------------------

Please enter the First NIC:_
0

The above error may occur when you restart the network service for the first time. There is no need to worry. In fact, the configuration has been completed and the network service will be back to normal once you restart it again.

The configuration files modified during the binding process are as follows:


[root@node1 tmp]# sh bonding_v2.0.sh 

----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

 
4 network cards available:
eth0 eth1 eth2 eth3
----------------------------------------------

Please enter the First NIC:_
1

Network configuration information is as follows:


[root@node1 tmp]# sh bonding_v2.0.sh 

----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

 
4 network cards available:
eth0 eth1 eth2 eth3
----------------------------------------------

Please enter the First NIC:_
2

Dual-network card binding information:


[root@node1 tmp]# cat /proc/net/bonding/bond0 
Ethernet Channel Bonding Driver: v3.4.0-2 (October 7, 2008)

Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: eth2
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth2
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 08:00:27:41:b4:16

Slave Interface: eth3
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 08:00:27:6c:3d:0c

4. Scripting code


[root@node1 tmp]# sh bonding_v2.0.sh 

----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

 
4 network cards available:
eth0 eth1 eth2 eth3
----------------------------------------------

Please enter the First NIC:_
4

Related articles: