CentOS 7.x 网卡名修改脚本

脚本参考http://blog.csdn.net/fanhonooo/article/details/53494100编写

自动识别当前网卡名称,并将其改为eth,如需改为其他名称,请修改“device_name=eth”

修改网卡名后需要重启系统生效,脚本中默认会在30秒等待后进行重启,用户可键入Ctrl+C中断重启

转载请注明出处:http://www.madown.com/2017/07/17/centos-7-x-网卡名修改脚本/

#!/bin/bash
#
# CentOS 7.x 修改网卡名称为eth0,eth1,…
#
device_name=eth
i=0
ls /etc/sysconfig/network-scripts/ifcfg-* |grep -v ifcfg-lo |sort -n |while read line
do
if [ “$(basename ${line})” != “ifcfg-${device_name}${i}” ];then
printf “Change Network Device Name $(basename ${line} |awk -F’-‘ ‘{print $2}’) to ${device_name}${i}…”
mv ${line} $(dirname ${line})/ifcfg-${device_name}${i}
sed -i “s/NAME=$(basename ${line} |awk -F’-‘ ‘{print $2}’)/NAME=${device_name}${i}/g” $(dirname ${line})/ifcfg-${device_name}${i}
sed -i “s/DEVICE=$(basename ${line} |awk -F’-‘ ‘{print $2}’)/DEVICE=${device_name}${i}/g” $(dirname ${line})/ifcfg-${device_name}${i}
printf “\033[32;1m%10s\033[0m\n” “[ DONE ]”
fi
((i++))
done
sed -i “/GRUB_CMDLINE_LINUX/s/\(.*\)rhgb\(.*\)/\1net.ifnames=0 biosdevname=0 rhgb\2/g” /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null
printf “\033[32;1mReboot…\033[0m\n”
printf “\033[32;1mYou Can Enter Ctrl+C To Stop Reboot\033[0m\n”
sec=30
while [ $sec -ge 0 ]
do
printf “\033[32;1mWait for %d Seconds\r\033[0m” $sec
sleep 1
sec=$(( $sec – 1 )) done
reboot

发表评论

电子邮件地址不会被公开。 必填项已用*标注