在 CentOS 上做系统清理(cleanup)时,核心原则是:只删“明确可删的”内容,不删系统关键文件。下面按「安全级别」给你一套常用、稳妥的做法,适用于 CentOS 7 / 8 / Stream。
df -h
uname -r
确认:
# CentOS 7
yum clean all
# CentOS 8 / Stream
dnf clean all
✅ 安全
⚠️ 不要删当前正在用的内核
uname -r
# CentOS 7
rpm -qa | grep kernel
# CentOS 8+
dnf list installed | grep kernel
# CentOS 7
package-cleanup --oldkernels --count=2
# CentOS 8+
dnf remove $(dnf repoquery --installonly --latest-limit=-2 -q)
✅ 安全
journalctl --disk-usage
journalctl --vacuum-time=7d
# 或
journalctl --vacuum-size=500M
✅ 安全
ls -lh /var/log
安全删除:
rm -f /var/log/*.log-*
rm -f /var/log/messages-*
rm -f /var/log/secure-*
⚠️ 不要删
rm -rf /tmp/*
rm -rf /var/tmp/*
✅ 安全
find / -xdev -type f -size +500M 2>/dev/null
du -h --max-depth=1 / 2>/dev/null | sort -hr | head
✅ 安全
docker system df
docker system prune -f
⚠️ 会删除:
✅ 生产环境 先确认
❌ 删除:
/usr/lib/lib64/boot 下当前内核/var/lib 下的数据库目录(MySQL、PostgreSQL 等)❌ 使用:
rm -rf /*
#!/bin/bash
echo "Cleaning yum cache..."
yum clean all
echo "Cleaning old kernels..."
package-cleanup --oldkernels --count=2
echo "Cleaning journal logs..."
journalctl --vacuum-time=7d
echo "Cleaning tmp..."
rm -rf /tmp/* /var/tmp/*
echo "Done."
df -h 输出帮你判断该清哪里你可以直接贴:
cat /etc/centos-release
df -h