Linux双网卡绑定脚本的方法示例
linux运维及配置工作中,常常会用到双网卡绑定,少数几台服务器的配置还好,如果是需要配置几十甚至上百台,难免会枯燥乏味,易于出错,我编写了这个双网卡绑定的辅助脚本,可傻瓜式地完成linux双网卡绑定工作,当然,该脚本主要还是用于小批量的系统配置,如需配置大量的服务器,可提取脚本中的bonding函数,稍作修改即可,你值得一试!
1.适用范围
该shell脚本可在以下linux系统创建多个绑定网卡,用于生产环境没问题的:
- Redhat 5.x
- CentOS 5.x
- Kylin 3.x
- KUX 2.x
2.脚本特点
---------------------------------------------- ------ 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
如上,脚本在使用过程中,会依次提示输入需要绑定的子网卡名称、绑定网卡名称、IP地址和子网掩码、绑定模式、是否指定主网卡,并主动对系统中可用网卡进行识别并列出,供用户选择,同时会判断输入的各项信息的正确性,保障双网卡绑定顺利进行。
3.使用说明
首先将脚本上传至linux系统任意目录下,使用root用户执行:
[root@node1 tmp]# sh bonding_v2.0.sh ---------------------------------------------- ------ Network Configuration Assistant ------ ---------------------------------------------- 4 network cards available: eth0 eth1 eth2 eth3 ---------------------------------------------- Please enter the First NIC:_
首先脚本会列出系统中有多少可用网卡,并提示输入需要绑定的第一块子网卡的名称,这里输入eth2。
无需担心输入错误,输入重复等问题,脚本会自动判断你的输入信息是否正确。
---------------------------------------------- ------ Network Configuration Assistant ------ ---------------------------------------------- 4 network cards available: eth0 eth1 eth2 eth3 The information you have entered: First NIC : eth2 ---------------------------------------------- Please select the second NIC:_
接着输入需要绑定的第二块网卡的名称eth3,如果输入错误会出现相应的提示,比如错输入为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:_
正确输入需要绑定的第二块网卡eth3后,如果linux系统中之前不存在绑定网卡,则默认第一个绑定网卡的名称为bond0,如果系统中存在绑定网卡,脚本会提示输入bondN(N为数字):
---------------------------------------------- ------ 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:_
按照提示输入需要绑定的IP地址,比如10.1.1.1
脚本会自动判断输入的IP地址格式和范围是否正确,无需担心输入错误
---------------------------------------------- ------ 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]:_
输入IP地址后,会提示输入子网掩码,默认为255.255.255.0,直接回车即可,也可以输入自定义的子网掩码。
---------------------------------------------- ------ 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]:_
这一步是输入网卡绑定级别,默认使用主备模式:mode=1,直接回车即可,也可以输入自定义的绑定级别(如输入 mode=6),格式为:
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]:_
此时脚本提示是否设置primary网卡,primary网卡的作用是在子网卡均正常时,primary网卡优先处于活动状态,这里默认为不设置primary网卡,也可以输入数字1或2选择一个网卡为primary网卡。
---------------------------------------------- ------ 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]:_
至此,所有信息输入完毕,脚本会让您判断输入的所有信息是否正确,正确则输入y,进行下一步操作,否则输入n,重新输入以上信息。
---------------------------------------------- ------ 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:_
看到这里时,所有绑定网卡的配置文件已经修改完毕了,输入1重启网络服务;输入2直接退出脚本。
建议选择重启网络服务,如果直接退出脚本而不重启网络服务,修改的配置文件是不会生效的,并且在使用该脚本创建下一个绑定网卡时显示信息会出现异常(因为脚本通过ifconfig命令获取当前网卡信息)。
you can check the file and then restart the network service.
1 ) service network restart
2 ) exit
----------------------------------------------
Please make your choice:1
正在关闭接口 eth0: [确定]
正在关闭接口 eth1: [确定]
正在关闭接口 eth2: [确定]
正在关闭接口 eth3: [确定]
关闭环回接口: [确定]
弹出环回接口: [确定]
弹出界面 bond0: RTNETLINK answers: File exists
在 bond0 添加地址 10.1.1.1 时出错。
[确定]
弹出界面 eth0: [确定]
弹出界面 eth1: [确定]
第一次重启网络服务时可能会出现以上类似报错,无需担心,其实配置已经完成,再次重启网络服务即恢复正常。
在绑定过程中修改的配置文件如下:
[root@node1 tmp]# cd /etc/sysconfig/network-scripts/
[root@node1 network-scripts]# cat ifcfg-bond0
DEVICE=bond0
BOOTPROTO=none
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
IPV6INIT=no
PEERDNS=yes
IPADDR=10.1.1.1
NETMASK=255.255.255.0
BONDING_OPTS="miimon=100 mode=1"
[root@node1 network-scripts]# cat ifcfg-eth2
DEVICE=eth2
BOOTPROTO=none
#HWADDR=08:00:27:41:b4:16
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
IPV6INIT=no
PEERDNS=yes
SLAVE=yes
MASTER=bond0
[root@node1 network-scripts]# cat ifcfg-eth3
DEVICE=eth3
BOOTPROTO=none
#HWADDR=08:00:27:6c:3d:0c
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
IPV6INIT=no
PEERDNS=yes
SLAVE=yes
MASTER=bond0
[root@node1 network-scripts]# cat /etc/modprobe.conf
alias scsi_hostadapter ata_piix
alias scsi_hostadapter1 ahci
alias net-pf-10 off
alias ipv6 off
options ipv6 disable=1
alias snd-card-0 snd-intel8x0
options snd-card-0 index=0
options snd-intel8x0 index=0
remove snd-intel8x0 { /usr/sbin/alsactl store 0 >/dev/null 2>&1 || : ; }; /sbin/modprobe -r --ignore-remove snd-intel8x0
alias eth0 e1000
alias eth1 e1000
alias eth2 e1000
alias eth3 e1000
alias bond0 bonding
网络配置信息如下:
[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)
eth1 Link encap:Ethernet HWaddr 08:00:27:72:08: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)
eth2 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)
eth3 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)
双网卡绑定信息:
[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.脚本代码
#!/bin/bash # Date:2018-11-26 14:28:45 # Author:ZhangJian # Mail:1037509307@qq.com # Func:Configure Network Bonding # Version:2.0 #V1.6版本更新:不再将绑定的模式mode=x写入modprobe.conf文件,将其写入ifcfg-bond中,因为在modprobe.conf文件中时,修改了mode模式后,重启网络服务可能不生效。 #V2.0版本更新:将primary网卡设置为可选择项。 trap "tput clear;tput cup 3;echo 'Any Questions: Send a message to QQ 1037509307.';tput cup 6;exit" 2 3 if [[ -n $1 ]]; then cat <
