JiaoBen/MD/centos.sh

161 lines
4.9 KiB
Bash
Raw Normal View History

2024-12-26 09:43:54 +08:00
#!/usr/bin/bash
2025-01-06 14:28:46 +08:00
# 输出菜单
list() {
echo "--------------------------------------"
echo "------------性能优化工具箱------------"
echo "--------------------------------------"
echo "1. 更换 YUM 源"
echo "2. 关闭防火墙"
echo "3. 时间同步"
echo "4. 创建用户"
2025-03-09 15:44:47 +08:00
echo "5. 删除用户"
echo "6. 安装常用软件"
echo "7. 内存使用情况"
echo "8. 磁盘使用情况"
echo "9. 服务器信息"
echo "10 CPU使用情况"
echo "11. 退出"
2025-01-06 14:28:46 +08:00
echo "--------------------------------------"
2024-12-26 09:43:54 +08:00
}
2025-01-06 14:28:46 +08:00
# 更换 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
2024-12-26 09:43:54 +08:00
}
2025-01-06 14:28:46 +08:00
# 关闭防火墙
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 已禁用(需重启生效)。"
2024-12-26 09:43:54 +08:00
}
2025-01-06 14:28:46 +08:00
# 时间同步
timed() {
echo "正在安装和同步时间..."
if yum -y install ntpdate &>/dev/null; then
ntpdate ntp1.aliyun.com
echo "时间同步成功!"
else
echo "时间同步失败,请检查网络连接或 YUM 配置。"
fi
2024-12-26 09:43:54 +08:00
}
2025-01-06 14:28:46 +08:00
# 创建用户
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
2024-12-26 09:43:54 +08:00
}
2025-03-09 15:44:47 +08:00
delete_user() {
read -p "请输入要删除的用户名:" name
# 检查用户是否存在
if id "$name" &>/dev/null; then
# 删除用户及其主目录和邮件池
userdel -r "$name"
if [ $? -eq 0 ]; then
# 如果用户删除成功,尝试从记录文件中移除该用户的密码信息
sed -i "/^用户名:$name/d" /opt/user.txt
echo "用户 $name 已成功删除。"
else
echo "用户删除失败,请检查系统配置。"
fi
else
echo "用户 $name 不存在,无需删除。"
fi
}
2025-01-06 14:28:46 +08:00
# 安装常用软件
anzhuang() {
echo "正在安装常用软件vim、lsof、unzip、wget..."
if yum -y install vim lsof unzip wget &>/dev/null; then
echo "常用软件安装完成!"
else
echo "软件安装失败,请检查 YUM 源配置。"
fi
2024-12-26 09:43:54 +08:00
}
2025-03-09 15:44:47 +08:00
mem() {
mem_total=$(free -m | awk 'NR==2 {print $2}')
mem_used=$(free -m | awk 'NR==2 {print $3}')
mem_free=$(free -m | awk 'NR==2 {print $4}')
mem_cache=$(free -m | awk 'NR==2 {print $6}')
mem_s=$((mem_used * 100 / mem_total))
mem_f=$((100 - mem_s))
echo "内存的使用率:$mem_s%"
echo "内存的总量: $mem_total MB"
echo "内存的空闲率: $mem_f%"
echo "内存的缓存量: $mem_cache MB"
echo "内存的空闲量: $mem_free MB"
}
disk() {
read -p "请输入分区名称:" name
case $name in
"/")
df -Th | awk 'NR==2 {print "总量:"$3,"使用:"$4,"空闲:"$5}'
;;
"/boot")
df -Th | awk 'NR==7 {print "总量:"$3,"使用:"$4,"空闲:"$5}'
;;
*)
echo "无效的分区名称"
;;
esac
}
system_info() {
cat /etc/redhat-release | awk '{print "系统版本号:"$4}'
echo "内核版本为:`uname -r`"
ip a | grep "2:" | awk -F: '{print "网卡名称:"$2}'
ip a | grep "inet " | awk '{print $2}' | awk -F "/" '{print "IP地址是:"$1}'
}
cpu_info() {
w | awk -F: 'NR==1 {print "CPU的平均负载:"$NF}'
vmstat | awk 'NR==3 {print "CPU空闲率:"$(NF-2)}'
cpu_id=$(vmstat | awk 'NR==3 {print $(NF-2)}')
cpu_free=$((100 - cpu_id))
echo "CPU使用率: $cpu_free%"
}
2025-01-06 14:28:46 +08:00
# 主循环
while true; do
list
2025-03-17 15:19:21 +08:00
read -p "请输入您的选项1-10" num
2025-01-06 14:28:46 +08:00
case $num in
1) yuan; sleep 2 ;;
2) fire; sleep 2 ;;
3) timed; sleep 2 ;;
4) user; sleep 2 ;;
2025-03-09 15:44:47 +08:00
5) delete_user;sleep 2;;
6) anzhuang; sleep 2 ;;
7) mem; sleep 2;;
8) disk; sleep 2;;
9) system_info; sleep 2;;
10)cpu_info; sleep 2;;
2025-03-17 15:19:21 +08:00
q) echo "退出工具箱,感谢使用!"; exit ;;
2025-01-06 14:28:46 +08:00
*) echo "输入无效,请输入数字 1-6 选择功能。"; sleep 2 ;;
esac
2025-03-09 15:44:47 +08:00
done