You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shell-project/os-test/系统初始化实例脚本.sh

200 lines
5.8 KiB

2 years ago
#!/usr/bin/bash
# <<! 中间的是shell的多行注释内容 !
<<!
Author: newrain
Demand: CentOS7系统初始化
Date: 2022/4/18
!
# 0 为远程仓库 1为本地仓库(需要确定本地存在仓库地址)
localYum=0
# localYum=0时配置无效
localYumUrl="http://download.beyourself.org.cn/local_yum.sh"
# 网卡名称,此处可接受数组,进行多网卡配置 (ens33 ens34 ens35)
netInterface="ens33 ens36 ens37"
# 主机名称, 重新登录生效
HOSTNAME=localhost
# 安装软件包
yumList="vim wget curl net-tools ntpdate yum-utils bash-completion psacct bind-utils"
# 输出间隔时间
sleepTime=1
# 输出格式化
function colorTools(){
declare -A color=([SUCCESS]="32" [ERROR]="31" [WARNING]="33" [INFO]="34")
printf "\033[%-2sm%-8s [%-5s] %-25s\033[0m\n" ${color[$1]} `date +"%T"` $1 "$2"
sleep $sleepTime
}
# 信号捕捉,退出时执行
function signal(){
colorTools ERROR "系统初始化失败,请检查系统配置"
exit 1
}
trap signal KILL INT
# 系统环境检测
function envCheck(){
# 网络检测
colorTools INFO "开始系统检测程序"
colorTools INFO "开始网络检测"
ping -c 1 -W 1 www.baidu.com > /dev/null 2>&1
if [ $? -ne 0 ];then
colorTools ERROR "网络连接失败"
exit 1
else
colorTools SUCCESS "网络检测成功"
fi
# 执行用户检测
colorTools INFO "开始用户检测"
if [ $UID -ne 0 ];then
colorTools ERROR "请使用root用户执行此脚本"
exit 1
else
colorTools SUCCESS "用户检测完成,当前用户为root"
fi
# 执行系统检测
colorTools INFO "开始系统检测"
if [ -f /etc/redhat-release ];then
colorTools SUCCESS "系统检测完成,当前系统为CentOS/RedHat"
else
colorTools ERROR "系统检测失败,仅支持CentOS/RedHat"
exit 1
fi
}
# 网络配置
function networkConfigure(){
systemctl stop NetworkManager &>/dev/null
systemctl disable NetworkManager &>/dev/null
systemctl enable network &>/dev/null
for i in ${netInterface};do
colorTools INFO "开始网络配置,网卡名称 $i"
address=$(ip a show dev $i | grep -oP "[1-9][\d]{1,2}(\.\d{1,3}){3}/(\d+)")
ipaddr=$(echo $address | awk -F'/' '{print $1}')
netmask=$(echo $address | awk -F'/' '{print $2}')
gateway=$(ip r show dev $i | grep -oP "default via \K[1-9][\d]{1,2}(\.\d{1,3}){3}")
cat > /etc/sysconfig/network-scripts/ifcfg-$i <<EOF
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
NAME="$i"
IPADDR="${ipaddr}"
GATEWAY="${gateway}"
PREFIX=${netmask}
DNS1=114.114.114.114
DNS2=8.8.8.8
UUID="`uuidgen`"
DEVICE="$i"
ONBOOT="yes"
EOF
colorTools SUCCESS "网络配置完成"
done
}
# yum源配置
function yumConfigure(){
colorTools INFO "开始yum配置"
cd /etc/yum.repos.d/ && rename .repo .repo.bak *.repo &>/dev/null
set -e
if [ $localYum -eq 1 ];then
curl -s download.beyourself.org.cn/yum.sh | bash
colorTools SUCCESS "配置本地源完成,请在/etc/hosts文件中修改本地源ip地址解析"
elif [ $localYum -eq 0 ];then
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
fi
set +e
colorTools SUCCESS "yum 源配置完成"
}
# 系统常规配置
function other(){
colorTools INFO "开始常规配置"
colorTools INFO "开始防火墙配置"
systemctl stop firewalld && \
systemctl disable firewalld
colorTools INFO "开始selinux配置"
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config && \
setenforce 0
colorTools INFO "开始安装常用软件 ${yumList}"
yum install -y ${yumList}
colorTools INFO "开始主机名配置 ${HOSTNAME}"
hostnamectl set-hostname $HOSTNAME
colorTools INFO "开始时区配置并进行时间校准"
timedatectl set-timezone Asia/Shanghai && \
ntpdate ntp.aliyun.com
colorTools SUCCESS "常规配置完成"
}
# 系统参数优化
function optimized(){
colorTools INFO "开始优化系统"
colorTools INFO "开始优化内核参数"
cat > /etc/sysctl.conf <<EOF
# 在FIN-WAIT-2状态的时间,默认值是60秒
net.ipv4.tcp_fin_timeout = 1
# 在第一次keep alive请求发送后,不活动连接的时间,单位为秒,默认值为18000
net.ipv4.tcp_keepalive_time = 1200
# 在第一次keep alive请求发送后,不活动连接的确认次数,默认值为9
net.ipv4.tcp_keepalive_probes = 3
# 开启路由转发
net.ipv4.ip_forward = 1
# 开启反向路由
# net.ipv4.conf.all.rp_filter = 1
EOF
sysctl -p
colorTools INFO "开始优化系统参数"
cat > /etc/security/limits.conf <<EOF
* soft nofile 102400
* hard nofile 102400
EOF
colorTools SUCCESS "优化系统完成"
}
for i in "$@";do
if [[ "$i" =~ '--' ]];then
varName=${i%=*}
varName=${varName#--}
varValue=${i#*=}
eval "$varName='$varValue'"
fi
done
envCheck
case $1 in
"main")
yumConfigure
other
optimized
networkConfigure
;;
"networkConfigure")
networkConfigure
;;
"other")
other
;;
"optimized")
optimized
;;
"yumConfigure")
yumConfigure
;;
*)
# 脚本后面带参数,只执行指定内容
echo -e "\tUsage: $0 [main|networkConfigure|other|optimized|yumConfigure] [options] 执行[全部功能|网络配置|其他配置|优化系统|yum配置]"
# 修改配置
echo -e "\toptions: \n\t--localYum=0/1, 0为阿里云源,1为本地源\n\t--netInterface='ens33 ens36 ens37' 网络接口,多个接口使用数组\n\t--HOSTNAME='localhost' 主机名\n\t--yumList='vim wget' 安装软件数组,多个软件使用数组"
exit 0
;;
esac
colorTools SUCCESS "$1 配置完成, 正在进行网络重启"
systemctl restart network