From 68af8e0ff148ccf7611c0d2d06356b5dbc5b90cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=89=A1=E8=9B=8E?= Date: Sun, 1 Jun 2025 21:28:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=87=8D=E6=96=B0=E7=BC=96=E5=86=99=20?= =?UTF-8?q?init.sh=20=E8=84=9A=E6=9C=AC=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E6=B8=85=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加系统信息显示功能 - 实现旧内核包和残留配置文件的清理 - 增加 /tmp 目录临时文件的清理 - 实现 APT 缓存、用户缓存目录和 systemd 日志的清理 - 优化脚本结构,增加用户交互和确认提示 --- bash/init.sh | 258 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 183 insertions(+), 75 deletions(-) diff --git a/bash/init.sh b/bash/init.sh index c80b094..f0cdfa1 100644 --- a/bash/init.sh +++ b/bash/init.sh @@ -1,90 +1,198 @@ #!/bin/bash -set -euo pipefail -echo "=== 通用 Linux 初始化脚本 ===" +# Linux 系统清理脚本 -# 获取系统信息 -DISTRO_ID=$(grep ^ID= /etc/os-release | cut -d= -f2 | tr -d '"') -DISTRO_NAME=$(grep ^PRETTY_NAME= /etc/os-release | cut -d= -f2 | tr -d '"') -CODENAME=$(lsb_release -cs 2>/dev/null || echo "") +# =============================== +# 功能: +# 1. 显示系统信息 +# 2. 清理旧的内核包和残留的配置文件 +# 3. 清理 /tmp 目录下的临时文件 +# 4. 清理 APT 缓存目录 /var/cache/apt/archives +# 5. 清理用户缓存目录 ~/.cache +# 6. 清理 systemd 日志 /var/log/journal +# =============================== -echo "检测到系统: $DISTRO_NAME" +# 检查是否以 root 身份运行 +if [ "$EUID" -ne 0 ]; then + echo "请使用 sudo 或以 root 身份运行此脚本。" + exit 1 +fi -# 设置 DNS -set_dns() { - 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 -} +echo "==============================" +echo " Linux 系统清理脚本 " +echo "==============================" -# 设置时区和同步时间 -set_timezone() { - echo "设置时区为 Asia/Shanghai..." - ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime || true - if command -v timedatectl >/dev/null 2>&1; then - timedatectl set-timezone Asia/Shanghai || true - timedatectl set-ntp true || true - else - 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 +# 1. 显示系统信息 +echo "" +echo "系统信息:" + +# 获取当前 Linux 发行版 +if [ -f /etc/os-release ]; then + . /etc/os-release + distro=$NAME +else + distro=$(uname -s) +fi +echo "操作系统:$distro" + +# 获取登录的用户名 +username=$(logname) +echo "登录用户名:$username" + +# 获取 CPU 数量 +cpu_count=$(nproc) +echo "CPU 数量:$cpu_count" + +# 获取内存大小和使用率 +total_mem=$(free -h | awk '/Mem:/ {print $2}') +used_mem=$(free -h | awk '/Mem:/ {print $3}') +mem_usage=$(free | awk '/Mem:/ {printf("%.2f"), $3/$2 * 100}') +echo "内存大小:$total_mem" +echo "已用内存:$used_mem ($mem_usage%)" + +# 获取硬盘大小和使用率 +disk_total=$(df -h / | awk 'NR==2 {print $2}') +disk_used=$(df -h / | awk 'NR==2 {print $3}') +disk_usage=$(df -h / | awk 'NR==2 {print $5}') +echo "硬盘总容量:$disk_total" +echo "已用硬盘:$disk_used ($disk_usage)" + +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 -} +done -# 设置软件源 -set_mirrors() { - echo "更换软件源为清华镜像..." +# 显示将要移除的内核包 +if [ ${#remove_kernels[@]} -eq 0 ]; then + echo "没有需要移除的旧内核。" +else + echo "将要移除的旧内核包:" + for kernel in "${remove_kernels[@]}"; do + echo "$kernel" + done - case "$DISTRO_ID" in - ubuntu) - cat > /etc/apt/sources.list < /etc/apt/sources.list </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 "请输入要保留的日志大小(例如:100M,1G):" + 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 "==============================" \ No newline at end of file