以下是Debian系统中清理dmesg日志的常用技巧:
手动清空缓冲区
使用命令 sudo dmesg -c
直接清除当前内核日志缓冲区内容。
设置定时任务自动清理
通过 crontab -e
添加定时任务,例如每周日午夜执行 sudo dmesg -c
:
0 0 * * 0 sudo dmesg -c
。
利用logrotate工具轮转管理
sudo apt-get install logrotate
。/etc/logrotate.d/dmesg
,添加以下内容(示例为每周轮转,保留1份旧日志):/var/log/dmesg {
weekly
rotate 1
missingok
notifempty
compress
create 0644 root root
}
执行 sudo logrotate -f /etc/logrotate.conf
测试配置。限制内核日志缓冲区大小
编辑 /etc/sysctl.conf
,添加参数限制缓冲区大小(如 log_buf_len=1M
),然后执行 sudo sysctl -p
生效。
注意:清理前确保无需保留重要日志,避免影响系统故障排查。