Fixing “setlocale: LC_CTYPE: cannot change locale (UTF-8)” Error in CentOS 6 SSH Sessions


2 views

This common CentOS 6 locale issue occurs when the system tries to use UTF-8 encoding but the required locale files aren't properly generated. The error specifically mentions LC_CTYPE which handles character classification and case conversion.

# Current problematic locale settings
$ 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

First, check what locales are actually available on your system:

# List all generated locales
$ locale -a

# Check locale packages
$ rpm -qa | grep glibc-common

The most robust fix is to regenerate the locales:

# As root user:
$ sudo su -
# Install necessary package if missing
$ yum install glibc-common -y
# Generate the locale
$ localedef -v -c -i en_US -f UTF-8 en_US.UTF-8
# Verify generation
$ locale -a | grep en_US.UTF-8

For SSH sessions specifically, modify either /etc/environment or user's ~/.bashrc:

# Add to /etc/environment (system-wide)
LANG=en_US.UTF-8
LC_ALL=en_US.UTF-8

# Or add to ~/.bashrc (user-specific)
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

For SSH-specific cases, check the server's /etc/ssh/sshd_config:

# Ensure these lines exist:
AcceptEnv LANG LC_*
UseDNS no

After making changes, test with:

# Reload environment
$ source ~/.bashrc
# Or restart SSH service
$ service sshd restart
# Verify
$ locale

For minimal systems where you can't generate locales:

# Set to POSIX standard
export LC_ALL=POSIX
export LANG=POSIX

When working with CentOS 6 systems, you might encounter the following error when logging in via SSH:

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

This typically indicates a mismatch between your system's locale settings and the available locale definitions. The locale command output reveals more details:

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
[...]

The core issue stems from two main problems:

  1. The system is trying to use UTF-8 as a standalone locale (invalid format)
  2. The required locale definitions are either missing or improperly generated

First, check what locales are actually available on your system:

locale -a

If en_US.UTF-8 is missing from the output, we need to generate it.

For CentOS 6, use these commands to generate the required locale:

sudo localedef -v -c -i en_US -f UTF-8 en_US.UTF-8
sudo localedef -v -c -i en_US -f UTF-8 UTF-8

Edit your shell configuration file (~/.bashrc or /etc/environment) to ensure proper locale settings:

export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

Then apply the changes:

source ~/.bashrc

For a permanent system-wide solution, edit the locale configuration:

sudo vi /etc/sysconfig/i18n

Set the following values:

LANG="en_US.UTF-8"
LC_ALL="en_US.UTF-8"

After making these changes, verify with:

locale
locale -a

You should no longer see the warning messages, and all locale variables should point to valid definitions.

  • If locales still don't generate properly, check /usr/lib/locale/locale-archive permissions
  • For minimal installations, you might need to install additional packages: yum install glibc-common
  • In rare cases, you may need to rebuild the locale archive: sudo localedef --list-archive | sudo localedef --delete-from-archive