问题:
服务器是ubuntu,用Mac的iterm2 ssh连上去,终端显示中文乱码,也不能输入中文,然而本地终端可以显示和输入。
解决方法:
这种情况一般是终端和服务器的字符集不匹配,MacOSX下默认的是utf8字符集。
输入locale可以查看字符编码设置情况,而我的对应值是空的。
因为我在本地和服务器都用zsh替代了bash,而且使用了oh-my-zsh,而默认的.zshrc没有设置为utf-8编码,所以本地和服务器端都要在.zshrc设置,步骤如下,bash对应.bash_profile或.bashrc文件。
1.在终端下输入
1 | vim ~/.zshrc |
2.在文件内容末端添加:
1 | export LC_ALL=en_US.UTF-8 |
接着重启一下终端,或者输入source ~/.zshrc使设置生效。
设置成功的话,在本地和登录到服务器输入locale回车会显示下面内容。
1 | LANG="en_US.UTF-8" |
这时,中文输入和显示都正常了。
3.如果设置之后出现以下问题
-bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
解决办法:
1 | sudo locale-gen en_US.UTF-8 |
解释:
1 | 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. |
(大概意思就是服务器上没有安装这个编码的字符集,安装之后设置一下就支持了)
参考:
oh-my-zsh中文乱码问题
ubuntu