下面从 CentOS 系统清理与优化 的角度,给你一套实用、可操作的方案,适用于 CentOS 7 / 8 / Stream,按“安全 → 进阶 → 长期优化”分层说明。
# CentOS 7
yum clean all
# CentOS 8 / Stream
dnf clean all
清理旧缓存目录(可选):
rm -rf /var/cache/yum/*
rm -rf /var/cache/dnf/*
# CentOS 7
yum autoremove
# CentOS 8 / Stream
dnf autoremove
查看当前内核:
uname -r
查看已安装内核:
rpm -qa | grep kernel
保留 当前 + 一个旧版本,删除其余:
yum remove kernel-<version>
✅ 推荐工具(更安全):
yum install yum-utils
package-cleanup --oldkernels --count=2
查看大小:
journalctl --disk-usage
限制大小:
journalctl --vacuum-size=200M
或永久配置:
vim /etc/systemd/journald.conf
SystemMaxUse=200M
重启:
systemctl restart systemd-journald
rm -rf /tmp/*
建议设置自动清理:
systemctl enable tmp.mount
查看大文件:
du -sh /var/log/*
清理旧日志:
find /var/log -type f -name "*.log-*" -delete
du -ah / | sort -rh | head -20
du -h --max-depth=1 / | sort -hr
docker system prune -af
systemctl list-unit-files --type=service | grep enabled
示例:
systemctl disable bluetooth
systemctl disable postfix
查看:
free -h
关闭 swap(内存充足时):
swapoff -a
永久关闭:
vim /etc/fstab
# 注释 swap 行
ulimit -n
修改:
vim /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
✅ 定期清理脚本(cron)
0 3 * * 0 yum clean all && journalctl --vacuum-size=200M
✅ 使用监控工具
htopiotopncdu(磁盘分析神器)✅ 设置日志轮转
vim /etc/logrotate.conf
yum clean all
yum autoremove -y
journalctl --vacuum-size=200M
rm -rf /tmp/*
你可以直接贴:
df -h
du -sh /*