28

I’ve seen many comments here in Ask Ubuntu regarding preference in using sudo -s rather than sudo su.

For me, that two commands do exactly the same. But why is sudo -s supposed to be better?

Stefan Hamcke
  • 879
  • 1
  • 12
  • 29
fitojb
  • 2,188
  • 1
  • 16
  • 33
  • 1
    I think `sudo -i` or `sudo su` is better than `sudo -s` for reasons mentioned here: http://ubuntuforums.org/showpost.php?p=6188826&postcount=4 Also: this is a possible duplicate of http://superuser.com/questions/29/difference-between-comands-su-s-and-sudo-s – arrange Oct 04 '11 at 06:27
  • I think this question should be moved to unix.stackexchange.com – igi Dec 25 '11 at 08:48

4 Answers4

26

I think sudo -s is not "better", it's just different.

sudo -s

keeps your $HOME directory, so when you start programs, they will use your (and not root's) config files etc. Disadvantage: they can also change the ownership of your files in the $HOME directory, typical example is the .Xauthority file, but I have never experienced a problem with that.

sudo su or sudo -i

programs will use the root's home directory and its config files. This might be more intuitive, but it also means that it will not use any configuration you may have set for yourself (aliases, bash history,...).

arrange
  • 14,727
  • 4
  • 42
  • 32
  • 1
    Okay, I now understand the difference. Thank you! – fitojb Oct 04 '11 at 18:34
  • 1
    "sudo su" is not equivalent to "sudo -i". "sudo -i" is equivalent to "sudo su -" but "sudo su -" keeps the current working directory while "sudo -i" moves you into root's home – Gregory Pakosz Feb 22 '12 at 21:50
13

Because sudo -s will not change your environment variables, especially $HOME, which will stay HOME=/home/USER. If you use sudo su this will change to HOME=/root which can have negative side effects when programs you start try to access your user's home directory.

So basically it's like sudo vs gksudo when running graphical programs.

htorque
  • 63,950
  • 40
  • 194
  • 219
5

Aside from the already mentioned differences with environment variables, I find sudo su silly and wasteful because you are asking sudo to run su as root, and ask it to run a shell as root. Why ask a program to ask a program to give you a root shell, when you can just have the first program give you that shell? Does that matter in any practical sense? No, I'm just pedantic.

psusi
  • 37,033
  • 2
  • 68
  • 106
  • You’re right, it’s somewhat redundant. – fitojb Oct 04 '11 at 18:31
  • Well, not completely so: if you are using `sudo su` to be root, yes, just go `sudo -i`, for example. But it does help when you want to become a different user: `sudo su - myuser`. – hggdh Oct 04 '11 at 22:31
  • 2
    There is another difference: `sudo -i` will change to `/root` directory and `sudo su` will stay in the directory you are. – laurent Oct 05 '11 at 00:05
  • 1
    @hggdh, `sudo -u myuser`. – psusi Oct 05 '11 at 13:38
3

I would disagree with other answers when considering the cases when you want to log in as a user who do not have a shell. That is a very common case with users assigned to services.

Using "sudo su" will start the shell specified by the user. However in the case of a user assigned to server process, the shell is /usr/bin/false and the logging will fail. Instead you should enter as:

sudo -sHu <server-user>

that will log you in and cd to its home path, even if the user does not have a shell assigned