Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement dual Network Card binding script in Linux

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to implement the dual Nic binding script in Linux? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1. Scope of application

The shell script can create multiple bound NICs on the following linux systems, which can be used in the production environment:

Redhat 5.x

CentOS 5.x

Kylin 3.x

KUX 2.x

two。 Script characteristics

-Network Configuration Assistant-The information you have entered:First NIC: eth0Second NIC : eth2bond name: bond1IP address: 192.168.56.11netmask: 255.255.255.0bonding_mode: mode=1primary NIC: none-Pls make sure its OK [y/n]: y

As mentioned above, during the use of the script, the script will be prompted to enter the name of the sub-network card to be bound, the name of the bound network card, the IP address and subnet mask, the binding mode, whether to specify the primary network card, and actively identify and list the network cards available in the system for users to choose from. At the same time, it will judge the correctness of the information entered to ensure the smooth binding of the dual network cards.

3. instructions

First, upload the script to any directory on the linux system and execute it using the root user:

[root@node1 tmp] # sh bonding_v2.0.sh-Network Configuration Assistant- -- 4 network cards available:eth0 eth2 eth3 eth4-Please enter the First NIC:_

First, the script lists how many network cards are available in the system and prompts for the name of the first subnet card that needs to be bound. Enter eth3 here.

There is no need to worry about typing errors, repetition and other problems, the script will automatically determine whether your input information is correct.

-Network Configuration Assistant-4 network cards available:eth0 eth2 eth3 eth4The information you have entered : First NIC: eth3-Please select the second NIC:_

Then enter the name eth4 of the second Nic to be bound. If you enter an error, such as eth8, you will be prompted to re-enter:

-Network Configuration Assistant-4 network cards available:eth0 eth2 eth3 eth4The information you have entered : First NIC: eth3-eth8 is not available Please enter another one.Please select the second NIC:_

After correctly entering the second Nic eth4 that needs to be bound, if the bound Nic does not exist in the linux system, the name of the first bound Nic is bond0 by default. If there is a bound Nic in the system, the script prompts you to enter bondN (N is a number):

-Network Configuration Assistant-The information you have entered:First NIC: eth3Second NIC Eth4bond name: bond0-The default first bond name is-- bond0.Please enter an IP address:_

Follow the prompts to enter the IP address to be bound, such as 10.1.1.1

The script automatically determines whether the format and range of IP addresses entered are correct, without worrying about typing errors.

-Network Configuration Assistant-The information you have entered:First NIC: eth3Second NIC : eth4bond name: bond0IP address: 10.1.1.1-Pls enter the NETMASK [255.255.255.0]: _

After entering the IP address, you will be prompted to enter the subnet mask, which defaults to 255.255.255.0. You can enter directly or enter a custom subnet mask.

-Network Configuration Assistant-The information you have entered:First NIC: eth3Second NIC : eth4bond name: bond0IP address: 10.1.1.1netmask: 255.255.255.0-Pls enter the bonding_ mode[default: mode=1 Active-backup]: _

This step is to enter the binding level of the Nic. By default, you can use the master / slave mode: mode=1, and you can enter directly. You can also enter a custom binding level (such as entering mode=6) in the format:

Mode= {0,1,2,3,4,5 6}-Network Configuration Assistant-The information you have entered:First NIC: eth3Second NIC: eth4bond name: bond0IP address: 10.1.1.1netmask: 255.255.255.0bonding_mode: mode=1-1-eth32-eth43-nonechoose whether to set up the primary NIC or not [default:3-none]: _

At this time, the script prompts whether to set the primary network card. The function of the primary network card is to give priority to the active primary network card when the sub-network cards are normal. Here, the default is not to set the primary network card, or you can enter the number 1 or 2 to select a network card as the primary network card.

-Network Configuration Assistant-The information you have entered:First NIC: eth3Second NIC : eth4bond name: bond0IP address: 10.1.1.1netmask: 255.255.255.0bonding_mode: mode=1primary NIC: none-Pls make sure its OK [y/n]: _

At this point, after all the information has been entered, the script will let you determine whether all the information entered is correct, enter y correctly, 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_bakyou can check the file and then restart the network service.1) service network restart2) 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, and enter 2 to exit the script directly.

It is recommended to restart the network service. If you directly exit the script without restarting the network service, the modified configuration file will not take effect, and an exception will appear when you use the script to create the next bound ENI (because the script obtains the current ENI information through the ifconfig command).

You can check the file and then restart the network service.1) service network restart2) exit-Please make your choice:1 is shutting down interface eth0: [OK] interface eth2: [OK] is shutting down Interface eth3: [OK] shutting down interface eth4: [OK] shutting down loopback interface: [OK] pop-up loopback interface: [OK] pop-up interface bond0: RTNETLINK answers: File exists error while adding address 10.1.1.1 to bond0. [OK] pop-up interface eth0: [OK] pop-up interface eth2: [OK]

An error similar to the above may occur when the network service is restarted for the first time. Don't worry. In fact, the configuration has been completed. Restart the network service again and it will return to normal.

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

[root@node1 tmp] # cd / etc/sysconfig/network-scripts/ [root@node1 network-scripts] # cat ifcfg-bond0 DEVICE=bond0BOOTPROTO=noneONBOOT=yesTYPE=EthernetUSERCTL=noIPV6INIT=noPEERDNS=yesIPADDR=10.1.1.1NETMASK=255.255.255.0BONDING_OPTS= "miimon=100 mode=1" [root@node1 network-scripts] # cat ifcfg-eth3 DEVICE=eth3BOOTPROTO=none#HWADDR=08:00:27:41:b4:16ONBOOT=yesTYPE=EthernetUSERCTL=noIPV6INIT=noPEERDNS=yesSLAVE=yesMASTER=bond0 [root@node1 network-scripts] # cat ifcfg-eth4DEVICE=eth4BOOTPROTO=none#HWADDR=08:00:27:6c:3d:0cONBOOT=yesTYPE=EthernetUSERCTL=noIPV6INIT=noPEERDNS=yesSLAVE=yesMASTER=bond0 [root@node1 network-scripts] # cat / etc/modprobe.confalias scsi_hostadapter ata _ piixalias scsi_hostadapter1 ahcialias net-pf-10 offalias ipv6 offoptions ipv6 disable=1alias snd-card-0 snd-intel8x0options snd-card-0 index=0options snd-intel8x0 index=0remove snd-intel8x0 {/ usr/sbin/alsactl store 0 > / dev/null 2 > & 1 | |: }; / sbin/modprobe-r-- ignore-remove snd-intel8x0alias eth0 e1000alias eth2 e1000alias eth3 e1000alias eth4 e1000alias bond0 bonding

The network configuration information is as follows:

[root@node1 tmp] # ifconfig bond0 Link encap:Ethernet HWaddr 08:00:27:41:B4:16 inet addr:10.1.1.1 Bcast:10.1.1.255 Mask:255.255.255.0 UP BROADCAST RUNNING MASTER MULTICAST MTU:1500 Metric:1 RX packets:1197 errors:0 dropped:0 overruns:0 frame:0 TX packets:125 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:106338 ( 103.8 KiB) TX bytes:16662 (16.2 KiB) eth0 Link encap:Ethernet HWaddr 08:00:27:35:B4:81 inet addr:192.168.56.11 Bcast:192.168.56.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1487 errors:0 dropped:0 overruns:0 frame:0 TX packets:1190 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:146576 (143.1 KiB) TX bytes:151787 (148.2 KiB) eth2 Link encap:Ethernet HWaddr 08 KiB 00 00 7 7 7 8 1 46 inet addr:10.1.1.11 Bcast:10.1.1.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:193 errors:0 dropped:0 overruns:0 frame:0 TX packets:455 errors:0 dropped:0 overruns:0 carrier:0 collisions 0 txqueuelen:1000 RX bytes:31434 (30.6 KiB) TX bytes:22657 (22.1 KiB) eth3 Link encap:Ethernet HWaddr 08:00:27:41:B4:16 UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1 RX packets:591 errors:0 dropped:0 overruns:0 frame:0 TX packets:93 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:55217 (53.9 KiB) TX bytes:10511 (10 .2 KiB) eth4 Link encap:Ethernet HWaddr 08:00:27:41:B4:16 UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1 RX packets:606 errors:0 dropped:0 overruns:0 frame:0 TX packets:32 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:51121 (49.9 KiB) TX bytes:6151 (6.0 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:3297 errors:0 dropped:0 overruns:0 frame:0 TX packets:3297 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:4725726 (4.5 MiB) TX bytes:4725726 (4.5 MiB)

Binding information of dual Nic:

[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: NoneCurrently Active Slave: eth3MII Status: upMII Polling Interval (ms): 100Up Delay (ms): 0Down Delay (ms): 0Slave Interface: eth3MII Status: upSpeed: 1000 MbpsDuplex: fullLink Failure Count: 0Permanent HW addr: 08:00:27:41:b4:16Slave Interface: eth4MII Status: upSpeed: 1000 MbpsDuplex: fullLink Failure Count: 0Permanent HW addr: 08:00:27:6c:3d:0c

4. Script code

Update of the #! / bin/bash# Date:2018-11-26 1414 bin/bash# Date:2018 28 modprobe.conf Author:ZhangJian# Mail:1037509307@qq.com# Func:Configure Network Bonding# Version:2.0#V1.6 version: the bound mode mode=x is no longer written to the modprobe.conf file, but to the ifcfg-bond, because when the mode mode is modified in the modprobe.conf file, the restart of the network service may not take effect. # V2.0 update: set the primary network card to be optional. Trap "tput clear;tput cup 3x echo 'Any Questions: Send a message to QQ 1037509307. The candidate tput cup 6 exit" 2 3if [[- n $1]]; then cat / dev/null if [[$?-eq 0]]; then echo "${NIC_NAME_bond}" | grep-Fw "${NAME_bond}" & > / dev/null if [[$?-ne 0]] Then info_print "${NIC_NAME_bond}" echo-e "The information you have entered:\ nFirst NIC: ${NIC1}\ nSecond NIC: ${NIC2}\ nbond name: ${NAME_bond}" echo-e "-\ n" break else Info_print "${NIC_NAME_bond}" echo-e "The information you have entered:\ nFirst NIC: ${NIC1}\ nSecond NIC: ${NIC2}" echo-e "-\ n" echo "You can not use an existing name: ${NAME_bond}" Echo-e "\ 033 [31m ${NAME_bond}\ 033 [0m is not available] Please enter another one like-bondN. " Fi else info_print "${NIC_NAME_bond}" echo-e "The information you have entered:\ nFirst NIC: ${NIC1}\ nSecond NIC: ${NIC2}" echo-e "-\ n" echo-e "033 [31m ${NAME_bond}\ 033 [0m is not available" Please enter another one like-bondN. " Fi done fi # set IP address while true Do echo-e "\ nPlease enter an IP address:_\ b\ c" read IP_bond echo "${IP_bond}" | grep-owE'^ (([1-9] | [1-9] [0-9] | 1 [0-9] {2} | 2 [0-5] {2})\.) {3} ([1-9] | [1-9] [0-9] | 1 [0-9] {2} | 2 [0-5] {2}) & > / dev/null if [[$?-eq 0]] Then info_print echo-e "The information you have entered:\ nFirst NIC: ${NIC1}\ nSecond NIC: ${NIC2}\ nbond name: ${NAME_bond}\ nIP address: ${IP_bond}" echo-e "-\ n" break else info_print echo -e "The information you have entered:\ nFirst NIC: ${NIC1}\ nSecond NIC: ${NIC2}\ nbond name: ${NAME_bond}" echo-e "-\ n" echo-e "IP address:\ 033 [31m ${IP_bond}\ 033 [0m format errors" Please re-enter. " Fi done # set netmask while true Do echo-e "\ nPls enter the NETMASK [255.255.255.0]: _\ b\ c" read NETMASK_bond NETMASK_bond=$ {NETMASK_bond:= "255.255.255.0"} # when the variable is NETMASK_bond, the default value of 255.255.255.0 echo "$NETMASK_bond" is assigned to it | grep-owE'^. Then info_print echo-e "The information you have entered:\ nFirst NIC: ${NIC1}\ nSecond NIC: ${NIC2}\ nbond name: ${NAME_bond}\ nIP address: ${IP_bond}\ nnetmask: ${NETMASK_bond}" echo-e "-\ N "break else info_print echo-e" The information you have entered:\ nFirst NIC: ${NIC1}\ nSecond NIC: ${NIC2}\ nbond name: ${NAME_bond}\ nIP address: ${IP_bond} "echo-e" -\ n "echo-e" Input error.\ nPlease enter the correct NETMASK or press ENTER to use 255.255.255.0.\ n "fi done#: > $BOND_DIR/$BOND echo" BOOTPROTO=none "> > $BOND_DIR/$BOND echo" ONBOOT=yes "> > $BOND_DIR/$BOND echo" TYPE=Ethernet "> > $BOND_DIR/$BOND echo" USERCTL=no "> > $BOND_DIR/$BOND echo" IPV6INIT=no "> > $BOND_DIR/$BOND echo" PEERDNS=yes "> > $BOND_DIR/$BOND echo" IPADDR=$ {BOND_IPADDR} "> > $BOND_DIR/$BOND echo" NETMASK=$ {BOND_NETMASK} " > $BOND_DIR/$BOND if [[${get_primary_NIC} = = none]] Then echo "BONDING_OPTS=\" miimon=100 ${mode_bond}\ "> $BOND_DIR/$BOND else echo" BONDING_OPTS=\ "miimon=100 ${mode_bond} primary=$ (echo ${SLAVE1} | cut-dazzle SLAVE1'- f2)\" > > $BOND_DIR/$BOND fi # modify $SLAVE1 > $BOND_DIR/$SLAVE1 echo "DEVICE=$ {SLAVE1_DEV}" > > $BOND_DIR/$SLAVE1 echo 'BOOTPROTO=none' > > $BOND_DIR/$SLAVE1 # cat $BOND_DIR/../ifcfg-bak/$SLAVE1 | grep HWADDR > > $BOND_DIR/$SLAVE1 echo "# $SLAVE1_MAC" > > $BOND_DIR/$SLAVE1 echo ONBOOT=yes > > $BOND_DIR/$SLAVE1 echo TYPE=Ethernet > > $BOND_DIR/$SLAVE1 echo USERCTL=no > > $BOND_DIR/$SLAVE1 echo IPV6INIT=no > > $BOND_DIR/$SLAVE1 echo PEERDNS=yes > > $BOND_DIR/$SLAVE1 echo SLAVE=yes > > $BOND_DIR/$SLAVE1 echo MASTER=$BOND_DEV > > $BOND_DIR/$SLAVE1 # modify SLAVE2 > $BOND_DIR/$SLAVE2 echo "DEVICE=$SLAVE2_DEV" > > $BOND_DIR/$SLAVE2 echo BOOTPROTO=none > > $BOND_DIR/$SLAVE2 # cat $BOND_DIR/../ifcfg-bak/$SLAVE2 | grep HWADDR > > $BOND_DIR/$SLAVE2 echo "# $SLAVE2_MAC" > > $BOND_DIR/$SLAVE2 echo ONBOOT=yes > > $BOND_DIR/$SLAVE2 echo TYPE=Ethernet > > $BOND_DIR/$SLAVE2 echo USERCTL=no > > $BOND_DIR/$SLAVE2 echo IPV6INIT=no > > $BOND_DIR/$SLAVE2 echo PEERDNS=yes > > $BOND_DIR/$SLAVE2 echo SLAVE=yes > > $BOND_DIR/$SLAVE2 echo MASTER=$BOND_DEV > > $BOND_DIR/$SLAVE2 [[- e / etc/modprobe.conf.bak]] & & cp / etc/modprobe.conf / etc/modprobe.conf.bak.new | | cp / etc/modprobe. Conf / etc/modprobe.conf.bak echo "alias $BOND_DEV bonding" > > / etc/modprobe.confwhile true Do tput clear;tput cup 2cat

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report