Difference between sudo -i and sudo su

What is the difference between sudo -i and sudo su?

21
2022-07-25 20:43:55
Source Share
Answers: 1

I think @slm misread the question, so providing another answer.

He did hit on the main point, about one being a login shell and the other not.
When running sudo -i the shell will become a login shell, and so it will read things like ~/.profile where as a non-login shell will only read ~/.bashrc.

When chaining sudo with su (as in sudo su), neither the sudo nor the su invoke a login shell. The equivalent to sudo -i when using su would instead be sudo su -l.

I personally consider sudo su to be along the lines of "useless use of cat" examples. You can get the same behavior with sudo -s.

There are basically 5 common ways of invoking a root shell via sudo

  • sudo su

    • non-login shell
    • sets HOME to /root
    • Prunes the environment
  • sudo -i

    • login shell
    • sets HOME to /root
    • Prunes the environment
  • sudo su -l

    • login shell
    • sets HOME to /root
    • Prunes the environment

    When invoking a shell, this is equivalent to sudo -i

  • sudo -s

    • non-login shell
    • sets HOME to /root
    • Prunes the environment

    When invoking a shell, this is equivalent to sudo su

  • sudo -Es

    • non-login shell
    • Leaves HOME alone
    • Leaves the environment alone (except for $PATH and $LD_LIBRARY_PATH iirc)

Note, that these rules only apply when using them to gain a shell. There is a difference between sudo -s somecommand and sudo su -c somecommand.

8
2022-07-25 22:01:10
Source