-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8)

来源:互联网 发布:工程宝软件下载 编辑:程序博客网 时间:2024/05/02 04:32

Fixing the LC_CTYPE: cannot change locale (UTF-8) error on CentOS

If you’ve recently upgraded to CentOS 6.3 you might have seen the following error message after logging in to your system via SSH:

-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8)

Although it’s easy to asume the problem must be on the server-side (and in a part it is), but you actually have to fix an ssh_config setting on your local system.

SSH sends LC_* environment variables

The problem is that your SSH client is by default configured to send your LC_* environment variables. This means that SSH will try to set every LC_* variable you have set on your local (connecting) system on the remove system as well. The problem is that since CentOS 6.3 the name of the locale is not compatible with what some SSH clients (like the default OpenSSH client on Apple Mac OSx) has set. Let’s take a closer look:

Locale setting on local system

When you run:

printenv

you see which LC_* variables are set locally. On my Mac OSx Mountain Lion system it prints:

LC_TYPE=UTF-8

Locales available on the remote CentOS system

If you enter:

locale -a

you get a list of locales which you have available on your CentOS system. The locale you have set on your local system (“UTF-8″ in my case) should also be present here. When grep’ing the list:

locale -a | grep  ”UTF-8″

it quickly becomes clear that my the locale setting on my locale (Mac OSx) system is not available on the remote CentOS system.

Preventing OpenSSH from sending the LC_* variables

The fix is very easy. On your local system open your ssh_config file as root (usually in /etc/ssh_config or /etc/sshd/ssh_config) and comment this line:

SendEnv LANG LC_*

That’s it! From now on you will no longer get the annoying Bash error. Alternatively you can also set the locale setting of your locale system to something that matches your remote system, but this is the foolproof way that will always work, regardless of the locale config of your local and remote system.

原创粉丝点击