scp tab completion error: F-bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US.UTF-8)

1. Problem description

During a certain Linux operation, the function scp command was used, and the following directory could not be completed using Tab. The error was reported: -bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US.UTF-8): No such file or directory


Related links: character map file `UTF-8’ not found

2. Cause handling

1) As shown above, the prompt is that there is a problem with the Linux character set;

2) Check execution: locale -a and check the character encoding installed by the system, as shown below: There is no en_US.UTF-8

localectl status #Check current settings

System Locale: LANG=en_US.UTF-8
VC Keymap: cn
X11 Layout: cn
#Initial default after reinstallation
localectl status #The output is as follows
System Locale: LANG=en_US.UTF-8
VC Keymap: us
X11 Layout: us


ls /usr/lib/locale/ #Check the current rain gear path, and there is no English displayed.
C.utf8 zh_CN.gbk zh_HK.utf8 zh_SG.utf8 zh_TW.utf8
zh_CN zh_CN.utf8 zh_SG zh_TW
zh_CN.gb18030 zh_HK zh_SG.gbk zh_TW.euctw

locale #The default output is as follows
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.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=
#Current character set
echo $LANG #output
en_US.UTF-8

3) Execute: find / -name locale*

/etc/locale.conf
/usr/include/c++/8/bits/locale_facets_nonio.h
/usr/include/c++/8/bits/locale_classes.h
/usr/include/c++/8/bits/locale_facets_nonio.tcc
/usr/include/c++/8/bits/locale_facets.h
/usr/include/c++/8/bits/localefwd.h
/usr/include/c++/8/bits/locale_classes.tcc
/usr/include/c + + /8/bits/locale_facets.tcc
/usr/include/c++/8/bits/locale_conv.h
/usr/include/c++/8/locale
/usr/include/bits/types/locale_t.h
/usr/include/bits/locale.h
/usr/include/locale.h
/usr/bin/localectl
/usr/bin/localedef
/usr/bin/locale
/usr/lib64/python3.6/locale.py
/usr/lib64/python3.6/__pycache__/locale.cpython-36.opt-1.pyc
/usr/lib64/python3.6/__pycache__/locale.cpython-36.opt-2.pyc
/usr/lib64/python3.6/__pycache__/locale.cpython-36.pyc
/usr/lib/locale
/usr/share/perl5/locale.pm
/usr/share/X11/locale
/usr/share/X11/locale/locale.alias
/usr/share/X11/locale/locale.dir
/usr/share/man/man7/locale.7.gz
/usr/share/man/man1p/localedef.1p.gz
/usr/share/man/man1p/locale.1p.gz
/usr/share/man/man3p/localeconv.3p.gz
/usr/share/man/man0p/locale.h.0p.gz
/usr/share/man/man1/locale.1.gz
/usr/share/man/man1/localectl.1.gz
/usr/share/man/man1/localedef.1.gz
/usr/share/man/man3/locale.3pm.gz
/usr/share/man/man3/localeconv.3.gz
/usr/share/man/man5/locale.conf.5.gz
/usr/share/man/man5/locale.5.gz
/usr/share/locale
/usr/share/locale/locale.alias
/usr/share/bash-completion/completions/localectl
/usr/share/i18n/locales

#Inquire
grep -r "LC_ALL" /usr/ /etc/ #Output
/bin/dracut:export LC_ALL=C
/etc/profile.d/lang.sh: export LC_ALL
/bin/check-security-updates:export LC_ALL=C
/bin/pmdiff:export LC_ALL=POSIX
/lib/dracut/modules.d/10i18n/parse-i18n.sh: export LC_ALL

#default
echo $LC_ALL #The output is empty
echo $LC_CTYPE #is also empty


4) Check, use LANG=”zh_CN.UTF-8″, modify it to: en_US.UTF-8

5) Directly execute: export LC_CTYPE=zh_CN.UTF-8 and export LC_ALL=zh_CN.UTF-8, or write /etc/profile.d/lang.sh, and the verification is passed:

localedef -i en_US -f UTF-8 en_US.UTF-8 #Invalid site, error:
[error] ?"UTF-8": ?
[error] "ANSI_X3.4-1968": 

echo "export LC_ALL=en_US.UTF8" >> /etc/profile
source /etc/profile

6) Later, I encountered a similar error, as shown below:

Execute: yum check-update –security, the prompt is as follows, C.UTF-8 is currently used

Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 4:03:41 ago on Wed Sep 13 03:46:54 2023.
No security updates needed, but 179 updates available

export LC_ALL=en_US.UTF8 #The error is reported as follows
-bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF8): No such file or directory
-bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF8)

The above error is that the system has set the default region_language. The character set is en_US.UTF-8, but the corresponding locale file is not defined in the system; we need to manually generate this locale file. implement:

vim /etc/environment #Add the following two lines
 LANG="en_US.UTF-8"
 LC_ALL=

source /etc/environment
#Production configuration file
localedef -v -c -i en_US -f UTF-8 en_US.UTF-8 #The error is reported as follows
failed to set locale!
[error] character map file `UTF-8' not found: No such file or directory
failed to set locale!
[error] default character map file `ANSI_X3.4-1968' not found: No such file or directory

yum install glibc-langpack-zh glibc-locale-source glibc-langpack-en -y
#Execute again:
localedef -v -c -i en_US -f UTF-8 en_US.UTF-8 #The output is as follows

[warning] LC_NAME: field `name_gen' not defined
[warning] LC_IDENTIFICATION: field `audience' not defined
[warning] LC_IDENTIFICATION: field `application' not defined
[warning] LC_IDENTIFICATION: field `abbreviation' not defined
[verbose] LC_CTYPE: table for class "upper": 2388001817131 bytes
[verbose] LC_CTYPE: table for class "lower": 18446744073709551615 bytes
[verbose] LC_CTYPE: table for class "alpha": 18446744073709551615 bytes
[verbose] LC_CTYPE: table for class "digit": 18446744073709551615 bytes
[verbose] LC_CTYPE: table for class "xdigit": 18446744073709551615 bytes
[verbose] LC_CTYPE: table for class "space": 18446744073709551615 bytes
[verbose] LC_CTYPE: table for class "print": 18446744073709551615 bytes
[verbose] LC_CTYPE: table for class "graph": 18446744073709551615 bytes
[verbose] LC_CTYPE: table for class "blank": 18446744073709551615 bytes
[verbose] LC_CTYPE: table for class "cntrl": 18446744069414584898 bytes
[verbose] LC_CTYPE: table for class "punct": 18446744073709551615 bytes
[verbose] LC_CTYPE: table for class "alnum": 18446744073709551615 bytes
[verbose] LC_CTYPE: table for class "combining": 18446744073709551615 bytes
[verbose] LC_CTYPE: table for class "combining_level3": 18446744073709551615 bytes
[verbose] LC_CTYPE: table for map "toupper": 0 bytes
[verbose] LC_CTYPE: table for map "tolower": 0 bytes
[verbose] LC_CTYPE: table for map "totitle": 0 bytes
[verbose] LC_CTYPE: table for width: 0 bytes

Log out and log in again to verify, no more errors are reported, as follows:


Executing export LC_ALL=en_US.UTF8 no longer reports an error: