3 Ways to Fix SSH Locale Environment Variable Error

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.

Next Step

I hope this tutorial helped you solve the SSH locale problem. You might also want to set up SSH keys to protect your server from brute force attacks.

Questions or suggestions are always welcome. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂

Rate this tutorial
[Total: 6 Average: 4.7]

7 Responses to “3 Ways to Fix SSH Locale Environment Variable Error

  • In Fedora 29 no etc/locale.gen

    • Xiao Guo An (Admin)
      5 years ago

      On Fedora 29, you need to install the glibc-langpack-** packages. Then locales will automatically installed to /usr/lib/locale/ directory.

      For example, to install Chinese locale,

       sudo dnf install glibc-langpack-zh

      You can search the package name for your language like below.

      sudo dnf search glibc-langpack
  • Apachedoc
    4 years ago

    Help Linux mint..

    bash: warning: setlocale: LC_ALL: cannot change locale (en_US.utf-8)
    bash: warning: setlocale: LC_ALL: cannot change locale (en_US.utf-8)
    /bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.utf-8)

    no file or directory… when I tried sudo nano /etc/ssh/ssh_config It was empty

    I am on Linux mint forum by foss under Aaron knosp

  • Sarfaraz
    4 years ago

    sudo vim /etc/environment

    # Insert the following in :

    ——————————————-
    ANG=en_US.utf-8
    LC_ALL=en_US.utf-8

    :wq

    ——————————————

    #This seems to also fix it, found this solution somewhere on the net a while ago.

  • Thank you so very much! Tip #3 worked for when I was at my wit’s end as I could not connect to a remote server using SSH. This fixed it immediately!

  • Gwyneth Llewelyn
    1 year ago

    Is there a way to selectively send the locale, i.e. disallowing it just for a few servers (which might have broken locales), while still allowing it to anywhere else?

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here