温馨提示×

Linux怎么清理无用包

小樊
32
2026-07-07 01:04:54
栏目: 智能运维

在 Linux 中清理无用包(不再需要的依赖、缓存、旧内核等),不同发行版命令不同。下面按常见发行版给你一份实用速查表


一、Debian / Ubuntu / Linux Mint

1️⃣ 删除不再需要的依赖包

sudo apt autoremove

会删除“曾经作为依赖安装、但现在没有任何软件使用”的包。

2️⃣ 清理下载的安装包缓存

sudo apt clean

或只删除旧版本:

sudo apt autoclean

3️⃣ 删除旧内核(谨慎)

查看当前内核:

uname -r

列出已安装内核:

dpkg --list | grep linux-image

删除旧内核(不要删当前使用的):

sudo apt remove linux-image-旧版本-generic

推荐工具(安全)

sudo apt install byobu
sudo purge-old-kernels

二、RHEL / CentOS / Rocky / AlmaLinux

1️⃣ 删除无用依赖

sudo yum autoremove

或(CentOS 8+ / Rocky / Alma):

sudo dnf autoremove

2️⃣ 清理缓存

sudo yum clean all

或:

sudo dnf clean all

3️⃣ 删除旧内核

uname -r
rpm -qa kernel

保留当前内核,删除旧版本:

sudo yum remove kernel-旧版本

三、Arch Linux / Manjaro

1️⃣ 删除孤立包

sudo pacman -Rns $(pacman -Qdtq)

2️⃣ 清理包缓存

sudo pacman -Sc

或彻底清理(谨慎):

sudo pacman -Scc

3️⃣ 清理旧内核

Arch 通常只保留当前内核,手动删除旧版本:

sudo pacman -R linux-lts

四、通用清理(所有发行版)

1️⃣ 清理用户缓存

rm -rf ~/.cache/*

2️⃣ 查找大文件(手动清理)

du -h --max-depth=1 /var | sort -hr

3️⃣ 日志清理

sudo journalctl --vacuum-time=7d

五、一键清理(推荐新手)

Ubuntu / Debian

sudo apt update && sudo apt autoremove -y && sudo apt autoclean

CentOS / Rocky / Alma

sudo dnf autoremove -y && sudo dnf clean all

六、注意事项 ⚠️

  • ❌ 不要删除 当前正在使用的内核
  • clean 不会影响系统运行,但 purge 会彻底删除配置
  • ✅ 生产服务器建议先 dry-run 或备份

如果你愿意,可以告诉我:

  • 你的 Linux 发行版 + 版本
  • 桌面还是服务器
  • 是否担心误删内核

我可以给你最安全的一套命令

0