在 CentOS 中进行系统清理(cleanup),可以根据不同需求使用以下工具和命令:
CentOS 主要使用 yum(CentOS 7 及以前)或 dnf(CentOS 8 / Stream)作为包管理器。
清理 yum/dnf 缓存
# CentOS 7
yum clean all
# CentOS 8 / Stream
dnf clean all
删除无用依赖包
# 删除不再被需要的依赖
yum autoremove # CentOS 7
dnf autoremove # CentOS 8+
列出并清理旧内核
# 查看已安装内核
rpm -q kernel
# 保留最新内核,删除旧内核(CentOS 7)
package-cleanup --oldkernels --count=1
journalctl 日志
# 查看日志占用
journalctl --disk-usage
# 清理旧日志(保留最近 7 天)
journalctl --vacuum-time=7d
/var/log 下日志
可使用 logrotate(系统自带)自动管理,也可手动删除大文件(如 messages-*、secure-*)。
系统临时目录
rm -rf /tmp/*
rm -rf /var/tmp/*
注意不要删除正在使用的文件
自动清理工具
yum install tmpwatch # CentOS 7
tmpwatch 24 /tmp # 删除 /tmp 下 24 小时未访问文件
ncdu(推荐)
yum install ncdu
ncdu /
交互式查看哪些目录占用空间大。
du / df
df -h
du -sh /*
yum install bleachbit
bleachbit --clean system.cache system.tmp
谨慎使用,避免误删重要文件
dnf clean all
dnf autoremove
journalctl --vacuum-time=7d
package-cleanup --oldkernels --count=1
ncdu /
如果你是 CentOS 7 还是 8/Stream、服务器还是桌面环境,我可以给你更精确的清理方案。