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.su -
? Don't you think that poses a security hole in multi-user environments, where more than one person needs to have root access?su -
? Yes, one would have to set a root password, but that's trivial.