首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Centos7更改网卡名为eth0

Centos7更改网卡名为eth0

作者头像
院长技术
发布于 2020-06-13 11:41:24
发布于 2020-06-13 11:41:24
1.4K0
举报
文章被收录于专栏:院长运维开发院长运维开发

仅适用于CentOS7,慎用

代码语言:javascript
复制
#!/bin/bash
array=($(ls /etc/sysconfig/network-scripts))
path=/etc/sysconfig/network-scripts/
name=ifcfg-eth0
file="`echo ${array[*]} | grep -o 'ifcfg-ens[[:digit:]]\+'`"

judge() {
  if [ -f $path$file ]; then
    echo "the net name is not modified"
  else
    echo "$path$file was modified"
    exit
  fi
}

DM() {
  if [ `grep -o 'DEVICE' $path$file | wc -l` -eq 1 ]; then
    sed -i 's/DEVICE=.*/DEVICE="eth0"/' $path$file &>/dev/null
  fi
}

NM() {
  if [ `grep -o 'NAME' $path$file | wc -l` -eq 1 ]; then
    sed -i 's/NAME=.*/NAME="eth0"/' $path$file
  fi
}

other() {
  mv $path$file $path$name
  if [ -f "/etc/default/grub" ]; then
    sed -i '/^GRUB_CMDLINE_LINUX/c \GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root "net.ifnames=0 biosdevname=0" rd.lvm.lv=centos/swap rhgb quiet"' /etc/default/grub
    grub2-mkconfig -o /boot/grub2/grub.cfg &>/tmp/1.txt
    if [ `grep 'done' /tmp/1.txt | wc -l` -eq 1 ]; then
      echo "禁止可预测命名规则成功。"
    else
      echo "禁止可预测命名规则失败。"
    fi
    cat /dev/null >/tmp/1.txt
  fi
}

read -p "Do you want to modify the net name: {yes|no} " choice
case $choice in
  yes)
    judge
    sleep 1
    DM
    sleep 1
    NM
    sleep 1
    other
    sleep 3
    echo "$name was modified,please restart your linux" ;;
  no)
    echo "you will quit."
    sleep 3 ;;
  *)
    echo "Please input {yes|no}"
esac
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档