shell实现centos7双网卡修改网卡名eth0,eth1,并设置网络

时间:2021-09-20 17:19:29
#!/bin/bash

interface1=`ls /sys/class/net|grep en|awk 'NR==1{print}'`
interface2=`ls /sys/class/net|grep en|awk 'NR==2{print}'` interface_file1="/etc/sysconfig/network-scripts/ifcfg-$interface1"
interface_file2="/etc/sysconfig/network-scripts/ifcfg-$interface2" echo $interface_file1
echo $interface_file2 while getopts "i:g:n:I:G:N:" opt; do
case $opt in
i)
ip1=$OPTARG
;;
g)
gateway1=$OPTARG
;;
n)
netmask1=$OPTARG
;;
I)
ip2=$OPTARG
;;
G)
gateway2=$OPTARG
;;
N)
netmask2=$OPTARG
;; \?)
;;
esac
done
#判断grub文件是否修改net.ifname信息
new_grub=`cat /etc/default/grub|grep -c "net.ifnames"` if [ $new_grub -eq 0 ];then sed -i '1,$s/quiet/quiet net.ifnames=0 biosdevname=0/g' /etc/default/grub fi grub2-mkconfig -o /boot/grub2/grub.cfg mv $interface_file1 /etc/sysconfig/network-scripts/ifcfg-eth0
mv $interface_file2 /etc/sysconfig/network-scripts/ifcfg-eth1 cat >/etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=$ip1
GATEWAY=$gateway1
NETMASK=$netmask1
DNS1=114.114.114.114
EOF cat >/etc/sysconfig/network-scripts/ifcfg-eth1 <<EOF
TYPE=Ethernet
BOOTPROTO=static
NAME=eth1
DEVICE=eth1
ONBOOT=yes
IPADDR=$ip2
GATEWAY=$gateway2
NETMASK=$netmask2
DNS1=114.114.114.114
EOF reboot

  执行格式 : networksetting.sh -i 192.168.*.* -g 192.168.*.1 -n 255.255.255.0 -I 192.168.*.* -G 192.168.*.1 -N 255.255.255.0