CentOS6之前基于传统的命名方式如:eth1
,eth0
....
Centos7提供了不同的命名规则,默认是基于固件、拓扑、位置信息来分配。这样做的优点是命名是全自动的、可预知的,缺点是比eth0
、wlan0
更难读。比如enp5s0
系统环境
[root@demo1 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@demo1 ~]# uname -a
Linux demo1 3.10.0-957.21.3.el7.x86_64 #1 SMP Tue Jun 18 16:35:19 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
[root@demo1 ~]# date
2021年 07月 01日 星期四 14:28:21 CST
脚本使用
等待60秒进行重启即可,60秒内随时按下Ctrl+C取消重启
重启后网卡名称已设置为eth0
配置恢复
执行恢复操作后会恢复至之前的ens命名规则
有部分机器恢复后会出现还是eth0的情况,可以使用nmtui进行修改下即可
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
cp="/bin/cp"
mv="/bin/mv"
BAK_PATH="/usr/src"
#GRUB_FILE="/etc/sysconfig/grub"
GRUB_FILE2="/etc/default/grub"
GRUB_CFG="/boot/grub2/grub.cfg"
DATE_NEW=`date +"%y-%m-%d"`
FILE_PATH="/etc/sysconfig/network-scripts"
FILE_NAMES=`ls ${FILE_PATH} | grep ifcfg | grep -Ev 'bak|lo'`
IPADDR=`ifconfig | grep -E '^ens' -A 1 | tail -1 | awk -F'[ ]+' '{print $3}'`
CHECK_ROOT() {
[[ $EUID != 0 ]] && echo -e "${ERROR} 当前账号非ROOT(或没有ROOT权限),无法继续操作,请使用${GREEN_BACKGROUND_PREFIX} sudo su ${FONT_COLOR_SUFFIX}来获取临时ROOT权限(执行后会提示输入当前账号的密码)。" && exit 1
}
CHKECK_IP () {
CHECK_STEP1=`echo $1 | awk -F"." '{print NF}'`
if [ $CHECK_STEP1 -eq 4 ] || [ $CHECK_STEP1 -eq 6 ]
then
CHECK_STEP2=`echo $1 | awk -F"." '{if ($1!=0 && $NF!=0) split ($0,IPNUM,".")} END
{ for (k in IPNUM) if (IPNUM[k]==0) print IPNUM[k]; else if (IPNUM[k]!=0 && IPNUM[k]!~/[a-z|A-Z]/ && length(IPNUM[k])<=3 &&
IPNUM[k]<255 && IPNUM[k]!~/^0/) print IPNUM[k]}'| wc -l`
if [ $CHECK_STEP2 -eq $CHECK_STEP1 ]
then
echo 0
else
echo 1
fi
else
echo 1
fi
}
RollBACK() {
cp $BAK_PATH/grub2.${DATE_NEW} $GRUB_FILE2
BAK_NETWORK=`ls -lrth $BAK_PATH | grep ifcfg | tail -1 | awk '{print $NF}'`
NEW_NETWORK=`ls -lrth $BAK_PATH | awk '/ifcfg-ens/{print $NF}' | awk -F'.' '{print $1}' | tail -1`
cp $BAK_PATH/$BAK_NETWORK $FILE_PATH/$NEW_NETWORK
mv -bf $FILE_PATH/ifcfg-eth0 /tmp >/dev/null 2>&1
echo "Script done!"
}
echo "是否修改以下网卡配置文件:
$FILE_NAMES
"
echo && stty erase '^H' && read -p "continue?(y继续/b还原/*退出): " YN
case $YN in
y)
CHECK_ROOT
CHECK_IPADDR=`CHKECK_IP $IPADDR`
if [ $CHECK_IPADDR -eq 1 ]
then
echo "这个 $IPADDR 不是正确合法的有效IP !"
exit 110
fi
for NAME in $FILE_NAMES; do
PD=`grep "" $FILE_PATH/$NAME | wc -l`
if [[ $PD -eq 0 ]]; then
continue
fi
DEVN=`grep "NAME" $FILE_PATH/$NAME | awk -F'=' '{print $2}'`
cp $FILE_PATH/$NAME $BAK_PATH/${NAME}.${DATE_NEW}
#cp $GRUB_FILE $BAK_PATH/grub.${DATE_NEW}
cp $GRUB_FILE2 $BAK_PATH/grub2.${DATE_NEW}
trap 'onCtrlC' INT
function onCtrlC () {
echo '检测到用户按下Ctrl+C尝试终止脚本,正在进行回滚...'
RollBACK
exit 110
}
sed -i "s/$DEVN/eth0/g" $FILE_PATH/$NAME
sed -i "/UUID/d" $FILE_PATH/$NAME
mv $FILE_PATH/$NAME $FILE_PATH/ifcfg-eth0
#sed -i '/rhgb quiet/s/.$//' $GRUB_FILE
#sed -i '/rhgb quiet/s/$/ net.ifnames=0 biosdevname=0 /' $GRUB_FILE
#sed -i '/rhgb quiet/s/.$/"/' $GRUB_FILE
sed -i '/rhgb quiet/s/.$//' $GRUB_FILE2
sed -i '/rhgb quiet/s/$/ net.ifnames=0 biosdevname=0 /' $GRUB_FILE2
sed -i '/rhgb quiet/s/.$/"/' $GRUB_FILE2
echo ""
echo && stty erase '^H' && read -p "重新生成GRUB...(y/n): " NG
case $NG in
y)
grub2-mkconfig -o $GRUB_CFG
echo "正在准备重启..."
IFNAMES=`grep "ifnames" $GRUB_CFG | wc -l`
if [[ ! $IFNAMES -ge 2 ]]; then echo "$GRUB_CFG 异常,请手动检查,正在进行回滚..." ; RollBACK ; grub2-mkconfig -o $GRUB_CFG; exit 110; fi
shutdown -r 1
trap 'onCtrlC' INT
function onCtrlC () {
echo '检测到用户按下Ctrl+C尝试终止脚本,正在取消重启...'
shutdown -c
exit 110
}
for ((s=59;s>0;s--))
do
[ $s -gt 0 -a $s -lt 10 ] && echo "距离关机还有 0$s 秒" || echo "距离关机还有 $s 秒"
sleep 1
clear
done
;;
*)
echo "Input Error! "
echo "Script Exit... "
exit 110
;;
esac
done
;;
b)
RollBACK
grub2-mkconfig -o $GRUB_CFG
exit 110
;;
*)
exit 110
;;
esac