Search code examples
bashrootsudoenv

sudo has different environments, why?


I realize that:

sudo -i
env

is different from:

sudo -i
sudo env

They are both sudo, why are they different?

In my case, I realize the PATHs are very different, so sometimes things can't run with "sudo blah" but it works when I first log into sudo, then run the command. (Like my other question #4976658)


Solution

  • There are expected differences, such as SUDO_USER, SUDO_UID, and SUDO_GID.

    And then there is PATH.

    When you run

    sudo -i
    env
    

    you should expect root's PATH, but when you run

    sudo -i
    sudo env
    

    you should expect a default PATH.

    I think you will find that this is due to the "secure path" option, that both Ubuntu and Fedora have adopted.

    I agree it's annoying. There are many other threads about this behavior. :-/

    For example, see this stackoverflow thread: sudo changes PATH - why?