Environment-Variables

為 su 設置根 PATH

  • September 2, 2018

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

當被要求保護環境時,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.

引用自:https://unix.stackexchange.com/questions/466373