前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Centos7更改网卡名为eth0

Centos7更改网卡名为eth0

作者头像
院长技术
发布2020-06-13 11:41:24
9260
发布2020-06-13 11:41:24
举报

仅适用于CentOS7,慎用

#!/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 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档