温馨提示×

centos cleanup如何优化

小樊
42
2026-08-19 04:03:04
栏目: 智能运维

下面从 CentOS 系统清理与优化 的角度,给你一套实用、可操作的方案,适用于 CentOS 7 / 8 / Stream,按“安全 → 进阶 → 长期优化”分层说明。


一、基础清理(安全、必做)

1. 清理 YUM / DNF 缓存

# CentOS 7
yum clean all

# CentOS 8 / Stream
dnf clean all

清理旧缓存目录(可选):

rm -rf /var/cache/yum/*
rm -rf /var/cache/dnf/*

2. 删除无用包

# CentOS 7
yum autoremove

# CentOS 8 / Stream
dnf autoremove

3. 清理旧内核(⚠️ 谨慎)

查看当前内核:

uname -r

查看已安装内核:

rpm -qa | grep kernel

保留 当前 + 一个旧版本,删除其余:

yum remove kernel-<version>

✅ 推荐工具(更安全):

yum install yum-utils
package-cleanup --oldkernels --count=2

二、日志与临时文件清理

1. journal 日志

查看大小:

journalctl --disk-usage

限制大小:

journalctl --vacuum-size=200M

或永久配置:

vim /etc/systemd/journald.conf
SystemMaxUse=200M

重启:

systemctl restart systemd-journald

2. 清理 /tmp

rm -rf /tmp/*

建议设置自动清理:

systemctl enable tmp.mount

3. 清理 /var/log

查看大文件:

du -sh /var/log/*

清理旧日志:

find /var/log -type f -name "*.log-*" -delete

三、磁盘与 inode 优化

1. 查找大文件

du -ah / | sort -rh | head -20

2. 查找大目录

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

3. 清理 Docker(如使用)

docker system prune -af

四、系统性能优化(进阶)

1. 关闭不必要服务

systemctl list-unit-files --type=service | grep enabled

示例:

systemctl disable bluetooth
systemctl disable postfix

2. 调整 SWAP(内存优化)

查看:

free -h

关闭 swap(内存充足时):

swapoff -a

永久关闭:

vim /etc/fstab
# 注释 swap 行

3. 文件描述符优化

ulimit -n

修改:

vim /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535

五、长期维护建议(最佳实践)

✅ 定期清理脚本(cron)

0 3 * * 0 yum clean all && journalctl --vacuum-size=200M

✅ 使用监控工具

  • htop
  • iotop
  • ncdu(磁盘分析神器)

✅ 设置日志轮转

vim /etc/logrotate.conf

六、快速一键清理(保守版)

yum clean all
yum autoremove -y
journalctl --vacuum-size=200M
rm -rf /tmp/*

七、如果你愿意,我可以:

  • ✅ 按 CentOS 7 / 8 具体版本 给你定制方案
  • ✅ 给你 生产环境安全清理清单
  • ✅ 排查 磁盘爆满 / inode 满 真实原因
  • ✅ 针对 服务器 / Docker / K8s 节点 优化

你可以直接贴:

df -h
du -sh /*

0