Centos7修改网卡名称

核心思想是修改 udev rules,然后重新加载网卡驱动既可
配置文件: /etc/udev/rules.d/net.rules
网卡驱动: e1000 ,如果 不是e1000驱动,请手动重启,或者手动重新加载网卡驱动

运行完成后,请检查 配置文件 /etc/udev/rules.d/net.rules

#!/bin/bash
# rename ethernet device name

function netdevice_list(){
    echo "The ethernet device list : "
    echo "-------------------------"
    ip -bri link show | awk '{print $1}'
    echo "-------------------------"
}
function usage(){
    echo "Usage $(basename $0) {ethX} {newName}     change ethernet device name, You can chroose one from above list. "
    echo "      $(basename $0) reset                reset all devices name to system default."
    exit 1
}
function driver_reload(){
    modprobe -r e1000
    modprobe e1000
    echo "INFO:driver_e1000_reload"
}
function reset(){
    echo "" > /etc/udev/rules.d/net.rules
        driver_reload
    echo "Reset to system default"
        exit 0
}

case $1 in
    "reset")
        # reset /etc/udev/rules.d/net.rules to empty;
        reset
        ;;
    
    *)  
        if [ $# -eq 2 ];then 
            ip -c link show $1 &>/dev/null
            if [ $? -ne 0 ];then
                echo "Device $1 do not exsit!"
                    exit 1
            fi
            MACADDRESS=$(ip -bri link show $1| awk '{print $3}')
            test -e /etc/udev/rules.d/net.rules &&  sed -i "/$MACADDRESS/d" /etc/udev/rules.d/net.rules
            echo ''SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$MACADDRESS\", ATTR{type}==\"1\", NAME=\"$2\"'' >> /etc/udev/rules.d/net.rules
            driver_reload
        else    
            netdevice_list
            usage
        fi
    ;;
esac

你可能感兴趣的:(Centos7修改网卡名称)