CentOS修改网卡名eth0并交互式修改IP地址脚本
Posted 白-胖-子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS修改网卡名eth0并交互式修改IP地址脚本相关的知识,希望对你有一定的参考价值。
#!/bin/bash
# CentOS交互式修改网卡地址
CF="\\e[1;31m"
CE="\\e[0m"
centos_net_name(){
grep 'net.ifnames=0 biosdevname=0' /etc/default/grub &>/dev/null
if [ $? = 1 ];then
sed -ri '/GRUB_CMDLINE_LINUX=/s#(")$# net.ifnames=0 biosdevname=0"#' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
mv /etc/sysconfig/network-scripts/ifcfg-`ip a | grep -Eo "[0-9]:.*e.*:" | tr -d ' ' | cut -d: -f2` /etc/sysconfig/network-scripts/ifcfg-eth0
sed -ri '/NAME=/s#(".*")$#"eth0"#' /etc/sysconfig/network-scripts/ifcfg-eth0
sed -ri '/DEVICE=/s#(".*")$#"eth0"#' /etc/sysconfig/network-scripts/ifcfg-eth0
echo -e "$CF网卡名已经修改完成,重启电脑生效!$CE"
else
echo -e "$CF网卡名已经修改无需重新修改!$CE"
fi
}
centos_net_ip(){
while :;do
read -p "请输入要设置的网卡名称: " NET_NAME
{ nmcli connection | grep -E "\\b$NET_NAME\\b" >/dev/null; } && break || echo "请重新输入正确且存在>的网卡名称!"
done
while :;do
read -p "请输入要修改的IP地址: " IP
{ echo $IP | grep -E "^(([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$">/dev/null; } && break || echo "请输入正确的IP地址!"
done
while :;do
read -p "请输入子网掩码(0-32): " MASK
[ $MASK -le 32 ] && break || echo "输入错误请重新输入!"
done
while :;do
read -p "请输入网关: " GATEWAY
{ echo $GATEWAY | grep -E "^(([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$">/dev/null; } && break || echo "请输入正确的网关!"
done
nmcli connection modify ${NET_NAME} ipv4.addresses ${IP}/${MASK} ipv4.gateway ${GATEWAY} ipv4.dns 114.114.114.114 && nmcli connection reload >/dev/null && nmcli connection up $NET_NAME >/dev/null && echo "网卡地址配置完成!" || echo "未知错误!请检查!"
}
centos_net_name&¢os_net_ip
以上是关于CentOS修改网卡名eth0并交互式修改IP地址脚本的主要内容,如果未能解决你的问题,请参考以下文章