shell脚本——一键完成虚拟机初始化

新装的虚拟机总是没有vim,没有wget,使用起来特别的不方便,因此,我自己写了一个shell脚本,用来初始化虚拟机,亲测简单有效

#!/usr/bin/env bash
#
#Author:campnou
#Email:[email protected]
#Date:2020/2/24
#This is a script for initializing a new machine

#关闭防火墙和selinux
systemctl stop firewalld
systemctl disable firewalld &> /dev/null
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0

#安装wget
echo "正在安装wget,请稍候......"
/bin/yum -y install wget &> /dev/null
if [ $? -eq 0 ]
then
echo "wget 已经安装成功!"
else
exit
echo "wget 安装失败"
fi

#将yum源改为阿里源,并安装epel源
echo "正在更改yum源,请稍候......"
/usr/bin/wget -O /etc/yum.repos.d/Centos-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
/bin/wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &> /dev/null
if [ $? -eq 0 ]
then
echo "基础源和扩展源已经更改为阿里源"
else
exit
echo "基础源安装失败"
fi

#安装ntp、vim、net-tools
echo "正在安装基础工具包,请稍候......"
/bin/yum -y install ntp vim  lsof  net-tools yum-utils &>/dev/null
if [ $? -eq 0 ]
then
echo "基础工具包已经安装完毕"
else
exit
echo "基础工具包安装失败"
fi

#添加用户
read -p "请输入你要添加的用户名:" user1
/usr/sbin/useradd $user1
echo "123456" | passwd --stdin $user1
echo "用户添加成功,默认初始密码为123456"

#修改ssh的默认端口
sed -i 's/#Port 22/Port 22/g' /etc/ssh/sshd_config
echo "Port 100" >> /etc/ssh/sshd_config
usermod -G wheel $user1
sed -i 's/#auth           required        pam_wheel.so use_uid/auth           required        pam_wheel.so use_uid/g' /etc/pam.d/su
echo "SU_WHEEL_ONLY yes" >> /etc/login.defs
systemctl restart sshd
echo "正在安装ELRepo,请稍候......"
rpm --import http://www.elrepo.org/RPM-GPG-KEY-elrepo.org &>/dev/null
yum -y install https://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm &>/dev/null
if [ $? -eq 0 ]
then
echo "安装ELRepo成功"
fi
yum --disablerepo="*" --enablerepo="elrepo-kernel" list available -y |tail -8
echo "正在安装kernel-ml,请稍候......"
yum  --enablerepo=elrepo-kernel -y install kernel-ml &>/dev/null
if [ $? -eq 0 ]
then
echo "安装kernel-lt成功"
fi
sed -i 's/GRUB_DEFAULT=saved/GRUB_DEFAULT=0/g' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg &>/dev/null
echo "升级内核成功,请重启您的机器"
echo -e "\e[1;36m 所有配置已完成,请您不要忘记修改用户默认密码 \e[0m"

你可能感兴趣的:(shell脚本——一键完成虚拟机初始化)