16

I have PATH=foo set in /etc/profile

According to /etc/password the root and myaccount users have /bin/bash as the login shell.

When I log in as myaccount, the PATH has been set correctly from /etc/profile. If I then sudo su, PATH does not include the value set in /etc/profile.

If I sudo su - instead then /etc/profile is sourced. I know it is good practice to do this anyway but I don't see why it's necessary in order to source /etc/profile.

Why does sudo su clobber PATH, disregarding /etc/profile and how to I make it not?

2

1 Answer 1

25

sudo su is an abomination that should be cursed unto the ends of the earth.

Having got that out of the way, you'll be asking what you should do instead.

# sudo asks for *your* password and must be configured via /etc/sudoers
sudo -s             # Shell for superuser
sudo -i             # Login shell for superuser
sudo -u jimmy -s    # Shell for user "jimmy"
sudo -u jimmy -i    # Login shell for user "jimmy"

# su asks for the root password and usually requires no configuration
su                  # Shell for superuser
su -                # Login shell for superuser
su jimmy            # Shell for "jimmy"
su - jimmy          # Login shell for "jimmy"

Only login shells source /etc/profile.

3
  • "Only login shells source /etc/profile" but when I open a new terminal in my normal X session, that's not a login shell, but it has the correct PATH, so if that shell has it, why doesn't the su shell have it?
    – spraff
    Commented Dec 2, 2016 at 10:36
  • 1
    @spraff PATH is inherited from your login, which does run as a login shell. Commented Dec 2, 2016 at 10:39
  • @spraff In that shell: The value of PATH has been inherited from the login shell that started the X server for your user. When you use sudo, it erases the environment variables (almost all of them) for security reasons.
    – user232326
    Commented Dec 2, 2016 at 10:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .