macOS SSH 登陆 Linux 后中文乱码问题

2017-11-07 2061点热度 0人点赞 0条评论

这种情况一般是终端和服务器的字符集不匹配,MacOS 默认的是 UTF-8 字符集。

输入 locale 可以查看字符编码设置情况。我在 Mac 下使用了 zsh 替代了 bash,而 oh-my-zsh.zshrc 配置文件中没有设置默认编码,所以需要本地和服务器端设置相同编码。

  1. 修改.zshrc 配置文件(如果是 bash 则修改对应.bash_profile.bashrc 文件):
$ vim ~/.zshrc
  1. 在文件内容末端添加:
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
  1. 使修改生效:
$ source ~/.zshrc

如果设置之后出现以下问题:

-bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)

则可能是服务器上缺少对应字符集。

The error "bash: warning: setlocale: LC_ALL: cannot change locale (en_US)" occurs when the remote server does not understand the locale "en_US.UTF-8". The fix is to generate the locale using the command "sudo locale-gen en_US.UTF-8" and update the locale repository to store this locale, such that future connections(ssh) can understand this locale. The command "sudo dpkg-reconfigure locales" updates the local repository with the newly generated locale, i.e en_US.UTF-8.

在服务器上安装对应的字符集就可以修复(以 Ubuntu 为例):

sudo locale-gen en_US.UTF-8
sudo dpkg-reconfigure locales

有时字符集问题也可能是本地找不到与远程相匹配的字符,编辑 服务器上的/etc/ssh/ssh_config 文件,注释此行:

# SendEnv LANG LC_*

即可解决问题。

SilverLining

也可能是只程序猿

文章评论