shell脚本自动修改centos7网口名称eth0并配置ip

post by rocdk890 / 2020-2-19 14:16 Wednesday linux技术
2020年真是个不好的开始,还没有开始过年就出现这么大的疾病,呆在家里呆了这么多天实在是太无聊了,就写了几个脚本.
系统:centos 7.x(64位)

cat /root/soft_shell/network.sh
#!/bin/env bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
export PATH

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to initialization OS."
    exit 1
fi

read -p "Enter the IP address:" ipadd
read -p "Enter the gateway:" gate

function RENMAE_NIC()
{
	local _NICNAME=$(ip addr | grep "^2"|awk -F ": " '{print $2}')
	if [[ ${_NICNAME} != "eth0" ]];then
		echo -e "\e[31m The nic name is [\e[31;1m${_NICNAME}\e[0m].\n Press 'Enter' key rename nic name to 'eth0'.\e[0m"
		read -t 5
		echo "IPADDR=$ipadd
NETMASK=255.255.255.0
GATEWAY=$gate" >> /etc/sysconfig/network-scripts/ifcfg-${_NICNAME}
		sed -i "s/dhcp/static/g" /etc/sysconfig/network-scripts/ifcfg-${_NICNAME}
		sed -i s/"^NAME=.*$"/"NAME=eth0"/g /etc/sysconfig/network-scripts/ifcfg-${_NICNAME}
		sed -i s/"^DEVICE=.*$"/"DEVICE=eth0"/g /etc/sysconfig/network-scripts/ifcfg-${_NICNAME}
		mv /etc/sysconfig/network-scripts/ifcfg-${_NICNAME} /etc/sysconfig/network-scripts/ifcfg-eth0
		sed -i s/'rhgb quiet"$'/'net.ifnames=0 biosdevname=0 rhgb quiet"'/g /etc/default/grub
		grub2-mkconfig -o /boot/grub2/grub.cfg
		echo -e "\e[32m The nic name has renamed.\n Press 'Enter' key reboot.\e[0m"
		read -t 5
		reboot
	fi
	unset local _NICNAME
}
RENMAE_NIC
夜空- 本站版权
1、本站所有主题由该文章作者发表,该文章作者与夜空享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和夜空的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:blog.slogra.com/post-765.html

标签: centos shell ip 一键 自动 静态 eth0

  1. 2020-08-17 14:57
    @小番茄:你连上面这句都要抄的吗?
  1. gravatar 小番茄
    2020-08-04 11:09
    cat: /root/soft_shell/network.sh: No such file or directory
    这个报错是啥原因呢

评论: