在 Debian 系统上做“自动 cleanup(自动清理)”通常指:自动删除不再需要的包、缓存、日志、旧内核等,以释放磁盘空间并保持系统整洁。下面按「安全 → 进阶 → 自动化」给你一套常用方案。
sudo apt autoremove
删除“曾经被依赖、现在没人用的包”。
sudo apt clean
清空 /var/cache/apt/archives/
sudo apt autoclean
只删掉旧版本的 deb 缓存(更安全)
Debian 不会自动删内核,时间久了 /boot 会满。
sudo apt autoremove --purge
通常会自动移除最旧的内核(保留当前+上一个)。
sudo journalctl --vacuum-time=7d
或永久限制大小:
sudo nano /etc/systemd/journald.conf
SystemMaxUse=200M
然后:
sudo systemctl restart systemd-journald
sudo rm -rf /tmp/*
(重启也会自动清)
unattended-upgrades(自动安全更新 + 清理)sudo apt install unattended-upgrades
sudo dpkg-reconfigure unattended-upgrades
编辑:
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades
确保包含:
Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "false";
它会 自动 apt autoremove。
apticron(可选,邮件提醒)sudo apt install apticron
debian-goodies 找残留sudo apt install debian-goodies
dpigs
sudo crontab -e
添加:
0 3 * * * apt autoremove -y && apt autoclean -y
创建:
sudo nano /etc/systemd/system/cleanup.service
[Unit]
Description=Debian Cleanup
[Service]
Type=oneshot
ExecStart=/usr/bin/apt autoremove -y
ExecStart=/usr/bin/apt autoclean -y
sudo nano /etc/systemd/system/cleanup.timer
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
启用:
sudo systemctl enable --now cleanup.timer
⚠️ 有风险,先看列表:
apt list --installed | grep -v debian
ncdu 查大文件sudo apt install ncdu
sudo ncdu /
✅ unattended-upgrades + apt autoremove + journalctl 限制
如果你告诉我:
我可以给你一套完全可复制的配置文件。