CentOS Local设置,消除告警: warning: Setting locale failed

Tags: centos 

目录

情况1

经常遇到这样的问题,虽然不影响命令运行,但是总是出现warning,很讨厌:

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = (unset),
	LC_CTYPE = "zh_US.UTF-8",
	LANG = "zh_CN.UTF-8"
	are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").

出现这个告警是因为local设置的是zh_CN.UTF-8,但是系统上没有中文local。

根据How to Set Up System Locale on CentOS 7localectl list-locales查看,发现全都是en_开头的:

$ localectl list-locales
en_AG
en_AG.utf8
en_AU
en_AU.iso88591
en_AU.utf8
...

根据Perl warning Setting locale failed in Debian,可以下面的命令生成local:

localedef -i zh_CN  -f UTF-8 zh_CN.UTF-8

这个命令在CentOS上也适用。

情况2

在另一台机器上又遇到了类似问题,ssh登录时提示:

-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory

执行一些命令时(主要是perl脚本),也会出现和上面类似的情况。

变量LC_CTYPE的值为UTF-8:

$ echo $LC_CTYPE
UTF-8

根据warning: setlocale: LC_CTYPE: cannot change locale中的说法,这个LC_CTYPE是从登录的MAC上上传的参数,MAC上的这个变量值确实也是UTF-8,在目标机器上,用locale命令看到LC_CTYPE变量值也明显不同:

$ locale
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=en_US.UTF-8
LC_CTYPE=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=

在目标机器的/etc/locale.conf中明确设置LC_CTYPE:

$ cat /etc/locale.conf
LANG="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"

重新登录,问题消失。


推荐阅读

Copyright @2011-2019 All rights reserved. 转载请添加原文连接,合作请加微信lijiaocn或者发送邮件: [email protected],备注网站合作

友情链接:  系统软件  程序语言  运营经验  水库文集  网络课程  微信网文  发现知识星球