Revert "feat: 重新编写 init.sh 脚本,增加系统清理功能"

This reverts commit 68af8e0ff1.
This commit is contained in:
2025-06-01 22:40:09 +08:00
parent 68af8e0ff1
commit a1fbf5bd7f

View File

@@ -1,198 +1,90 @@
#!/bin/bash #!/bin/bash
set -euo pipefail
# Linux 系统清理脚本 echo "=== 通用 Linux 初始化脚本 ==="
# =============================== # 获取系统信息
# 功能: DISTRO_ID=$(grep ^ID= /etc/os-release | cut -d= -f2 | tr -d '"')
# 1. 显示系统信息 DISTRO_NAME=$(grep ^PRETTY_NAME= /etc/os-release | cut -d= -f2 | tr -d '"')
# 2. 清理旧的内核包和残留的配置文件 CODENAME=$(lsb_release -cs 2>/dev/null || echo "")
# 3. 清理 /tmp 目录下的临时文件
# 4. 清理 APT 缓存目录 /var/cache/apt/archives
# 5. 清理用户缓存目录 ~/.cache
# 6. 清理 systemd 日志 /var/log/journal
# ===============================
# 检查是否以 root 身份运行 echo "检测到系统: $DISTRO_NAME"
if [ "$EUID" -ne 0 ]; then
echo "请使用 sudo 或以 root 身份运行此脚本。"
exit 1
fi
echo "==============================" # 设置 DNS
echo " Linux 系统清理脚本 " set_dns() {
echo "==============================" echo "配置 DNS 到 114.114.114.114 和 223.5.5.5..."
echo -e "nameserver 114.114.114.114\nnameserver 223.5.5.5" > /etc/resolv.conf || true
}
# 1. 显示系统信息 # 设置时区和同步时间
echo "" set_timezone() {
echo "系统信息:" echo "设置时区为 Asia/Shanghai..."
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime || true
# 获取当前 Linux 发行版 if command -v timedatectl >/dev/null 2>&1; then
if [ -f /etc/os-release ]; then timedatectl set-timezone Asia/Shanghai || true
. /etc/os-release timedatectl set-ntp true || true
distro=$NAME
else else
distro=$(uname -s) echo "timedatectl 不可用,尝试使用 ntpdate..."
(apt install -y ntpdate || yum install -y ntpdate || dnf install -y ntpdate) >/dev/null 2>&1 || true
ntpdate ntp.aliyun.com || true
fi fi
echo "操作系统:$distro" }
# 获取登录的用户名 # 设置软件源
username=$(logname) set_mirrors() {
echo "登录用户名:$username" echo "更换软件源为清华镜像..."
# 获取 CPU 数量 case "$DISTRO_ID" in
cpu_count=$(nproc) ubuntu)
echo "CPU 数量:$cpu_count" cat > /etc/apt/sources.list <<EOF
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${CODENAME} main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${CODENAME}-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${CODENAME}-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu ${CODENAME}-security main restricted universe multiverse
EOF
apt update -y || true
;;
# 获取内存大小和使用率 debian|raspbian)
total_mem=$(free -h | awk '/Mem:/ {print $2}') cat > /etc/apt/sources.list <<EOF
used_mem=$(free -h | awk '/Mem:/ {print $3}') deb https://mirrors.tuna.tsinghua.edu.cn/${DISTRO_ID}/ ${CODENAME} main contrib non-free non-free-firmware
mem_usage=$(free | awk '/Mem:/ {printf("%.2f"), $3/$2 * 100}') deb https://mirrors.tuna.tsinghua.edu.cn/${DISTRO_ID}/ ${CODENAME}-updates main contrib non-free non-free-firmware
echo "内存大小:$total_mem" deb https://mirrors.tuna.tsinghua.edu.cn/${DISTRO_ID}/ ${CODENAME}-backports main contrib non-free non-free-firmware
echo "已用内存:$used_mem ($mem_usage%)" deb https://mirrors.tuna.tsinghua.edu.cn/${DISTRO_ID}-security ${CODENAME}-security main contrib non-free non-free-firmware
EOF
apt update -y || true
;;
# 获取硬盘大小和使用率 centos|rhel|almalinux|rocky)
disk_total=$(df -h / | awk 'NR==2 {print $2}') echo "配置 RHEL 系列清华镜像..."
disk_used=$(df -h / | awk 'NR==2 {print $3}') if [ -f /etc/yum.repos.d/CentOS-Base.repo ]; then
disk_usage=$(df -h / | awk 'NR==2 {print $5}') sed -i.bak -e 's|^mirrorlist=|#mirrorlist=|g' \
echo "硬盘总容量:$disk_total" -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' \
echo "已用硬盘:$disk_used ($disk_usage)" /etc/yum.repos.d/CentOS-Base.repo
echo "=============================="
# 2. 清理旧的内核包和残留的配置文件
echo ""
echo "步骤 1清理旧的内核包和残留的配置文件"
# 获取当前正在运行的内核版本
current_kernel=$(uname -r)
echo "当前正在运行的内核版本:$current_kernel"
# 获取已安装的内核包列表
installed_kernels=$(dpkg --list | grep 'linux-image-[0-9]' | awk '/^ii/{print $2}')
echo "已安装的内核包:"
echo "$installed_kernels"
# 初始化要移除的内核包列表
remove_kernels=()
# 筛选需要移除的旧内核包
for kernel in $installed_kernels; do
if [[ "$kernel" != *"$current_kernel"* ]]; then
remove_kernels+=("$kernel")
fi fi
done yum makecache || true
;;
# 显示将要移除的内核包 fedora)
if [ ${#remove_kernels[@]} -eq 0 ]; then echo "配置 Fedora 清华镜像..."
echo "没有需要移除的旧内核。" sed -i.bak -e 's|^metalink=|#metalink=|g' \
else -e 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.tuna.tsinghua.edu.cn/fedora|g' \
echo "将要移除的旧内核包:" /etc/yum.repos.d/fedora*.repo
for kernel in "${remove_kernels[@]}"; do dnf makecache || true
echo "$kernel" ;;
done
# 提示用户确认 *)
read -p "是否确认移除以上内核包?[y/N]: " confirm echo "不支持的系统类型:$DISTRO_ID,跳过换源"
if [[ "$confirm" =~ ^[Yy]$ ]]; then ;;
echo "正在移除旧内核包..." esac
apt remove --purge -y "${remove_kernels[@]}" }
echo "自动清理不需要的依赖项..." # 主执行函数
apt autoremove --purge -y main() {
set_mirrors
set_dns
set_timezone
echo "✅ 初始化完成。"
}
echo "更新 GRUB 引导菜单..." main
update-grub
echo "旧内核包已成功移除。"
else
echo "已跳过旧内核包的清理。"
fi
fi
# 清理残留的配置文件
echo "正在清理残留的配置文件..."
dpkg -l | awk '/^rc/{print $2}' | xargs dpkg --purge
echo "残留的配置文件已清理。"
# 3. 清理 /tmp 目录下的临时文件
echo ""
echo "步骤 2清理 /tmp 目录下的临时文件"
# 提示用户确认
read -p "是否清理 /tmp 目录下的所有文件?[y/N]: " confirm_tmp
if [[ "$confirm_tmp" =~ ^[Yy]$ ]]; then
echo "正在清理 /tmp 目录..."
rm -rf /tmp/*
echo "/tmp 目录已清理。"
else
echo "已跳过 /tmp 目录的清理。"
fi
# 4. 清理 APT 缓存目录 /var/cache/apt/archives
echo ""
echo "步骤 3清理 APT 缓存目录 /var/cache/apt/archives"
# 显示当前缓存占用空间
apt_cache_size=$(du -sh /var/cache/apt/archives | awk '{print $1}')
echo "APT 缓存当前占用空间:$apt_cache_size"
# 提示用户确认
read -p "是否清理 APT 缓存?这将删除已下载的包文件。[y/N]: " confirm_apt
if [[ "$confirm_apt" =~ ^[Yy]$ ]]; then
echo "正在清理 APT 缓存..."
apt clean
echo "APT 缓存已清理。"
else
echo "已跳过 APT 缓存的清理。"
fi
# 5. 清理用户缓存目录 ~/.cache
echo ""
echo "步骤 4清理用户缓存目录 ~/.cache"
# 获取用户的主目录
user_home=$(eval echo "~$SUDO_USER")
# 显示当前缓存占用空间
user_cache_size=$(du -sh "$user_home/.cache" 2>/dev/null | awk '{print $1}')
echo "用户缓存目录当前占用空间:$user_cache_size"
# 提示用户确认
read -p "是否清理用户缓存目录 ~/.cache[y/N]: " confirm_user_cache
if [[ "$confirm_user_cache" =~ ^[Yy]$ ]]; then
echo "正在清理用户缓存目录..."
rm -rf "$user_home/.cache/*"
echo "用户缓存目录已清理。"
else
echo "已跳过用户缓存目录的清理。"
fi
# 6. 清理 systemd 日志 /var/log/journal
echo ""
echo "步骤 5清理 systemd 日志 /var/log/journal"
# 显示当前日志占用空间
journal_size=$(du -sh /var/log/journal 2>/dev/null | awk '{print $1}')
echo "systemd 日志当前占用空间:$journal_size"
if [ -d "/var/log/journal" ]; then
# 提示用户确认
read -p "是否清理 systemd 日志?[y/N]: " confirm_journal
if [[ "$confirm_journal" =~ ^[Yy]$ ]]; then
echo "请输入要保留的日志大小例如100M1G"
read -p "保留大小: " journal_limit
echo "正在清理 systemd 日志,保留大小为 $journal_limit..."
journalctl --vacuum-size="$journal_limit"
echo "systemd 日志已清理。"
else
echo "已跳过 systemd 日志的清理。"
fi
else
echo "未找到 /var/log/journal 目录,可能未启用持久化日志。"
fi
echo ""
echo "系统清理完成!"
echo "=============================="