From fa23a487cce7256036d33b75d59aa1e1b92a7bca Mon Sep 17 00:00:00 2001 From: ZhuoQinghui <1302344380@qq.com> Date: Fri, 1 Mar 2024 15:30:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=B0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CentOS/7/all/centos-7-init | 241 +++++++++++++++++++++++++++++++++++++ 1 file changed, 241 insertions(+) create mode 100644 CentOS/7/all/centos-7-init diff --git a/CentOS/7/all/centos-7-init b/CentOS/7/all/centos-7-init new file mode 100644 index 0000000..738bdfe --- /dev/null +++ b/CentOS/7/all/centos-7-init @@ -0,0 +1,241 @@ +#!/bin/bash + +# 循环提示用户输入要设置的主机名,直到用户正常输入 +while true; do + read -p "请输入要设置的主机名: " hostname + if [[ -n "$hostname" ]]; then + break + fi +done + +# 提示用户输入要创建的管理员账户 +read -p "请输入要创建的管理员账户: " admin_user + +# 提示用户输入要创建的普通账户 +read -p "请输入要创建的普通账户: " user + +# 循环询问用户是否要将yum源修改为阿里云镜像,直到用户输入y或n,默认为n +while true; do + read -p "是否将yum源修改为阿里云镜像?(y/n,默认为n): " use_aliyun_mirror + if [[ -z "$use_aliyun_mirror" ]]; then + use_aliyun_mirror="n" + fi + if [[ "$use_aliyun_mirror" == "y" || "$use_aliyun_mirror" == "n" ]]; then + break + fi +done + +# 循环询问用户是否要更新系统,直到用户输入y或n,默认为n +while true; do + read -p "是否要更新系统?(y/n,默认为n): " update_system + if [[ -z "$update_system" ]]; then + update_system="n" + fi + if [[ "$update_system" == "y" || "$update_system" == "n" ]]; then + break + fi +done + +# 如果用户不更新系统,循环询问是否更新软件,直到用户输入y或n,默认为n +if [[ "$update_system" == "n" ]]; then + while true; do + read -p "是否要更新软件?(y/n,默认为n): " update_software + if [[ -z "$update_software" ]]; then + update_software="n" + fi + if [[ "$update_software" == "y" || "$update_software" == "n" ]]; then + break + fi + done +fi + +# 循环询问用户是否安装宝塔,直到用户输入y或n,默认为n +while true; do + read -p "是否安装宝塔?(y/n,默认为n): " install_bt + if [[ -z "$install_bt" ]]; then + install_bt="n" + fi + if [[ "$install_bt" == "y" || "$install_bt" == "n" ]]; then + break + fi +done + +# 循环询问用户是否安装jdk8,直到用户输入y或n,默认为n +while true; do + read -p "是否安装jdk8?(y/n,默认为n): " install_jdk8 + if [[ -z "$install_jdk8" ]]; then + install_jdk8="n" + fi + if [[ "$install_jdk8" == "y" || "$install_jdk8" == "n" ]]; then + break + fi +done + +# 循环询问用户是否安装docker,直到用户输入y或n,默认为n +while true; do + read -p "是否安装docker?(y/n,默认为n): " install_docker + if [[ -z "$install_docker" ]]; then + install_docker="n" + fi + if [[ "$install_docker" == "y" || "$install_docker" == "n" ]]; then + break + fi +done + +# 如果安装docker,循环询问是否配置阿里云镜像加速,直到用户输入y或n,默认为n +if [[ "$install_docker" == "y" ]]; then + while true; do + read -p "是否配置阿里云镜像加速?(y/n,默认为n): " configure_aliyun_acceleration + if [[ -z "$configure_aliyun_acceleration" ]]; then + configure_aliyun_acceleration="n" + fi + if [[ "$configure_aliyun_acceleration" == "y" || "$configure_aliyun_acceleration" == "n" ]]; then + break + fi + done +fi + +# 如果不配置阿里云镜像加速,循环询问是否配置私服加速,直到用户输入y或n,默认为n +if [[ "$configure_aliyun_acceleration" == "n" ]]; then + while true; do + read -p "是否配置私服加速?(y/n,默认为n): " configure_private_registry_acceleration + if [[ -z "$configure_private_registry_acceleration" ]]; then + configure_private_registry_acceleration="n" + fi + if [[ "$configure_private_registry_acceleration" == "y" || "$configure_private_registry_acceleration" == "n" ]]; then + break + fi + done +fi + +# 如果安装docker,循环询问是否安装docker-compose,直到用户输入y或n,默认为n +if [[ "$install_docker" == "y" ]]; then + while true; do + read -p "是否安装docker-compose?(y/n,默认为n): " install_docker_compose + if [[ -z "$install_docker_compose" ]]; then + install_docker_compose="n" + fi + if [[ "$install_docker_compose" == "y" || "$install_docker_compose" == "n" ]]; then + break + fi + done +fi + +# -------------- 开始执行 -------------- + +# 如果用户输入了主机名,则设置当前主机主机名 +if [[ -n "$hostname" ]]; then + hostnamectl set-hostname "$hostname" +fi + +# 如果用户输入了管理员账户且账户不存在,则创建管理员账号 +if [[ -n "$admin_user" && ! $(id -u "$admin_user" 2>/dev/null) ]]; then + admin_password="Zqh$(echo "$admin_user" | awk '{print toupper(substr($0, 1, 1))}')@$(hostname)" + useradd "$admin_user" + echo "$admin_password" | passwd --stdin "$admin_user" + echo "创建管理员账号成功:账号为$admin_user,密码为$admin_password" +fi + +# 如果用户输入了普通账户且账户不存在,则创建普通账号 +if [[ -n "$user" && ! $(id -u "$user" 2>/dev/null) ]]; then + user_password="Zqh$(echo "$user" | awk '{print toupper(substr($0, 1, 1))}')@$(hostname)" + useradd "$user" + echo "$user_password" | passwd --stdin "$user" + echo "创建普通账号成功:账号为$user,密码为$user_password" +fi + +# 如果用户要将yum源修改为阿里云镜像,则进行阿里云yum镜像加速配置 +if [[ "$use_aliyun_mirror" == "y" ]]; then + mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak + curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo + yum clean all + yum makecache + echo "已将yum源修改为阿里云镜像" +fi + +# 如果用户要更新系统,则执行系统更新命令 +if [[ "$update_system" == "y" ]]; then + yum update -y + echo "系统更新完成" +fi + +# 如果用户要更新软件,则执行软件更新命令 +if [[ "$update_software" == "y" ]]; then + yum upgrade -y + echo "软件更新完成" +fi + +# 如果用户要安装宝塔,则执行宝塔安装命令 +if [[ "$install_bt" == "y" ]]; then + yum install -y wget + wget -O install.sh http://download.bt.cn/install/install_6.0.sh + sh install.sh + echo "宝塔安装完成" +fi + +# 如果用户要安装jdk8,则通过yum安装jdk8 +if [[ "$install_jdk8" == "y" ]]; then + rpm -qa | grep java | xargs rpm -e --nodeps + yum install -y java-1.8.0-openjdk + java -version + echo "JDK 8安装完成" +fi + +# 如果用户要安装docker,则通过yum安装docker-ce +if [[ "$install_docker" == "y" ]]; then + yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine + yum remove docker-ce + yum install -y yum-utils device-mapper-persistent-data lvm2 + yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo + yum install -y docker-ce docker-ce-cli containerd.io + systemctl start docker + systemctl enable docker + echo "Docker安装完成" +fi + +# 如果用户要为docker配置阿里云镜像加速,则执行配置阿里云镜像加速命令 +if [[ "$configure_aliyun_acceleration" == "y" ]]; then + mkdir -p /etc/docker + tee /etc/docker/daemon.json <<-'EOF' + { + "registry-mirrors": ["https://qu3lgucz.mirror.aliyuncs.com"] + } + EOF + + systemctl restart docker + echo "已配置阿里云镜像加速" +fi + +# 如果用户要配置私服加速,则执行配置私服加速命令 +if [[ "$configure_private_registry_acceleration" == "y" ]]; then + # 执行配置私服加速命令 + if [[ "$configure_private_registry_acceleration" == "y" ]]; then + while true; do + read -p "请输入私服镜像地址: " private_registry_mirror + if [[ -n "$private_registry_mirror" ]]; then + break + fi + done + + mkdir -p /etc/docker + tee /etc/docker/daemon.json <<-EOF + { + "registry-mirrors": ["$private_registry_mirror"] + } + EOF + systemctl restart docker + echo "已配置私服加速" + fi +fi + +# 如果用户要安装docker-compose,则先判断当前路径下是否有docker-compose二进制文件,如果没有则通过curl下载,然后执行安装 +if [[ "$install_docker_compose" == "y" ]]; then + if [[ ! -x "$(command -v docker-compose)" ]]; then + echo "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" + curl -L -o docker-compose https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) + chmod +x docker-compose + mv docker-compose /usr/local/bin/ + docker-compose version + fi + echo "Docker Compose安装完成" +fi \ No newline at end of file