3 Ways to Fix SSH Locale Environment Variable Error

April 12, 2020

This article is going to show you 3 ways to fix SSH locale environment variable error. What is locale environment variable? In Linux, a locale consists of four categories of environment variables:

  1. LANGUAGE
  2. LC_ALL
  3. LC_*:  LC_TYPE, LC_NUMERIC, LC_TIME…
  4. LANG

These environment variables define the system language, monetary format, date and time format etc on your Linux distribution. You can check out your locale environment variables by running the locale command in terminal:

locale

Sample output:

LANG=zh_CN.UTF-8
LANGUAGE=zh_CN
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC=en_US.UTF-8
LC_TIME=en_US.UTF-8
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY=en_US.UTF-8
LC_MESSAGES="zh_CN.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=

You can see that the above output contains all four categories of locale environment variables.

On Ubuntu, you can set LANGUAGE, LANG and LC_* variables graphically in system settings > language support.

locale environment variable

Systemd-based Linux distributions (Debian 8+, Ubuntu 15.04+, Fedora, CentOS7+, Arch Linux) can use the following command to set each locale environment variable.

sudo localectl set-locale variable_name=value

For example, set LANG=en_US.UTF-8.

sudo localectl set-locale LANG=en_US.UTF-8

Locale changes may need re-login or reboot to take effect.

SSH Locale Environment Variable Error

When you ssh into a remote Linux server, you might see the following locale related error.

Failed to set locale, defaulting to C

or

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = (unset),
        LC_TIME = "zh_CN.UTF-8",
        LC_MONETARY = "zh_CN.UTF-8",
        LC_ADDRESS = "zh_CN.UTF-8",
        LC_TELEPHONE = "zh_CN.UTF-8",
        LC_NAME = "zh_CN.UTF-8",
        LC_MEASUREMENT = "zh_CN.UTF-8",
        LC_IDENTIFICATION = "zh_CN.UTF-8",
        LC_NUMERIC = "zh_CN.UTF-8",
        LC_PAPER = "zh_CN.UTF-8",
        LANG = "en_US.UTF-8"
        are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
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

Why does this locale error happen? Well, that’s because your SSH client forwards your locale environment variables from your local computer to the remote Linux server which doesn’t have the needed locale generated.

This happens a lot if you are not a native English speaker. Normally you would configure a non-English language on your local computer, but most Linux servers by default only have the C locale (aka POSIX locale) and English locales generated.

Now let me show you 3 ways to fix this error.

Method 1: Generate Locales on the Server

To fix this error, you can generate the needed locales on the Linux server. First open the /etc/locale.gen file on your server.

sudo nano /etc/locale.gen

Find the needed locale and remove the # sign to uncomment. For example, to generate zh_CN.UTF-8 locale, find this line:

#zh_CN.UTF-8 UTF-8

Remove and # sign. Save and close the file. Then run the following command to generate it.

sudo locale-gen

locale-gen reads /etc/locale.gen file to know what locales to generate. You can also generate multiple locales by uncommenting multiple lines in that file.

Method 2: Refuse Client Locale Environment Variable

You can tell your SSH server to refuse client locale environment variable. Open the SSH server configuration file on your Linux server.

sudo nano /etc/ssh/sshd_config

Find the following line.

AcceptEnv LANG LC_*

Change it to

AcceptEnv no

Save and close the file. Then restart SSH daemon.

sudo systemctl restart ssh

On RHEL, CentOS, Fedora, you need to run

sudo systemctl restart sshd

On Fedora/CentOS server, then are multiple locale-related SSH settings.

AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE

Simply comment out all of them and restart SSH daemon.

Method 3: Disable Locale Environment Variable Forwarding

We can also disable SSH locale environment variable forwarding to fix this error. Open the SSH client configuration file on your local computer.

sudo nano /etc/ssh/ssh_config

Find this line:

SendEnv LANG LC_*

Add a # sign at the beginning to comment it out. Save and close the file.

Need help?

Do you need help setting up this on your own service?
Please contact us and we’ll provide you the best possible quote!