自己编写的脚本,很随意, 参考看一下就好,用的就是最简单的语法!
这里分三种情况(是根据ip的有没有配置)
情况一
如果你的网卡IP没有配好,用了的是NAT模式,且关了DHCP动态分布,无法直接配置,需要自己先手动配好虚拟机的IP!
我这里是配好ip能上外网执行的脚本
这里建议你复制粘贴的时候,在vim编辑器中选择粘贴模式,这样粘贴的格式不会出错,在命令行模式中输入 :set paste 即可进入到粘贴模式!
#!/bin/bash
#centos7: 初始化脚本
#auther:LESHENG# 防火墙设置echo "关闭防火墙和selinux中...."echosystemctl stop firewalld && systemctl disable firewalld &> /dev/null && setenforce 0 && sed -i '/^SELINUX/s/enforcing/disabled/' /etc/selinux/configif [ $? -eq 0 ];thenecho "防火墙和selinux已经成功关闭....."elseecho "防火墙和selnux关闭失败,请手动关闭!!!"fi
echo#外网检测
echo "正在检测网络是否能上外网......"echoping -c 2 www.baidu.com &> /dev/nullif [ $? -eq 0 ];thenecho "网络正常"elseecho "网络不可达!"fi
echo# 配置yum源-这里选用阿里源echo "配置yum源中....."
echoyum install -y wget &> /dev/nullif [ $? -ne 0 ];thenecho "wget 安装失败........."systemctl restart networkyum repolist &> /dev/nullsleep 2fimkdir -p /root/YUM_backupmv /etc/yum.repos.d/* /root/YUM_backupwget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/nullwget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &>/dev/nullyum clean all &>/dev/null && yum reppolist &>/dev/nullecho "你的yum源有:" $(ls /etc/yum.repos.d)sleep 2#配置主机名和host文件echo "正在配置你的主机名..."echoread -p "请输入你的主机名:" host hostname(){hostnamectl set-hostname $host}hostname host && echo -e "主机名设置成功!!"echo "正在配置你的hosts文件..."ip=$(ip a | grep ens33 |grep inet |awk '{print $2}' | awk -F"/" '{print $1}')echo "$ip $host" >> /etc/hostsecho "hosts配置完成!!!"# 安装基础软件包echo "安装基础软件包中....."echoyum install -y vim wget lrzsz net-tools gcc libpcap libpcap-devel yum_utils &>/dev/nullif [ $? -eq 0 ];thenecho "安装完成....."elseecho "安装失败..... "fi
# 时间同步
echoecho "时间同步中……"yum install -y ntpdate &> /dev/nullntpdate cn.pool.ntp.org &> /dev/nullfile=$(who | head -1 | cut -d" " -f1)echo "* */1 * * * /usr/sbin/ntpdate cn.pool.ntp.org" > /var/spool/cron/$fileif [ $? -eq 0 ];thenecho "时间同步成功!!!"echo "unset MAILCHECK" >> /etc/profilesource /etc/profile &> /dev/nullelseecho "时间同步失败!!!"fi
情况二
如果你是DHCP动态分配地址的,或者想以这种方法让虚拟机获取到ip的。这里可以修改你的ip为静态ip,且静态的ip为你动态获取的ip地址。
#!/bin/bash
#centos7: 初始化脚本
#auther:LESHENG# 防火墙设置echo "关闭防火墙和selinux中...."echosystemctl stop firewalld && systemctl disable firewalld &> /dev/null && setenforce 0 && sed -i '/^SELINUX/s/enforcing/disabled/' /etc/selinux/configif [ $? -eq 0 ];thenecho "防火墙和selinux已经成功关闭....."elseecho "防火墙和selnux关闭失败,请手动关闭!!!"fi#配置IP地址echo "开始配置你的ip....."
chack_ip () {sed -i.bak 's/BOOTPROTO="dhcp"/BOOTPROTO="none"/' /etc/sysconfig/network-cripts/ifcfg-ens33ip=`ip a | grep ens33 |grep inet |awk '{print $2}' | awk -F"/" '{print $1}'`net=255.255.255.0gate=`route -n |awk 'NR==3{print $2}'`dns=`cat /etc/resolv.conf |grep nameserver |awk '{print $2}'`
}chack_ip
(
cat <<EOF
IPADDR=$ip
NETMASK=$net
GATEWAY=$gate
DNS1=$dns
EOF
) >> /etc/sysconfig/network-scripts/ifcfg-ens33systemctl restart network if [ $? -eq 0 ];thenecho "ip 配置成功"elseecho "IP配置失败,请手动配置!"fi
sleep 2#外网检测
echo "正在检测网络是否能上外网......"echoping -c 2 www.baidu.com &> /dev/nullif [ $? -eq 0 ];thenecho "网络正常"elseecho "网络不可达!"fi
echo# 配置yum源-这里选用阿里源echo "配置yum源中....."
echoyum install -y wget &> /dev/nullif [ $? -ne 0 ];thenecho "wget 安装失败........."systemctl restart networkyum repolist &> /dev/nullsleep 2fimkdir -p /root/YUM_backupmv /etc/yum.repos.d/* /root/YUM_backupwget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/nullwget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &>/dev/nullyum clean all &>/dev/null && yum reppolist &>/dev/nullecho "你的yum源有:" $(ls /etc/yum.repos.d)sleep 2#配置主机名和host文件echo "正在配置你的主机名..."echoread -p "请输入你的主机名:" host hostname(){hostnamectl set-hostname $host}hostname host && echo -e "主机名设置成功!!"echo "正在配置你的hosts文件..."ip=$(ip a | grep ens33 |grep inet |awk '{print $2}' | awk -F"/" '{print $1}')echo "$ip $host" >> /etc/hostsecho "hosts配置完成!!!"# 安装基础软件包echo "安装基础软件包中....."echoyum install -y vim wget lrzsz net-tools gcc libpcap libpcap-devel yum_utils &>/dev/nullif [ $? -eq 0 ];thenecho "安装完成....."elseecho "安装失败..... "fi
# 时间同步
echoecho "时间同步中……"yum install -y ntpdate &> /dev/nullntpdate cn.pool.ntp.org &> /dev/nullfile=$(who | head -1 | cut -d" " -f1)echo "* */1 * * * /usr/sbin/ntpdate cn.pool.ntp.org" > /var/spool/cron/$fileif [ $? -eq 0 ];thenecho "时间同步成功!!!"echo "unset MAILCHECK" >> /etc/profilesource /etc/profile &> /dev/nullelseecho "时间同步失败!!!"fi
情况三
自定义自己的ip,在执行脚本时,手动式输入自己的ip的DNS模式
#!/bin/bash
#centos7: 初始化脚本
#auther:LESHENG# 防火墙设置echo "关闭防火墙和selinux中...."echosystemctl stop firewalld && systemctl disable firewalld &> /dev/null && setenforce 0 && sed -i '/^SELINUX/s/enforcing/disabled/' /etc/selinux/configif [ $? -eq 0 ];thenecho "防火墙和selinux已经成功关闭....."elseecho "防火墙和selnux关闭失败,请手动关闭!!!"fi# 配置IP地址
read -p "请输入你的IP地址(输错了可以按ctrl+backspace键删除):" ip
read -p "请输入你的掩码地址(输错了可以按ctrl+backspace键删除):" net
read -p "请输入你的网关地址(输错了可以按ctrl+backspace键删除):" gate
read -p "请输入你的DNS地址(输错了可以按ctrl+backspace键删除):" dnscat >> /etc/sysconfig/network-scripts/ifcfg-ens33 <<EOF
IPADDR=$ip
NETMASK=$net
GATEWAY=$gate
DNS1=$dns
EOFsystemctl restart network if [ $? -eq 0 ];thenecho "IP 配置成功"elseecho "IP 配置失败,请手动配置!!!"fi
sleep 2#外网检测
echo "正在检测网络是否能上外网......"echoping -c 2 www.baidu.com &> /dev/nullif [ $? -eq 0 ];thenecho "网络正常"elseecho "网络不可达!"fi
echo# 配置yum源-这里选用阿里源echo "配置yum源中....."
echoyum install -y wget &> /dev/nullif [ $? -ne 0 ];thenecho "wget 安装失败........."systemctl restart networkyum repolist &> /dev/nullsleep 2fimkdir -p /root/YUM_backupmv /etc/yum.repos.d/* /root/YUM_backupwget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/nullwget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &>/dev/nullyum clean all &>/dev/null && yum reppolist &>/dev/nullecho "你的yum源有:" $(ls /etc/yum.repos.d)sleep 2#配置主机名和host文件echo "正在配置你的主机名..."echoread -p "请输入你的主机名:" host hostname(){hostnamectl set-hostname $host}hostname host && echo -e "主机名设置成功!!"echo "正在配置你的hosts文件..."ip=$(ip a | grep ens33 |grep inet |awk '{print $2}' | awk -F"/" '{print $1}')echo "$ip $host" >> /etc/hostsecho "hosts配置完成!!!"# 安装基础软件包echo "安装基础软件包中....."echoyum install -y vim wget lrzsz net-tools gcc libpcap libpcap-devel yum_utils &>/dev/nullif [ $? -eq 0 ];thenecho "安装完成....."elseecho "安装失败..... "fi
# 时间同步
echoecho "时间同步中……"yum install -y ntpdate &> /dev/nullntpdate cn.pool.ntp.org &> /dev/nullfile=$(who | head -1 | cut -d" " -f1)echo "* */1 * * * /usr/sbin/ntpdate cn.pool.ntp.org" > /var/spool/cron/$fileif [ $? -eq 0 ];thenecho "时间同步成功!!!"echo "unset MAILCHECK" >> /etc/profilesource /etc/profile &> /dev/nullelseecho "时间同步失败!!!"fi
后面的两种情况,我没有执行试过,如有错误,欢迎一起讨论学习哈!!!
本文链接:https://my.lmcjl.com/post/961.html
展开阅读全文
4 评论