Linux_centso-7系统初始化脚本shell

这个脚本用于CentOS 7系统,提供了多项初始化任务,包括:修改默认官方YUM源为阿里云源,安装常用命令,安装并启用nginx服务,永久关闭防火墙和SELINUX。用户可以根据提示选择执行相应的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/bin/bash
echo "
+-----------------------------+
|           Tools             |
+-----------------------------+
|      1.修改默认官方yum源    |
|      2.修改为aliyun_repo    |
|      3.安装常用命令         |
|      4.安装epel源           |
|      5.安装nginx服务        |
|      6.永久关闭防火墙       |
|      7.永久关闭selinux      |
+-----------------------------+
"
read -p "请输入你的选择:" choose
case $choose in
1)
  echo "update yum source"
  sed -i "s/mirrorlist=http:/\#mirrorlist=http:/g" /etc/yum.repos.d/CentOS-Base.repo
  sed -i "s/\#baseurl=http:/baseurl=http:/g"  /etc/yum.repos.d/CentOS-Base.repo
  yum makecache
  echo "update yum source complete!!!"
  ;;
2) # 获得当前 CentOS 系统发行版本号
   # 第一个 awk 后边必须换行, 目前未查到原因
   releasetmp=`cat /etc/redhat-release | awk '{match($0,"release ")
   print substr($0,RSTART+RLENGTH)}' | awk -F '.' '{print $1}'`
   echo $releasetmp
   sleep 5

   yum install wget -y
   # 备份原文件  应该添加检测是否原来有备份文件, 有的话应该备份为别名文件  此处省略
   cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak 
   wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-$releasetmp.repo
   if [[ $? -eq 0 ]];then
     echo -e "\033[32m# yum 源已成功更新为 aliyun_repo #\033[0m" 
     sleep 3
   else
    echo -e "\033[31m# yum 源未成功更新为 aliyun_repo #\n3s 后退出...\033[0m"
    exit
   fi

   # 添加EPEL源
   wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-$releasetmp.repo 

   # 重建缓存
   yum clean all
   yum makecache

   # 自动更新包列表,可选择注释该行
   yum update -y
   ;; 
3)
   echo "安装常用命令"
   yum -y install vim lrzsz lsof wget net-tools killall
   echo "安装完成"
   ;;
4)
   echo "安装epel源"
   yum -y install epel-release
   echo "完成安装epel源"
   ;;
5) echo "安装nginx服务 并设置开机自启"
cat <<-EOF > /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
EOF
yum makecache fast

 echo " 安装nginx服务."
  if [ $? -eq 0 ];then
   yum -y install nginx
   else
   exit 502
  fi

 echo "添加新的内容到网站的首页文件中."
  if [ -d /usr/share/nginx/html ];then
   echo "hello nginx" >/usr/share/nginx/html/index.html
  else
   echo "请确认已经安装了nginx服务"
  fi

  # 启动nginx服务.
   systemctl start nginx
   systemctl enable nginx
  ;;
6)
  systemctl stop firewalld
  systemctl disable firewalld
;;
7)
  sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
setenforce 0
;;
*)
   exit   
esac
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值