Centos7自动修改网卡名称脚本

scadm 2021年4月25日00:49:17
评论
689 3901字

用过Centos7的同学都会发现上面的网卡命名规则是不规范的,对于习惯了之前的eth或em的命名难免想要修改回去。

最近公司在定制Centos7时就提了这个需求,于是就写了个脚本在安装系统时运行脚本自动修改网卡名称。

#!/bin/bash
# Script Name:NICs-Setting.sh 
# Support OS Type: RHEL/CentOS 5.x/6.x/7.x
function V5()
{
#local b0=-1
#local c0
#sed -i '/HWADDR/d' `grep HWADDR -l /etc/sysconfig/network-scripts/ifcfg-eth?`
[ ! -d /etc/sysconfig/network-scripts/NIC_Backup ] && mkdir /etc/sysconfig/network-scripts/NIC_Backup
b0=-1
cat /proc/net/dev |grep ':' | grep -v 'lo' | grep -v 'bond' |grep -v 'sit0' | cut -d: -f1 |sort > /tmp/net_name.txt
while read line
do
 c0=$line
 b0=`expr $b0 + 1`
 cp -f /etc/sysconfig/network-scripts/ifcfg-$c0 /etc/sysconfig/network-scripts/NIC_Backup &> /dev/null
 mv -f /etc/sysconfig/network-scripts/ifcfg-$c0 /etc/sysconfig/network-scripts/ifcfg-eth$b0 &> /dev/null
 sed -i '$a BOOTPROTO=static' /etc/sysconfig/network-scripts/ifcfg-eth$b0
 sed -i '$a NAME=eth'$b0'' /etc/sysconfig/network-scripts/ifcfg-eth$b0
 sed -i '/HWADDR/d' `grep HWADDR -l /etc/sysconfig/network-scripts/ifcfg-eth$b0`
done < /tmp/net_name.txt
cp $V5NetRuleFile $V5NetRuleFile-`date +%Y-%m-%d-%R`
> $V5NetRuleFile
local number=0
lshw -C network | grep "bus info" | awk -F : '{print $3":"$4}' | sort | while read line
do
 c0=$line
 echo 'ACTION=="add", SUBSYSTEM=="net", BUS=="pci", ID=="0000:'$c0'", NAME="eth'$number'"' >>/etc/udev/rules.d/60-net.rules
 let number++
done
# 
}
function V6()
{
 chmod a-x $V6WriteRuleCmd
 local number=0
 cp $V6NetRuleFile $V6NetRuleFile-`date +%Y-%m-%d-%R`
 > $V6NetRuleFile
 lshw -C network | grep "bus info" | awk -F : '{print $3":"$4}' | sort | while read line
 do
 echo "SUBSYSTEMS=="pci", ACTION=="add", DRIVERS=="?*", KERNELS=="0000:$line", NAME="eth$number"" >> $V6NetRuleFile
 let number++
 done
}
function V7()
{
 [ ! -d /etc/sysconfig/network-scripts/NIC_Backup ] && mkdir /etc/sysconfig/network-scripts/NIC_Backup
 b0=-1
 cat /proc/net/dev |grep ':' | grep -v 'lo' | grep -v 'bond' | cut -d: -f1 |sort > /tmp/net_name.txt
 while read line
 do
 c0=$line
 b0=`expr $b0 + 1`
 cp -f /etc/sysconfig/network-scripts/ifcfg-$c0 /etc/sysconfig/network-scripts/NIC_Backup &> /dev/null
 mv -f /etc/sysconfig/network-scripts/ifcfg-$c0 /etc/sysconfig/network-scripts/ifcfg-eth$b0 &> /dev/null
 sed -i 's/dhcp/static/g' /etc/sysconfig/network-scripts/ifcfg-eth$b0
 sed -i '/IPV6/d' /etc/sysconfig/network-scripts/ifcfg-eth$b0
 sed -i 's/'$c0'/eth'$b0'/g' /etc/sysconfig/network-scripts/ifcfg-eth$b0
 done < /tmp/net_name.txt
 sed -i '/GRUB_CMDLINE_LINUX/d' /etc/default/grub
 sed -i '$a GRUB_CMDLINE_LINUX="crashkernel=auto net.ifnames=0 biosdevname=0 rhgb quiet"' /etc/default/grub
 grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null
 local number=0
 cp $V7NetRuleFile $V7NetRuleFile-`date +%Y-%m-%d-%R`
 > $V7NetRuleFile
 lshw -C network | grep "bus info" | awk -F : '{print $3":"$4}' | sort | while read line
 do
 echo "SUBSYSTEMS=="pci", ACTION=="add", DRIVERS=="?*", KERNELS=="0000:$line", NAME="eth$number"" >>$V7NetRuleFile
 let number++
 done
}
# Main Start here.
#
PACKAGE1=redhat-lsb
V7PACKAGE=redhat-lsb-core
PACKAGE2=pciutils
V5NetRuleFile=/etc/udev/rules.d/60-net.rules
V6NetRuleFile=/etc/udev/rules.d/70-persistent-net.rules
V7NetRuleFile=/etc/udev/rules.d/70-persistent-net.rules
V6WriteRuleCmd=/lib/udev/write_net_rules
ErrUID=65
ErrPackage=66
ErrOSType=67
ErrOSVersion=68
OSType=`lsb_release -i | grep -E 'RedHat|CentOS'`
OSRelease=`lsb_release -r | awk '{print $2}'`
if [ $UID -ne 0 ]; then
 echo "The script must be execute by root"
 echo "Exiting now......"
 exit $ErrUID
fi
if [ "${OSRelease%%.*}"x == 7x ]; then
 rpm -q $V7PACKAGE &>/dev/null && rpm -q $PACKAGE2 &>/dev/null
 if [ $? -ne 0 ]; then
 echo "Please install the "$V7PACKAGE" and "$PACKAGE2" packages fir"
 echo "Exiting now......"
 fi
else
rpm -q $PACKAGE1 &>/dev/null && rpm -q $PACKAGE2 &>/dev/null
 if [ $? -ne 0 ]; then
 echo "Please install the "$PACKAGE1" and "$PACKAGE2" packages first, then execute the script."
 echo "Exiting now......"
 exit $ErrPackage
 fi
fi
if [ "$OSType" == "" ]; then
 echo "Unsupport OS type, the script only support OS type: RHEL/CentOS."
 exit $ErrOSType
else
 case $OSRelease in
 5.*) V5 ;;
 6.*) V6 ;;
 7.*) V7 ;;
 *) echo "Unsupport OS release version, the script only support release version: 5.x/6.x/7.x."
 echo "Exiting now..."
 exit $ErrOSVersion
 ;;
 esac
fi
echo -e '
//------------------------------
//tNetwork setting
//------------------------------
'
echo "Execute the scripts success, the NICs are in line!"
继续阅读
scadm
  • 本文由 发表于 2021年4月25日00:49:17
  • 转载请务必保留本文链接:https://www.wscyun.com/1190
Hadoop 节点扩容 运维

Hadoop 节点扩容

配置好hosts解析 # cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdom...
hadoop 分布式存储部署 运维

hadoop 分布式存储部署

Hadoop概念 Hadoop实现了一个分布式文件系统( Distributed File System),其中一个组件是HDFS。HDFS有高容错性的特点,并且设计用来部署在低廉的(low-cost...
CEPH分布式存储 运维

CEPH分布式存储

ceph简介: Ceph是一种为优秀的性能、可靠性和可扩展性而设计的统一的、分布式文件系统。ceph 的统一体现在可以提供文件系统、块存储和对象存储,分布式体现在可以动态扩展。在国内一些公司的云环境中...
docker容器部署 Docker

docker容器部署

查找镜像文件 # 在Docker官方仓库中搜索Nginx镜像,Nginx镜像文件中只包含Nginx软件程序、数据文件 # docker search nginx NAME DESCRIPTION ST...