解决终端中文UTF-8环境问题没有应用的问题,关联问题:btop启动报错无UTF-8。

环境说明

  • 操作系统:Debian 13 Trixie (Linux)

问题描述

  • root 用户无法使用中文 locale
  • btop 运行报错:No UTF-8 locale detected!

解决步骤

1. 生成 UTF-8 locale

bash
123
apt install -y locales
sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen
locale-gen

2. 设置系统默认 locale

bash
1
echo 'LANG="zh_CN.UTF-8"' > /etc/default/locale

3. 安装中文字体

bash
1
apt install -y fonts-wqy-microhei fonts-wqy-zenhei

4. 配置 ~/.bashrc

bash
12
export LANG=zh_CN.UTF-8
export LC_ALL=zh_CN.UTF-8

注意:仅设置 LANG 不足够,需要同时设置 LC_ALL,否则 locale charmap 仍返回 ANSI_X3.4-1968

5. 解除 root 用户 locale 限制

问题文件/root/.profile

该文件包含以下限制内容:

text
12345
# Installed by Debian Installer:
#  no localization for root because zh_CN.UTF-8
#  cannot be properly displayed at the Linux console
LANG=C
LANGUAGE=C

解决方法:删除这些限制行

验证

bash
12
source ~/.bashrc
locale

应显示:

text
12
LANG=zh_CN.UTF-8
LC_ALL=zh_CN.UTF-8