本文主要是介绍Linux下locale问题汇总,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
博文背景
有时候,ssh登录后会出现下面这个样子的问题
-bash: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
/bin/sh: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
有时候使用一些软件如apt,perl等会出现locale报错命令行查看locale会出现下面这个样子的问题
root@tlinux:~ # locale
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=C.UTF-8
LC_CTYPE="C.UTF-8"
LC_NUMERIC="C.UTF-8"
LC_TIME="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_PAPER="C.UTF-8"
LC_NAME="C.UTF-8"
LC_ADDRESS="C.UTF-8"
LC_TELEPHONE="C.UTF-8"
LC_MEASUREMENT="C.UTF-8"
LC_IDENTIFICATION="C.UTF-8"
LC_ALL=C.UTF-8
解决办法
这些问题的原因都是因为字符集被设置为了C.UTF-8但是本地却没有C.UTF-8的字符集文件,那么就有两个解决思路,方法一是换个字符集,方法二是安装字符集。
对于ssh来说,换个字符集,可以登录上去再换,登录上去再换的方法有:
1.直接编辑文件
root@tlinux:~ # cat /etc/default/locale
LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8
root@tlinux:~ # source /etc/default/locale
root@tlinux:~ # locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8
2.使用localectl命令进行操作
ubuntu@shenzhen:~ $ locale
LANG=C.UTF-8
LANGUAGE=
LC_CTYPE="C.UTF-8"
LC_NUMERIC="C.UTF-8"
LC_TIME="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_PAPER="C.UTF-8"
LC_NAME="C.UTF-8"
LC_ADDRESS="C.UTF-8"
LC_TELEPHONE="C.UTF-8"
LC_MEASUREMENT="C.UTF-8"
LC_IDENTIFICATION="C.UTF-8"
LC_ALL=
ubuntu@shenzhen:~ $ localectl set-locale LANG=en_US.UTF-8
==== AUTHENTICATING FOR org.freedesktop.locale1.set-locale ===
Authentication is required to set the system locale.
Authenticating as: ,,, (ubuntu)
Password:
==== AUTHENTICATION COMPLETE ===
ubuntu@shenzhen:~ $ source /etc/default/locale
ubuntu@shenzhen:~ $ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
对于ssh来说,也可以在登录的时候进行更换,登录时进行更换的方法是:
alias ssh='LC_ALL="en_US.UTF-8" ssh'
在.bashrc后面添加一行该别名配置即可在登录时指定客户端locale配置。
当然也可以在ssh客户端中配置SendEnv LANG LC_*
或在服务端配置AcceptEnv LANG LC_*
具体我没有测试成功。
对于安装字符集来说,有些机器好像就没有C.UTF-8的字符集,这个可以通过locale -a | grep -i c.utf
来查看,具体怎么装这个C.UTF-8我也不是很清楚,不过我有装一些其他字符集的方法:
在Debian下有(ubuntu下没有)中文简体字符集task-chinese-s
可以通过apt命令来进行安装操作。在Debian下和Ubuntu下都有一些常规字符集locales-all
可以通过apt命令来进行安装操作。在Centos8下有glibc-common langpacks-en glibc-all-langpacks glibc-langpack-en langpacks-zh_CN
都可以使用dnf来安装尝试一下。
包安装完了过后,就和上面ssh登录后更换字符集一样可以尝试进行字符集的更换。再赘述一遍:
ubuntu@shenzhen:~ $ locale
LANG=C.UTF-8
LANGUAGE=
LC_CTYPE="C.UTF-8"
LC_NUMERIC="C.UTF-8"
LC_TIME="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_PAPER="C.UTF-8"
LC_NAME="C.UTF-8"
LC_ADDRESS="C.UTF-8"
LC_TELEPHONE="C.UTF-8"
LC_MEASUREMENT="C.UTF-8"
LC_IDENTIFICATION="C.UTF-8"
LC_ALL=
ubuntu@shenzhen:~ $ localectl set-locale LANG=en_US.UTF-8
==== AUTHENTICATING FOR org.freedesktop.locale1.set-locale ===
Authentication is required to set the system locale.
Authenticating as: ,,, (ubuntu)
Password:
==== AUTHENTICATION COMPLETE ===
ubuntu@shenzhen:~ $ source /etc/default/locale
ubuntu@shenzhen:~ $ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
还有一些其他的解决办法,比如:
sudo dpkg-reconfigure locales
最后,祝君好运 !!!
这篇关于Linux下locale问题汇总的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!