Debian10系统locales及dpkg-reconfigure命令设置

今天想体验下Debian 10系统,通过本地终端SSH连接到VPS的时候又遇到了locales报错问题,虽然问题不大,但是作为一个半吊子技术渣渣强迫症,还是不能忍,花了点时间解决了下,这里做个记录希望能帮助到同样遇到这问题的朋友。

locale报错

系统是利用ISO镜像新安装的Debian10,登录用户非root,用的是安装过程中设置的有管理员权限的普通账户,本地SSH终端是iTerm2,SSH到远程VPS,执行apt install命令之后部分输出如下:

apt-listchanges: Can't set locale; make sure $LC_* and $LANG are correct!
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LC_CTYPE = "zh_CN.UTF-8",
    LC_TERMINAL = "iTerm2",
    LANG = "zh_CN.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory

非优雅解决办法

网上有些文章说,通过编辑/etc/locale.gen/etc/env或者/etc/enviroment等文件设置local,这些都不优雅,更骚的办法是通过系统自带的命令去修改,方法见如下所述。

解决办法

出现这个报错就是locale设置有问题,按照之前用Debian9的经验来说,其实只要一行命令重新设置locales就行,命令如下:

dpkg-reconfigure locales

但是如果是在Debian10上,通过普通用户登录,然后通过su切换到管理员权限,执行如上命令会出现如下提示:

root@cloudbool-Debian10:~# dpkg-reconfigure locales
bash: dpkg-reconfigure: command not found

但是查找dpkg-reconfigure这个命令却是存在于系统的:

root@cloudbool-Debian10:~# find / | grep dpkg-reconfigure
/usr/sbin/dpkg-reconfigure
/usr/share/bash-completion/completions/dpkg-reconfigure
/usr/share/man/de/man8/dpkg-reconfigure.8.gz
/usr/share/man/ru/man8/dpkg-reconfigure.8.gz
/usr/share/man/pt/man8/dpkg-reconfigure.8.gz
/usr/share/man/es/man8/dpkg-reconfigure.8.gz
/usr/share/man/man8/dpkg-reconfigure.8.gz
/usr/share/man/fr/man8/dpkg-reconfigure.8.gz

看可执行文件目录,是需要root权限才能执行,直接执行会报错,但是如果添加完全路径再执行,dpkg-reconfigure就不会报错了,但是另一个命令locale-gen又找不到了:

root@cloudbool-Debian10:~# /usr/sbin/dpkg-reconfigure locales
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LC_TERMINAL = "iTerm2",
    LC_CTYPE = "zh_CN.UTF-8",
    LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
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
/usr/bin/locale: Cannot set LC_CTYPE to default locale: No such file or directory
/usr/bin/locale: Cannot set LC_ALL to default locale: No such file or directory
/var/lib/dpkg/info/locales.postinst: 64: /var/lib/dpkg/info/locales.postinst: locale-gen: not found

通过查找locale-gen,发现这个命令也是在/usr/sbin目录下面,但是不知道环境变量PATH为什么没有加载那几个sbin的路径,既然没有自动加载,那我们手动加载好了,临时解决办法如下:

root@cloudbool-Debian10:~# export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

Update:
其实最好的方法是按Ctrl+D退出当前root用户,然后使用su –重新切换到root,PATH中就有sbin相关命令了,具体原因就是,Debian在2018年7月切换了su的实现方式,差别请查看这里:
https://sources.debian.org/src/util-linux/2.33.1-0.1/debian/util-linux.NEWS/
我拿手上机器试了下,发现除了Debian是这样,Ubuntu这两个命令也是有区别的,如下:

zoco@Ubuntu1804:~$ su
Password:
root@Ubuntu1804:/home/zoco# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
root@Ubuntu1804:/home/zoco# exit
zoco@Ubuntu1804:~$ su -
Password:
root@Ubuntu1804:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin