JiaoBen/MD/centos.sh
2025-01-06 14:28:46 +08:00

94 lines
2.7 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/bash
# 输出菜单
list() {
echo "--------------------------------------"
echo "------------性能优化工具箱------------"
echo "--------------------------------------"
echo "1. 更换 YUM 源"
echo "2. 关闭防火墙"
echo "3. 时间同步"
echo "4. 创建用户"
echo "5. 安装常用软件"
echo "6. 退出"
echo "--------------------------------------"
}
# 更换 YUM 源
yuan() {
echo "正在更换 YUM 源..."
rm -rf /etc/yum.repos.d/*
if curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null && \
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo &>/dev/null; then
yum clean all
yum makecache fast
echo "YUM 源更换完成!"
else
echo "YUM 源更换失败,请检查网络或链接。"
fi
}
# 关闭防火墙
fire() {
echo "正在关闭防火墙..."
systemctl stop firewalld
systemctl disable firewalld
setenforce 0 # 临时关闭 SELinux
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config # 永久关闭 SELinux
echo "防火墙已关闭SELinux 已禁用(需重启生效)。"
}
# 时间同步
timed() {
echo "正在安装和同步时间..."
if yum -y install ntpdate &>/dev/null; then
ntpdate ntp1.aliyun.com
echo "时间同步成功!"
else
echo "时间同步失败,请检查网络连接或 YUM 配置。"
fi
}
# 创建用户
user() {
read -p "请输入要创建的用户名:" name
if id "$name" &>/dev/null; then
echo "用户 $name 已存在,请勿重复创建。"
else
useradd "$name"
userpasswd=$(openssl rand -base64 8)
echo "$userpasswd" | passwd --stdin "$name" &>/dev/null
if [ $? -eq 0 ]; then
echo "用户名:$name,密码:$userpasswd" >> /opt/user.txt
echo "用户 $name 创建成功,密码已存储在 /opt/user.txt 中。"
else
echo "用户创建失败,请检查系统配置。"
fi
fi
}
# 安装常用软件
anzhuang() {
echo "正在安装常用软件vim、lsof、unzip、wget..."
if yum -y install vim lsof unzip wget &>/dev/null; then
echo "常用软件安装完成!"
else
echo "软件安装失败,请检查 YUM 源配置。"
fi
}
# 主循环
while true; do
list
read -p "请输入您的选项1-6" num
case $num in
1) yuan; sleep 2 ;;
2) fire; sleep 2 ;;
3) timed; sleep 2 ;;
4) user; sleep 2 ;;
5) anzhuang; sleep 2 ;;
6) echo "退出工具箱,感谢使用!"; exit ;;
*) echo "输入无效,请输入数字 1-6 选择功能。"; sleep 2 ;;
esac
done