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?

2
  • 1
    Are you running "su" or running "su -". ALWAYS use "su -", never use "su" you miss a load of config. Commented Apr 9, 2019 at 9:00
  • @Owl Wo, I didn't know that. Extremely useful, thanks. For those who wants to know more: unix.stackexchange.com/q/7013/101324 Commented Nov 27, 2019 at 18:57

1 Answer 1

11

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.
2
  • 3
    I've just now been bitten by this, after upgrading to debian Buster. This was super helpful. Commented Feb 24, 2019 at 21:54
  • See also unix.stackexchange.com/q/460478/5132 . Commented Dec 18, 2019 at 13:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.