Author: willblog, original: https: //blog.csdn.net/networken/article/details/97951173
The Linux system uses the English character set by default, and other characters such as the Chinese character set will not be installed.
View current character set
$ echo $LANG
en_US.UTF-8
Install character set
Use the locale command to see the character set used by the current system
$ locale
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=
Check whether the system has Chinese character set support
# locale -a | grep CN
bo_CN
bo_CN.utf8
ug_CN
ug_CN.utf8
zh_CN
zh_CN.gb18030
zh_CN.gb2312
zh_CN.gbk
zh_CN.utf8
If you did not execute the following command to install
# CentOS6.x:
yum groupinstall chinese-support
# CentOS7.x
yum install -y kde-l10n-Chinese
yum reinstall -y glibc-common
# Define character set
localedef -c -f UTF-8-i zh_CN zh_CN.UFT-8
# Confirm successful loading
locale -a
Modify the system character set
Modify the configuration file of the system character set:
# echo 'LANG="zh_CN.UTF-8"'>/etc/locale.conf
# source /etc/locale.conf
Or temporarily change the character set:
LANG="<character set>"
LANG="zh_CN.UTF-8"
# Or write character set environment variables to profile
vim /etc/profile
source /etc/profile
Verify character set modification
# echo $LANG
zh_CN.UTF-8
# locale
LANG=zh_CN.UTF-8
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER="zh_CN.UTF-8"
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT="zh_CN.UTF-8"
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL=
Modify the ssh terminal character set
If you follow the above method to modify the settings of the Chinese language, it still does not work, pay attention to the encoding selected by the SSH terminal, such as xshell, for example, select Chinese or UTF8 as the encoding of the terminal.
If the system interface still appears garbled, install the following packages:
yum groupinstall "fonts"-y
If it is still garbled, enter the fonts installation path and execute the following command
[ root@localhost ~]# cd /usr/share/fonts/[root@localhost ~]# fc-cache -fv
Take CentOS7 container image as an example
View the default configuration
# docker run -it --rm centos sh
sh-4.2# echo $LANG
sh-4.2# locale
LANG=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=
sh-4.2# locale -a
C
POSIX
en_US.utf8
Take the configuration of zh_CN.GB18030 character set as an example
yum install -y kde-l10n-Chinese
yum reinstall -y glibc-common
localedef -c -f GB18030 -i zh_CN zh_CN.GB18030
# Verify that the Chinese language pack is loaded successfully zh_CN.gb18030
# locale -a
C
POSIX
en_US.utf8
zh_CN.gb18030
Modify character set configuration
$ cat /etc/locale.conf
LANG="en_US.UTF-8"
$ echo 'LANG="zh_CN.GB18030"'>/etc/locale.conf && source /etc/locale.conf
$ echo "export LC_ALL=zh_CN.GB18030">>/etc/profile && source /etc/profile
# Verify that the configuration takes effect
$ echo $LANG
zh_CN.GB18030
Dockerfile example:
The docker container environment needs to make the corresponding character set image based on the dockerfile, and add the following content to the custom dockerfile:
# cat Dockerfile
FROM centos
LABEL Maintainer dockerhub.com
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& yum -y install kde-l10n-Chinese \
&& yum -y reinstall glibc-common \
&& localedef -c -f GB18030 -i zh_CN zh_CN.GB18030 \
&& echo 'LANG="zh_CN.GB18030"'>/etc/locale.conf \
&& source /etc/locale.conf \
&& yum clean all
ENV LANG=zh_CN.GB18030 \
LC_ALL=zh_CN.GB18030
Recommended Posts