设置Linux服务器IP地址的shell脚本

设置Linux服务器IP地址的shell脚本

#!/bin/bash
# 设置LinuxIP
echo "------SET IP NETMASK GATEWAY-----"
read -p "IPADDR: " gkAdd
read -p "NETMASK: " gkMask
read -p "GATEWAY: " gkWay

cp /etc/sysconfig/network-scripts/ifcfg-enp1s0 /etc/sysconfig/network-scripts/ifcfg-enp1s0.bak
cp /etc/sysconfig/network-scripts/ifcfg-enp2s0 /etc/sysconfig/network-scripts/ifcfg-enp2s0.bak

sed -i 's/BOOTPROTO=dhcp/BOOTPROTO=static/' /etc/sysconfig/network-scripts/ifcfg-enp1s0
sed -i 's/BOOTPROTO=dhcp/BOOTPROTO=static/' /etc/sysconfig/network-scripts/ifcfg-enp2s0
sed -i 's/ONBOOT=no/ONBOOT=yes/' /etc/sysconfig/network-scripts/ifcfg-enp1s0
sed -i 's/ONBOOT=no/ONBOOT=yes/' /etc/sysconfig/network-scripts/ifcfg-enp2s0

# 设置ifcfg-enp1s0网卡
echo "IPADDR="${
     gkAdd} >> /etc/sysconfig/network-scripts/ifcfg-enp1s0
echo "NETMASK="${
     gkMask} >> /etc/sysconfig/network-scripts/ifcfg-enp1s0
echo "GATEWAY="${
     gkWay} >> /etc/sysconfig/network-scripts/ifcfg-enp1s0
echo "DNS1=10.188.0.55" >> /etc/sysconfig/network-scripts/ifcfg-enp1s0
echo "DNS2=10.188.0.56" >> /etc/sysconfig/network-scripts/ifcfg-enp1s0

# 设置ifcfg-en2s0网卡
echo "IPADDR="${
     gkAdd} >> /etc/sysconfig/network-scripts/ifcfg-enp2s0
echo "NETMASK="${
     gkMask} >> /etc/sysconfig/network-scripts/ifcfg-enp2s0
echo "GATEWAY="${
     gkWay} >> /etc/sysconfig/network-scripts/ifcfg-enp2s0
echo "DNS1=10.188.0.55" >> /etc/sysconfig/network-scripts/ifcfg-enp2s0
echo "DNS2=10.188.0.56" >> /etc/sysconfig/network-scripts/ifcfg-enp2s0

# 重启网卡
systemctl restart network

ping -c5 ${
     gkWay} >/dev/null 2>&1
if [ $? -eq 0 ]
then
  echo "netWork OK."
else
  echo "netWork failed,try again."
fi

你可能感兴趣的:(设置Linux服务器IP地址的shell脚本)