Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What is the difference between the following commands:

sudo -s
sudo bash

I know for su I need to know the root password, and for sudo I have to be in the sudoers file, but once executed what is difference?

I know there is a difference between su and sudo -s because my home directory is /root after I execute su, but my home directory is still /home/myname after sudo -s. But I suspect this is just a symptom of an underlying difference that I'm missing.

share|improve this question
I prefer to use sudo su - This way you don't need the root password, and the - makes sure the home directory is set right. –  Jens Timmerman Apr 26 '13 at 10:34

2 Answers 2

up vote 20 down vote accepted

With su, you become another user — root by default, but potentially another user. If you say su -, your environment gets replaced with that user's login environment as well, so that what you see is indistinguishable from logging in as that user. There is no way the system can tell what you do while su'd to another user from actions by that user when they log in.

Things are very different with sudo:

  • Commands you run through sudo execute as the target user — root by default, but changeable with -u — but it logs the commands you run through it, tagging them with your username so blame can be assigned afterward. :)

  • sudo is very flexible. You can limit the commands a given user or group of users are allowed to run, for example. With su, it's all or nothing.

    This feature is typically used to define roles. For instance, you could define a "backups" group allowed to run dump and tar, each of which needs root access to properly back up the system disk.

    I mention this here because it means you can give someone sudo privileges without giving them sudo -s or sudo bash abilities. They have only the permissions they need to do their job, whereas with su they have run of the entire system. You have to be careful with this, though: if you give someone the ability to say sudo vi, for example, they can shell out of vi and have effectively the same power as with sudo -s.

  • Because it takes the sudoer's password instead of the root password, sudo isolates permission between multiple sudoers.

    This solves an administrative problem with su, which is that when the root password changes, all those who had to know it to use su had to be told. sudo allows the sudoers' passwords to change independently. In fact, it is common to password-lock the root user's account, so that all sysadmin tasks have to be done via sudo. In a large organization with many trusted sudoers, this means when one of the sysadmins leaves, you don't have to change the root password and distribute it to those admins who remain.

The main difference between sudo bash and sudo -s is spoofability. sudo -s looks in trusted locations to determine which shell to execute, whereas sudo bash causes sudo to run the first bash program in the PATH, which may not be the shell you intended it to run. There could be multiple bash executables on the system, in which case you might be tricked into running the wrong one; if someone knew you had $HOME/bin in your PATH ahead of /bin and could somehow get a bash program into your $HOME/bin directory, they could cause that program to do nasty things. sudo -s effectively can't be tricked that way, since it would require that the attacker gain root access to start with, and at that point dirty tricks are no longer necessary.

However, because sudo -s gives precedence to the SHELL variable over /etc/passwd when determining the shell to use, it may still be susceptible to spoofing by another path. If someone can modify a sudoer's environment prior to a sudo -s, they could get it to run any command they desired. sudo -s could also cause a security breach less directly through other environment variables like EDITOR and PAGER.

The solution to that is sudo -i, which is a relatively recent addition to sudo. This always gives you a root login shell and resets all but a few key environment variables. Roughly speaking, sudo -i is to sudo -s as su - is to su. Because sudo -i only looks at /etc/passwd and replaces the PATH, its security is as good as the security of the root account.

You might still want to use sudo -s for those situations where you know you want to remain in the same directory you were cd'd into when you ran sudo. It's still safer to sudo -i and cd back to where you were, though.

share|improve this answer
What do you mean by 'shell out of vi'? –  crisron Oct 20 at 5:01
@crisron: From within vi, type :sh and hit Enter. Now you're in a sub-shell, with all the privileges of the vi process that spawned that shell. If vi is running with root privileges, so will the shell. Or, you can run something other than a shell via :!cmd, read output from a command into the edit buffer via :r !cmd, etc. If all those are locked down, Makefile targets are shell scripts, and Vim has the :make command, which effectively lets you run arbitrary shell scripts from within the editor. The possibilities for mischief are far too immense for this comment box to hold. –  Warren Young Oct 20 at 5:57

From an ubuntuforums post I made a while ago:

Consider the following experiment:

applic@ion:~% sudo su
[sudo] password for applic:
root@ion:/home/applic# env > /tmp/sudo_su_env
root@ion:/home/applic# exit
applic@ion:~% sudo -s
applic .bashrc read...
root@ion:~% env >/tmp/sudo_s

Here are the differences I found:

With sudo -s:

reads $USER's ~/.bashrc

With sudo su:

reads /etc/environment
reads /root/.bashrc

Notice the difference in $HOME. Being root and having $HOME set to the normal user's home can cause problems. For example, if you run a graphical app, the normal user's ~/.Xauthority can get overwritten by root. This causes the normal user problems later on such as not being able to run certain graphical apps through cron.

To summarize:

                                     corrupted by user's 
        HOME=/root  uses root's PATH     env vars
sudo -i     Y       Y[2]                 N
sudo -s     N       Y[2]                 Y
sudo bash   N       Y[2]                 Y
sudo su     Y       N[1]                 Y
  1. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games probably set by /etc/environment
  2. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin

The bottom line is sudo -i is the proper command to run when you want a root shell that is untainted by the user's environment.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.