Debian日志回收与清理指南
可以回收与常用方式
使用 logrotate 回收日志
/var/log/syslog {
weekly
rotate 4
compress
delaycompress
missingok
notifempty
create 0640 root adm
}
含义:每周轮转、保留4份、压缩旧日志、延迟压缩、文件缺失不报错、空文件不轮转、轮转后新建文件权限为0640、属主root、属组adm。sudo logrotate -f /etc/logrotate.conf
清理 systemd 日志 journal
sudo journalctl --vacuum-time=2weeks
sudo journalctl --vacuum-size=100M
sudo journalctl -u nginx --vacuum-time=3d
手动清理与注意事项
rm -rf /var/log/* 清理全部日志,可能造成系统或审计信息丢失;如确需清理,务必先备份并逐项确认。快速检查与设置建议
ls /etc/cron.daily/logrotate
sudo logrotate -f /etc/logrotate.conf
SystemMaxUse=100M
MaxRetentionSec=2weeks
修改后重启服务:sudo systemctl restart systemd-journald