centos6 修改網路卡名指令碼

scm1911發表於2018-12-30

centos6 修改網路卡名指令碼

#!/bin/bash
#
#********************************************************************
#Author         : shchangming
#QQ             : 414945814
#Date           : 2016-12-27
#FileName       : change_ifname.sh
#URL            : https://www.cnblogs.com/shichangming
#Description    : This script is to change Ethernet name on centos6
#Copyright (C)  : 2018 All rights reserved
#********************************************************************

VERSION=`grep -Eo "[0-9]+" /etc/centos-release | head -1`

dev_list() {
    echo -e "\033[31mthe device list as following:\033[0m\n==========="
    ip a | grep mtu | awk -F "[ :]+" '{print $2}' | grep -v lo
    echo "==========="
}

change_name() {
    if [ ${VERSION} != "6" ];then
        echo "only support centos6";exit 
    fi
    
    dev_list
    
    read -p "please select a device:" OLD_NAME
    read -p "please input a new device name:" NEW_NAME
    
    /sbin/ifconfig ${OLD_NAME} &> /dev/null
    if [ $? -ne 0 ];then
        echo -e "\033[31mdevice [${OLD_NAME}] do not found\033[0m";exit
    fi
    
    DRIVER=`ethtool -i ${OLD_NAME} | awk -F "[: ]+" '/driver/{print $2}'`
    cd /etc/sysconfig/network-scripts/ && \
    sed -i "/DEVICE=/s/${OLD_NAME}/${NEW_NAME}/" ifcfg-${OLD_NAME}
    sed -i "/${OLD_NAME}/s/${OLD_NAME}/${NEW_NAME}/" /etc/udev/rules.d/70-persistent-net.rules
    mv ifcfg-${OLD_NAME} ifcfg-${NEW_NAME}
    modprobe -r $DRIVER
    modprobe $DRIVER
    /etc/init.d/network restart
}

change_name

本文連結:https://www.cnblogs.com/shichangming/p/10198063.html

相關文章