Is there a difference between sudo su - root and sudo -u root -H /bin/bash?

73,877

Solution 1

su - # causes the user to run a login shell aka bash --login
     # the same as if the user had logged in as the root from the login prompt

and sudo su - is the same as sudo su - root specifying root is redundant.

sudo -u root -H /bin/bash again the -u root is redundant, sudo runs as root by default but the -H /bin/bash is run as an sudoer, the env vars SUDO_USER, SUDO_UID and SUDO_COMMAND are set to the callers name/uid and bash respectively.

But in this case, bash is not run as a login shell.

Solution 2

Obviously, they can differ if root shell isn't /bin/bash - this is possible for tiny or embedded Linux environments, or traditional for BSD systems which prefer /bin/[t]csh for root user. Also, `su -' drops all environment except a few variables related to the terminal. sudo's behavior on environment is configurable (see env_reset in /etc/sudoers). So, be careful with details...

Share:
73,877

Related videos on Youtube

Vizard
Author by

Vizard

Updated on September 18, 2022

Comments

  • Vizard
    Vizard over 1 year

    Like title says, is there a difference between these two commands :

    sudo su - root
    sudo -u root -H /bin/bash
    

    I'm using GNU/Linux, if that makes a difference.

  • Admin
    Admin about 12 years
    sudo -H /bin/bash does run bash. The -H option doesn't take an argument.
  • Admin
    Admin about 12 years
    -H just changes the $HOME to the target user's home directory. That's it. As the man page says, this might even be the default policy.