Centos7自动修改网卡名称脚本

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

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

#!/bin/sh

[ -f /etc/init.d/functions ] && . /etc/init.d/functions
###修改网卡配置文件名称
function net () {
b0=-1
cat /proc/net/dev |grep ':' | grep -v 'lo' | cut -d: -f1 | sort >> /tmp/net_name.txt
do
c0=$line
b0=`expr $b0 + 1`
mv /etc/sysconfig/network-scripts/ifcfg-$c0 /etc/sysconfig/network-scripts/ifcfg-eth$b0
##修改网卡模式为static
sed -i 's/dhcp/static/g' /etc/sysconfig/network-scripts/ifcfg-eth$b0
##删除包含IPV6的行
sed -i '/IPV6/d' /etc/sysconfig/network-scripts/ifcfg-eth$b0
##修改网卡DEVICE为eth.
sed -i 's/'$c0'/eth'$b0'/g' /etc/sysconfig/network-scripts/ifcfg-eth$b0
done < /tmp/net_name.txt
}
###重新配置grub配置并更新内核
function grub () {
sed -i 's/crashkernel=auto rhgb quiet/crashkernel=auto net.ifnames=0 biosdevname=0 rhgb quiet/g' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
}

###修改网卡创建命名规则
function rules () {
b0=-1;
 lshw -c network| egrep "logical name" |awk -F " " '{print $3}' |while read line
do
 a0=$line
 b0=`expr $b0 + 1`
 c0=`lshw -c network | egrep -C 2 $a0|egrep serial | awk -F " " '{print $2}'`
 d0=`lshw -c network |egrep -C 2 $a0|egrep "bus info" | awk -F "@" '{print $2}'`
 echo 'ACTION=="add", SUBSYSTEM=="net", BUS=="pci", ATTR{address}=="'$c0'", ID=="'$d0'", NAME="eth'$b0'"' >>/etc/udev/rules.d/70-persistent-net.rules
 sed -i '$a HWADDR='$c0'' /etc/sysconfig/network-scripts/ifcfg-eth$b0
done
}
net
grub
rules

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

发表评论

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