为 su 设置根路径

use*_*209 6 root su path environment-variables

当我(在我的 debian 机器上)使用 更改为 root 用户时su,显然 PATH 设置不正确。一些重要的目录如usr/local/sbin丢失。

/etc/profile我有以下几行(显然不是我写的标准默认内容):

if [ "`id -u`" -eq 0]; then
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin/:/usr/bin:/sbin:/bin"
else
    PATH="/usr/local/bin:/usr/bin:/bin"
fi
export PATH
Run Code Online (Sandbox Code Playgroud)

/etc/login.defs我有(也是默认的东西):

ENV_SUPATH PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Run Code Online (Sandbox Code Playgroud)

为什么我的 PATH 设置不正确su

小智 7

当被要求保护环境时,现代su不再PATH从调用者那里重置;如果您需要执行此操作su --login

来自/usr/share/doc/util-linux/NEWS.Debian.gz

- new 'su' (with no args, i.e. when preserving the environment) also
  preserves PATH and IFS, while old su would always reset PATH and IFS
  even in 'preserve environment' mode.
...
The first difference is probably the most user visible one. Doing
plain 'su' is a really bad idea for many reasons, so using 'su -' is
strongly recommended to always get a newly set up environment similar
to a normal login. If you want to restore behaviour more similar to
the previous one you can add 'ALWAYS_SET_PATH yes' in /etc/login.defs.
Run Code Online (Sandbox Code Playgroud)

  • 在升级到 _debian Buster_ 之后,我刚刚被这个咬了。这非常有帮助。 (2认同)