Centos7自动修改网卡名称脚本

2015年12月11日15:35:00 评论 604

用过Centos7的同学都会发现上面的网卡命名规则是不规范的,对于习惯了之前的eth或em的命名难免想要修改回去。

最近公司在定制Centos7时就提了这个需求,于是就写了个脚本在安装系统时运行脚本自动修改网卡名称。

#!/bin/bash

# Script Name:NICs-Setting.sh 
# Support OS Type: RHEL/CentOS 5.x/6.x/7.x

function V5()
{
#local b0=-1
#local c0
#sed -i '/HWADDR/d' `grep HWADDR -l /etc/sysconfig/network-scripts/ifcfg-eth?`
[ ! -d /etc/sysconfig/network-scripts/NIC_Backup ] && mkdir /etc/sysconfig/network-scripts/NIC_Backup
b0=-1
cat /proc/net/dev |grep ':' | grep -v 'lo' | grep -v 'bond' |grep -v 'sit0' | cut -d: -f1 |sort > /tmp/net_name.txt
while read line
do
 c0=$line
 b0=`expr $b0 + 1`
 cp -f /etc/sysconfig/network-scripts/ifcfg-$c0 /etc/sysconfig/network-scripts/NIC_Backup &> /dev/null
 mv -f /etc/sysconfig/network-scripts/ifcfg-$c0 /etc/sysconfig/network-scripts/ifcfg-eth$b0 &> /dev/null
 sed -i '$a BOOTPROTO=static' /etc/sysconfig/network-scripts/ifcfg-eth$b0
 sed -i '$a NAME=eth'$b0'' /etc/sysconfig/network-scripts/ifcfg-eth$b0
 sed -i '/HWADDR/d' `grep HWADDR -l /etc/sysconfig/network-scripts/ifcfg-eth$b0`
done < /tmp/net_name.txt

cp $V5NetRuleFile $V5NetRuleFile-`date +%Y-%m-%d-%R`

> $V5NetRuleFile
local number=0
lshw -C network | grep "bus info" | awk -F : '{print $3":"$4}' | sort | while read line
do
 c0=$line
 echo 'ACTION=="add", SUBSYSTEM=="net", BUS=="pci", ID=="0000:'$c0'", NAME="eth'$number'"' >>/etc/udev/rules.d/60-net.rules
 let number++
done
# 
}

function V6()
{
 chmod a-x $V6WriteRuleCmd
 local number=0
 cp $V6NetRuleFile $V6NetRuleFile-`date +%Y-%m-%d-%R`
 > $V6NetRuleFile

 lshw -C network | grep "bus info" | awk -F : '{print $3":"$4}' | sort | while read line
 do
 echo "SUBSYSTEMS==\"pci\", ACTION==\"add\", DRIVERS==\"?*\", KERNELS==\"0000:$line\", NAME=\"eth$number\"" >> $V6NetRuleFile
 let number++
 done
}

function V7()
{

 [ ! -d /etc/sysconfig/network-scripts/NIC_Backup ] && mkdir /etc/sysconfig/network-scripts/NIC_Backup
 b0=-1
 cat /proc/net/dev |grep ':' | grep -v 'lo' | grep -v 'bond' | cut -d: -f1 |sort > /tmp/net_name.txt
 while read line
 do
 c0=$line
 b0=`expr $b0 + 1`
 cp -f /etc/sysconfig/network-scripts/ifcfg-$c0 /etc/sysconfig/network-scripts/NIC_Backup &> /dev/null
 mv -f /etc/sysconfig/network-scripts/ifcfg-$c0 /etc/sysconfig/network-scripts/ifcfg-eth$b0 &> /dev/null
 sed -i 's/dhcp/static/g' /etc/sysconfig/network-scripts/ifcfg-eth$b0
 sed -i '/IPV6/d' /etc/sysconfig/network-scripts/ifcfg-eth$b0
 sed -i 's/'$c0'/eth'$b0'/g' /etc/sysconfig/network-scripts/ifcfg-eth$b0
 done < /tmp/net_name.txt

 sed -i '/GRUB_CMDLINE_LINUX/d' /etc/default/grub
 sed -i '$a GRUB_CMDLINE_LINUX="crashkernel=auto net.ifnames=0 biosdevname=0 rhgb quiet"' /etc/default/grub
 grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null

 local number=0
 cp $V7NetRuleFile $V7NetRuleFile-`date +%Y-%m-%d-%R`
 > $V7NetRuleFile

 lshw -C network | grep "bus info" | awk -F : '{print $3":"$4}' | sort | while read line
 do
 echo "SUBSYSTEMS==\"pci\", ACTION==\"add\", DRIVERS==\"?*\", KERNELS==\"0000:$line\", NAME=\"eth$number\"" >>$V7NetRuleFile
 let number++
 done
}

# Main Start here.
#
PACKAGE1=redhat-lsb
V7PACKAGE=redhat-lsb-core
PACKAGE2=pciutils

V5NetRuleFile=/etc/udev/rules.d/60-net.rules
V6NetRuleFile=/etc/udev/rules.d/70-persistent-net.rules
V7NetRuleFile=/etc/udev/rules.d/70-persistent-net.rules
V6WriteRuleCmd=/lib/udev/write_net_rules

ErrUID=65
ErrPackage=66
ErrOSType=67
ErrOSVersion=68

OSType=`lsb_release -i | grep -E 'RedHat|CentOS'`
OSRelease=`lsb_release -r | awk '{print $2}'`


if [ $UID -ne 0 ]; then
 echo "The script must be execute by root"
 echo "Exiting now......"
 exit $ErrUID
fi

if [ "${OSRelease%%.*}"x == 7x ]; then
 rpm -q $V7PACKAGE &>/dev/null && rpm -q $PACKAGE2 &>/dev/null
 if [ $? -ne 0 ]; then
 echo "Please install the "$V7PACKAGE" and "$PACKAGE2" packages fir"
 echo "Exiting now......"
 fi
else
rpm -q $PACKAGE1 &>/dev/null && rpm -q $PACKAGE2 &>/dev/null
 if [ $? -ne 0 ]; then
 echo "Please install the "$PACKAGE1" and "$PACKAGE2" packages first, then execute the script."
 echo "Exiting now......"
 exit $ErrPackage
 fi
fi

if [ "$OSType" == "" ]; then
 echo "Unsupport OS type, the script only support OS type: RHEL/CentOS."
 exit $ErrOSType
else
 case $OSRelease in
 5.*) V5 ;;
 6.*) V6 ;;
 7.*) V7 ;;
 *) echo "Unsupport OS release version, the script only support release version: 5.x/6.x/7.x."
 echo "Exiting now..."
 exit $ErrOSVersion
 ;;
 esac
fi

echo -e '
//------------------------------
//\tNetwork setting
//------------------------------
'

echo "Execute the scripts success, the NICs are in line!"

weinxin
聂扬帆博客
一个分享IT运维相关工作经验和实战技巧的个人博客

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: