设置 su 的根路径

设置 su 的根路径

当我(在我的 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

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

ENV_SUPATH PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

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

答案1

当被要求保护环境时, Modernsu不再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.

相关内容