1、找到网卡配置文件名
ls /etc/sysconfig/network-scripts/
2、备份并查看原始配置文件(若原先有配置IP的,则按照第五点方式修改)
3、修改随机自启和IP地址
echo -e "IPADDR=192.168.43.12 \nNETMASK=255.255.255.0 \nGATEWAY=192.168.43.1" >> /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i "s/BOOTPROTO=dhcp/BOOTPROTO=static/" /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i "s/ONBOOT=no/ONBOOT=yes/" /etc/sysconfig/network-scripts/ifcfg-ens33
4、重启网卡生效
/etc/init.d/network restart
5、若原先有IP地址的,则按照以下方式修改
5.1 注释掉原先的IP选项
sed -i "s/IPADDR/#IPADDR/" /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i "s/NETMASK/#NETMASK/" /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i "s/GATEWAY/#GATEWAY/" /etc/sysconfig/network-scripts/ifcfg-ens33
5.2 追加新的IP并修改开机自启
echo -e "IPADDR=192.168.43.12 \nNETMASK=255.255.255.0 \nGATEWAY=192.168.43.1" >> /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i "s/BOOTPROTO=dhcp/BOOTPROTO=static/" /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i "s/ONBOOT=no/ONBOOT=yes/" /etc/sysconfig/network-scripts/ifcfg-ens33
5.3 重启网卡/etc/init.d/network restart