Strovent SystemsSign in
Reference

Locale warning after SSH login, "setlocale: LC_ALL: cannot change locale"

Why a shell warns that it cannot change the locale right after an SSH login, how the client's locale variables reach the host, and three ways to fix it.

bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)

The login works, but a warning like this appears, often with other programs repeating it. It is not an SSH failure. The shell was told to use a locale that the host does not have.

What happens

A locale is a language and character setting such as en_US.UTF-8. Programs pick it from environment variables. The locale(7) manual gives the order: LC_ALL first if it is set and not empty, then a variable with the name of the category, such as LC_CTYPE, and then LANG.

When the value is not a valid locale on that machine, setlocale(3) leaves the locale unchanged and returns an error. That is what the shell reports. The name in the brackets is the locale that could not be set.

The value can come from your device. SendEnv in the OpenSSH client configuration names the variables the client sends to the server, and AcceptEnv in the server configuration names those the server copies into the session. The manuals say both default to none, so a configuration that sends LANG and LC_* was set by someone. The client may send en_US.UTF-8 while the host has only other locales.

See what the host has

On the host, run:

locale
locale -a

locale shows the values in effect. locale -a lists all locales available on the host. The locale named in the warning is not in that list.

Fixes

1. For this session, choose a locale from the list. Replace C.UTF-8 below with a name that locale -a printed:

export LANG=C.UTF-8
unset LC_ALL

Put it in the shell startup file on the host to keep it. LC_ALL is unset because it overrides LANG and the category variables.

2. Stop sending the locale. In the OpenSSH client configuration, a - before a name removes a variable that was set earlier:

Host example.com
    SendEnv -LANG -LC_*

The host then uses its own defaults.

3. Add the locale on the host. Ask the administrator to install or generate it. The localedef(1) manual describes how locales are compiled from definition files. This needs privileges on the host.

If Mosh is involved

Mosh needs a UTF-8 locale on the host and fails at startup without one. See Mosh "needs a UTF-8 native locale to run".

Sources

Spotted a mistake? Email [email protected].