Bash – /etc/profile Not Sourced for `sudo su`

bashpathsudo

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?

Best Answer

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.

Related Question