在 Debian 系统中,“缓存”通常指以下几类,不同缓存有不同的自动清理方式。下面按常见场景说明。
APT 下载的 .deb 包会保存在:
/var/cache/apt/archives/
sudo apt clean # 删除所有已下载的 deb 包
sudo apt autoclean # 只删除过期的包
编辑配置文件:
sudo nano /etc/apt/apt.conf.d/99clean
加入:
APT::AutoClean "true";
或(更常用):
APT::Clean-Installed "false";
(安装后不立即清理,但 autoclean 会定期生效)
创建 service:
sudo nano /etc/systemd/system/apt-autoclean.service
内容:
[Unit]
Description=APT autoclean
[Service]
Type=oneshot
ExecStart=/usr/bin/apt-get autoclean
创建 timer:
sudo nano /etc/systemd/system/apt-autoclean.timer
内容:
[Unit]
Description=Run apt autoclean weekly
[Timer]
OnCalendar=weekly
Persistent=true
[Install]
WantedBy=timers.target
启用:
sudo systemctl enable --now apt-autoclean.timer
日志位置:
/var/log/journal/
编辑:
sudo nano /etc/systemd/journald.conf
修改或添加:
SystemMaxUse=200M
MaxRetentionSec=2week
重启服务:
sudo systemctl restart systemd-journald
用户级缓存,如浏览器、桌面环境。
tmpfiles.d:sudo nano /etc/tmpfiles.d/user-cache.conf
示例(7天清理):
d /home/*/.cache 0700 - - 7d
Debian 默认会自动清理 /tmp:
systemd-tmpfiles/usr/lib/tmpfiles.d/tmp.conf
默认:
d /tmp 1777 root root 10d
docker system prune -af
可加到 cron:
0 3 * * 0 docker system prune -af
apt autoclean + timerjournald.conf 限制大小如果你能说明是 服务器 / 桌面 / 容器环境,我可以给你一套更精确的自动清理方案。