As you stated in your question, the main difference is the environment.
sudo su -
vs. sudo -i
In case of sudo su -
it is a login shell, so /etc/profile
, .profile
and .bashrc
are executed and you will find yourself in root's home directory with root's environment.
sudo -i
is nearly the same as sudo su -
The -i
(simulate initial login) option runs the shell specified by the password database entry of the target user as a login shell. This means that login-specific resource files such as .profile
, .bashrc
or .login
will be read and executed by the shell.
sudo su
vs. sudo -s
sudo su
calls sudo
with the command su
. Bash is called as interactive non-login shell. So bash
only executes .bashrc
. You can see that after switching to root you are still in the same directory:
user@host:~$ sudo su
root@host:/home/user#
sudo -s
reads the $SHELL
variable and executes the content. If $SHELL
contains /bin/bash
it invokes sudo /bin/bash
, which means that /bin/bash
is started as non-login shell, so all the dot-files are not executed, but bash
itself reads .bashrc
of the calling user. Your environment stays the same. Your home will not be root's home. So you are root, but in the environment of the calling user.
Conclusion
The -i
flag was added to sudo
in 2004, to provide a similar function to sudo su -
, so sudo su -
was the template for sudo -i
and meant to work like it. I think it doesn't really matter which you use, unless the environment isn't important.
Addition
A basic point that must be mentioned here is that sudo
was designed to run only one single command with higher privileges and then drop those privileges to the original ones. It was never meant to really switch the user and leave open a root shell. Over the time, sudo
was expanded with such mechanisms, because people were annoyed about why to use sudo
in front of every command.
So the meaning of sudo
was abused. sudo
was meant to encourage the user to minimize the use of root privileges.
What we have now, is sudo
becomes more and more popular. It is integrated in nearly every well known linux distribution. The original tool to switch to another user account is su
. For an old school *nix veteran such thing like sudo
might seem needless. It adds complexity and behaves more likely to the mechanisms we know from Microsofts os-family, and thus is in contrary to the philosophy of simplicity of *nix systems.
I'm not really a veteran, but also in my opinion sudo
was always a thorn in my side, from the time is was introduced and I always worked around the usage of sudo
, if it was possible. I am most reluctant to use sudo
. On all my systems, the root account is enabled. But things change, maybe the time will come, when su
will be deprecated and sudo
replaces su
completely.
Therefore I think, it will be the best to use sudo
's internal mechanisms (-s
, -i
) instead of relying on an old tool such as su
.
ubuntu
which prevent users from standardsu -
. They created problem and now there are endless discussions on how to solve it. – jimmij Jul 24 '15 at 14:30su -
? Don't you think that poses a security hole in multi-user environments, where more than one person needs to have root access? – Erathiel Jul 24 '15 at 15:03sudo
, a users root enabling password never has to leave their control. (We do not live in a world where people refrain from insecure password practices, especially when it involves sending it to others.) Apart from that, it is useful to be able to restrict users to only being able to run a few commands withsudo
, if all a user needs root access for is to restart a service, why give them full root access? – Phizes Jul 24 '15 at 23:03su -
? Yes, one would have to set a root password, but that's trivial. – Phizes Jul 24 '15 at 23:05sudo
and prevent a root password. – Samuel Edwin Ward Jul 24 '15 at 23:10man sudo
. – Phizes Jul 24 '15 at 23:16sudo
gets the job done in probably the cleanest way. – Erathiel Jul 25 '15 at 14:57sudo
is insane - it always has been. It accepts shell globs to identify users! And PAM is way more secure. – mikeserv Jul 25 '15 at 15:04sudo bash
(or the shell of your choice)? have you matched differencies with the rest of the commands? It should match better withsudo -i
shouldn't it? – YoMismo Jul 29 '15 at 10:35env
) ofsudo -s
(which runs$SHELL
as root) andsudo bash
are 100% identical. – terdon Jul 29 '15 at 11:05sudo su
is extremely useful for becoming a non-root user. – Kyle Strand Dec 15 '15 at 23:09sudo su user
. Just dosudo -iu user
instead. – terdon Dec 16 '15 at 12:28-u
flag forsudo
. Semantically, though, I still prefersudo su
-- "as a super user (su-
), run (-do
) a single command: switch users (su
)." Plus it's simpler to type on a standard QWERTY keyboard. – Kyle Strand Dec 16 '15 at 21:04su
then i would just remove it. Reason: security. More programs with with SUID rights means more attack vectors. – Garo Apr 07 '21 at 11:22