温馨提示×

怎么优化debian清理流程

小樊
43
2026-09-22 06:37:26
栏目: 智能运维

在 Debian 系统上“清理流程”通常指系统瘦身、释放磁盘空间、减少冗余文件和提升可维护性。下面从常规清理 → 自动化 → 进阶优化三个层次给你一套可落地的方案。


一、基础清理(每次都该做)

1. 清理 apt 缓存

apt-get clean
apt-get autoclean
  • clean:删除所有已下载的 .deb
  • autoclean:只删过期包

2. 删除无用依赖

apt-get autoremove --purge

⚠️ 务必加 --purge,否则配置文件会残留

3. 清理旧内核

dpkg -l | grep linux-image
apt-get remove --purge linux-image-旧版本

或自动化:

apt-get autoremove --purge

二、系统化清理流程(推荐脚本化)

示例:一键清理脚本

#!/bin/bash
set -e

apt-get update
apt-get autoremove --purge -y
apt-get autoclean -y
apt-get clean
journalctl --vacuum-time=7d
rm -rf /var/tmp/*

保存为 /usr/local/bin/debian-clean.sh

chmod +x /usr/local/bin/debian-clean.sh

三、周期性自动清理(systemd 定时器)

1. 创建 service

/etc/systemd/system/debian-clean.service

[Unit]
Description=Debian Cleanup

[Service]
Type=oneshot
ExecStart=/usr/local/bin/debian-clean.sh

2. 创建 timer

/etc/systemd/system/debian-clean.timer

[Unit]
Description=Weekly Debian Cleanup

[Timer]
OnCalendar=weekly
Persistent=true

[Install]
WantedBy=timers.target

启用:

systemctl enable --now debian-clean.timer

四、日志与临时文件优化

journald 限制

/etc/systemd/journald.conf
SystemMaxUse=200M
MaxRetentionSec=2week

然后:

systemctl restart systemd-journald

清理临时目录

rm -rf /tmp/*
rm -rf /var/tmp/*

五、进阶优化(适合服务器)

1. 禁用 unnecessary 服务

systemctl list-unit-files | grep enabled

关闭例子:

systemctl disable bluetooth

2. 使用 deborphan 找孤儿包

apt install deborphan
deborphan
apt-get remove --purge $(deborphan)

3. 使用 cruft-ng(高级)

apt install cruft-ng
cruft

六、最小化安装建议(从源头减少清理)

  • 安装时用 debootstrapminimal
  • 避免 task-desktop 等元包
  • 使用 apt-get install --no-install-recommends

七、检查磁盘占用快速命令

du -h /var | sort -rh | head
ncdu

如果你愿意,可以告诉我:

  • 桌面还是服务器
  • 是否 Docker / LXC / 云主机
  • 是否有 定期维护需求

我可以帮你定制一套最合适的 Debian 清理方案

0