8

When I am changing (on my debian box) to the root user with su, apparently the PATH is not correctly set. Some vital directories like usr/local/sbin are missing.

In /etc/profile I have the following lines (apparently standard default stuff not written by me):

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

In /etc/login.defs I have (also default stuff):

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

Why is my PATH not correctly set when I do su?

Yaron
  • 4,289
  • 1
    Are you running "su" or running "su -". ALWAYS use "su -", never use "su" you miss a load of config. – Owl Apr 09 '19 at 09:00
  • @Owl Wo, I didn't know that. Extremely useful, thanks. For those who wants to know more: https://unix.stackexchange.com/q/7013/101324 – Clément Nov 27 '19 at 18:57

1 Answers1

10

Modern su no longer resets PATH from the caller when asked to preserve the environment; if you need this to happen execute su --login instead.

From /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.