一、 赋予可执行权限
chmod x /etc/rc.d/rc.local
二、 编辑启动文件
vim /etc/rc.d/rc.local
三、 在 /etc/rc.d/rc.local 中 加入 自己的执行脚本 & 后台运行
sh /opt/ping.sh &
代码语言:javascript复制#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
#touch /var/lock/subsys/local
sh /opt/ping.sh &
四、脚本如下
代码语言:javascript复制#!/bin/bash
ip=www.baidu.com
ping -c 2 -w 3 -i 0.3 $ip &>/dev/null
if [ $? -eq 0 ]
then
echo " 可以ping的通百度"
else
echo "正在更改你的网卡"
sed -i '/^IPADDR=/cIPADDR=192.168.110.132' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i '/^GATEWAY=/cGATEWAY=192.168.110.2' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i '/^DNS1=/cDNS1=8.8.8.8' /etc/sysconfig/network-scripts/ifcfg-ens33
echo "网卡配置文件已改完 正在重启网络服务"
systemctl restart network
fi
ping -c 2 $ip &>/dev/null
if [ $? -eq 0 ] ;then
echo "一切准备就绪"
else
echo "请检查你绑定的网卡是不是vm8"
fi