Debian 日志出现乱码,通常是字符编码不一致或终端/工具不支持中文导致的。下面按常见场景给你解决方案。
查看当前locale:
locale
正常情况应有(或包含):
LANG=zh_CN.UTF-8
LC_ALL=zh_CN.UTF-8
如果没有 UTF-8,日志里的中文很容易乱码。
sudo dpkg-reconfigure locales
zh_CN.UTF-8或手动:
sudo locale-gen zh_CN.UTF-8
sudo update-locale LANG=zh_CN.UTF-8
reboot
如果你是 SSH 连 Debian:
journalctl -xe
如果乱码:
export LC_ALL=zh_CN.UTF-8
journalctl -xe
或只看英文避免乱码:
LC_ALL=C journalctl -xe
比如:
cat /var/log/syslog
解决:
locale
确保是 UTF-8,再用:
less /var/log/syslog
(less 对编码更友好)
转换编码查看:
iconv -f GBK -t UTF-8 /var/log/xxx.log
或写入新文件:
iconv -f GBK -t UTF-8 old.log > new.log
如果是你自己的服务:
# -*- coding: utf-8 -*-
-Dfile.encoding=UTF-8
如果你愿意,可以把:
发出来,我可以直接帮你定位是哪一种乱码。