一、问题

  • 登录时报错环境变量目录找不到

  • 系统中仍有配置文件(如/etc/locale.conf)强制设置 LANG=en_US.UTF-8,优先级高于/etc/profile

  • 即使手动export LC_ALL=C,系统仍会先读取/etc/locale.conf的配置,导致locale命令仍加载不存在的en_US.UTF-8

Welcome to Alibaba Cloud Elastic Compute Service !

-bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US.UTF-8): No such file or directory
-bash: warning: setlocale: LC_COLLATE: cannot change locale (en_US.UTF-8): No such file or directory
-bash: warning: setlocale: LC_MESSAGES: cannot change locale (en_US.UTF-8): No such file or directory
-bash: warning: setlocale: LC_NUMERIC: cannot change locale (en_US.UTF-8): No such file or directory
-bash: warning: setlocale: LC_TIME: cannot change locale (en_US.UTF-8): No such file or directory
-bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
-bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)

二、修复方法

# 1. 强制清空当前会话的locale变量,设置为内置C(立即生效)
unset LC_ALL
unset LANG
export LC_ALL=C
export LANG=C

# 2. 覆盖/etc/locale.conf(系统级locale配置,优先级最高)
echo "LANG=C" > /etc/locale.conf
echo "LC_ALL=C" >> /etc/locale.conf

# 3. 覆盖/etc/profile的locale配置(全局登录生效)
sed -i '/LC_ALL=en_US.UTF-8/d' /etc/profile
sed -i '/LANG=en_US.UTF-8/d' /etc/profile
echo "export LC_ALL=C" >> /etc/profile
echo "export LANG=C" >> /etc/profile

# 4. 覆盖当前用户的.bashrc(登录会话生效)
sed -i '/LC_ALL=en_US.UTF-8/d' ~/.bashrc
sed -i '/LANG=en_US.UTF-8/d' ~/.bashrc
echo "export LC_ALL=C" >> ~/.bashrc
echo "export LANG=C" >> ~/.bashrc

# 5. 立即加载新配置,无需重启
source /etc/locale.conf
source /etc/profile
source ~/.bashrc