What is the difference between 'su -' , 'sudo bash' and 'sudo sh'?

Background

In Linux (and Unix in general), there is a SuperUser named Root. The SuperUser can do anything and everything, and thus doing daily work as the SuperUser can be dangerous. You could type a command incorrectly and destroy the system. Ideally, you run as a user that has only the privileges needed for the task at hand. In some cases, this is necessarily Root, but most of the time it is a regular user.

su -

This command is used to login at root account.
By default, the Root account password is locked in Ubuntu. This means that you cannot login as Root directly or use the su command to become the Root user. However, since the Root account physically exists it is still possible to run programs with root-level privileges. This is where sudo comes in - it allows authorized users (normally "Administrative" users) to run certain programs as Root without having to know the root password.

See How to Enable Root Account.

sudo sh

This command runs "sh" as a super user.
The sh utility is a command language interpreter that shall execute commands read from a command line string, the standard input, or a specified file.

sudo bash

This command runs "bash" as a super user.
Bash is the shell, or command language interpreter. Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). It is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools standard. It offers functional improvements over sh for both programming and interactive use. In addition, most sh scripts can be run by Bash without modification.

References: 1, 2, 3.


su -:

This will change your user identifier and inherit the environment variables as if you had logged in with that user. Normally you would use the format su - <userid> to login as the user . If you drop the "userid" it assumes you are trying to login as root - which you can't (unless you change the root password).

sudo bash & sudo sh:

Anything after the sudo is a program to run - so in these cases you are asking to run a couple of shells - bash and dash respectively. use man bash and man sh for more details on the different shells.


Related Questions:

  • Ask UbuntuHow to know my root password?
  • Stack OverflowWhat Linux shell should I use?