shell脚本自动修改centos7网口名称eth0并配置ip
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
这个报错是啥原因呢