(1) 将动态ip修改为静态ip的思路
②进入网卡配置目录 /etc/sysconfig/network-scripts/ifcfg-eth0
③ 修改BOOTPROTO=static
④ 修改ONBOOT=yes
⑤ 配置静态ip地址 IPADDR
⑥ 配置DNS NETMASK
⑦ 配置网关 GATWAY
⑧ 重启网卡验证配置,验证配置结果 service network restart
(2) 将静态ip修改为其它静态ip的思路
☆ 先判断是否是静态ip grep "dhcp" /etc/sysconfig/network-scripts/ifcfg-eth0/
☆
if[$? -ne 0]; then
sed -i `s/^IPADDR/#IPADDR/g` ifcfg-eth0
read -p "please Enter ip:"IPADDR
echo "IPADDR="$IPADDR">>/etc/sysconfig/network-scripts/
fi
☆ 重启网卡验证配置结果 service network restart
代码示例:
#!/bin/bash #2017年12月17日19:34:40 #by author daqi #change ip shell NET_FILE="/etc/sysconfig/network-scripts" NET_DIR="ifcfg-eth0" cd $NET_FILE/ #change ip static for static: grep "dhcp" $NET_FILE/ $NET_DIR if [ $? -ne 0 ];then sed -i s/^IPADDR/#IPADDR/g $NET_DIR read -p "Please enter ip Address,example 192.168.0.11 ip": IPADDR echo "IPADDR=$IPADDR">>$NET_FILE/$NET_DIR service network restart else #change ip dhcp for static sed -i s/dhcp/static/g $NET_DIR sed -i s/ONBOOT=no/ONBOOT=yes/g $NET_DIR read -p "Please enter ip Address,example 192.168.0.11 ip": IPADDR cat>>$NET_FILE/$NET_DIR <<EOF IPADDR=$IPADDR NETMASK=255.255.255.0 GATWAY=192.168.2.1 EOF service network restart fi |