下面按常见场景给你一个「CentOS 清理(cleanup)配置与操作指南」,涵盖 系统、日志、YUM/DNF、Docker、定时任务 等,你可以按需取用。
# CentOS 7
yum clean all
rm -rf /var/cache/yum
# CentOS 8 / Stream
dnf clean all
rm -rf /var/cache/dnf
# 查看当前内核
uname -r
# 查看已安装内核
rpm -qa kernel
# 删除旧内核(示例)
yum remove kernel-3.10.0-1160.el7.x86_64
✅ 建议:至少保留 2 个内核
# 查看大小
journalctl --disk-usage
# 保留最近 3 天
journalctl --vacuum-time=3d
# 限制最大 200M
journalctl --vacuum-size=200M
# 查看大文件
du -sh /var/log/*
# 清空大日志(不要直接 rm)
truncate -s 0 /var/log/messages
truncate -s 0 /var/log/secure
✅ 推荐配置日志轮转:
vim /etc/logrotate.conf
# 删除 7 天未访问的临时文件
find /tmp -type f -atime +7 -delete
systemctl status systemd-tmpfiles-clean.timer
systemctl enable --now systemd-tmpfiles-clean.timer
# 查看磁盘
df -h
# 查看目录占用
du -h --max-depth=1 /var
docker system df
docker system prune -a
docker image prune -a
docker volume prune
docker container prune
vim /usr/local/bin/centos_cleanup.sh
#!/bin/bash
yum clean all
journalctl --vacuum-time=7d
find /tmp -type f -atime +7 -delete
docker system prune -f
chmod +x /usr/local/bin/centos_cleanup.sh
crontab -e
0 3 * * 0 /usr/local/bin/centos_cleanup.sh >> /var/log/cleanup.log 2>&1
⚠️ CentOS 7 已于 2024-06-30 EOL
如果你还在用 CentOS 7,建议:
✅ 迁移到:
如必须继续用,可切换镜像源:
sed -i 's|mirrorlist=|#mirrorlist=|g' /etc/yum.repos.d/CentOS-*.repo
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=https://vault.centos.org|g' /etc/yum.repos.d/CentOS-*.repo
yum clean all
journalctl --vacuum-time=7d
find /tmp -type f -atime +7 -delete
如果你愿意,可以告诉我:
我可以给你定制一套最合适的 cleanup 方案。