Rocky9 Linux 修改网卡名称 ensxx 为 eth0

#!/bin/bash
##author xc
##2023-05-10
function chk_base()
{
clear
date
echo "############################################"
echo "#                                          #"
echo "# Rocky9 Linux 修改网卡名称 ensxx 为 eth0   #"
echo "#                                          #"
echo "############################################"

for i in `seq -w 2 -1 0`
do
echo -ne "\b\b\b\b$i 秒"
sleep 1
done
echo ""
MYVER=`cat /etc/redhat-release |awk '{print $4}'| awk -F [.] '{print $1}'`
expr 1 + $MYVER &>/dev/null
if [ $? -ne 0 ];then
   echo "请在 Rocky 9 版本的系统里面运行该脚本....."
exit
fi
if [ $UID -ne 0 ]; then
   echo "请用 root 用户运行该脚本...."
   exit
fi
}
function change_eth {
NETFILE_DIR="/etc/NetworkManager/system-connections"
NETFILE_SUM=`find $NETFILE_DIR -name "ens*.nmconnection"|sort -n`
ETHFILE_SUM=`find $NETFILE_DIR -name "eth*.nmconnection"|wc -l`
if [ $ETHFILE_SUM -ge 1 ];then
   echo "网卡名称已修改。"
   ls $NETFILE_DIR/eth*
   exit
else
   echo "开始修改网卡名称......"
j=0
for i in $NETFILE_SUM
do
NETNAME=`cat $i |grep "interface-name" |awk -F '=' '{print $2}'`
sed -i "s#$NETNAME#eth$j#g" $i
mv $i $NETFILE_DIR/eth$j.nmconnection
j=`expr $j + 1`
done
sed -i 's/GRUB_CMDLINE_LINUX="/GRUB_CMDLINE_LINUX=" net.ifnames=0 biosdevname=0 /' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg

echo "网卡名称修改完成"
echo -e "The grub file has been modified. Please restart system.\n"
echo -e "You have 5 seconds to choose.\n"
read -p "Please select and press Enter. (y/n): " -t 5 READY

if [[ $READY == 'y' ]];then
   echo "System is restarting....."
   init 6
elif [[ $READY == 'n' ]];then
   echo "Please restart the system by yourself."
   exit
else
   echo -e "\nInput error or no input. \nPlease restart the system by yourself."
   exit
fi
fi
}
chk_base
change_eth