直接上代碼!
#!/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
/sbin/lspci | awk '/net/ || /less/ { print $1 }' | sort | while read line
do
c0=$line
b0=`expr $b0 + 1`
echo 'ACTION=="add", SUBSYSTEM=="net", BUS=="pci", ID=="0000:'$c0'", NAME="eth'$b0'"' >>/etc/udev/rules.d/70-persistent-net.rules
done
}
net
grub
rules
好了以上就是一段可以快速修改網卡名稱的shell代碼了,希望文章對大家有協助。