Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I would like to know what is the difference between

  • su -
  • sudo bash
  • sudo sh

I cannot login as root while doing su - because I do not have the root password - I mean I have my user password with which I can sudo "command" but this password does not work for su - I can log in as root with sudo bash with my user password and I can login while doing sudo sh but then I get a command prompt in this form #

share|improve this question
add comment

3 Answers

up vote 13 down vote accepted

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.

share|improve this answer
2  
It's worth noting that you can log in as the super user by running sudo su - without a root password. Because sudo runs the su - command with root permissions, you will only be prompted for the user's password. This has the advantage of setting up the root user's environment, which may not be done by simply running a shell with sudo. –  Aaron D Feb 26 '13 at 2:19
    
Hi Aaron, how are you? What is your point? –  desgua Feb 26 '13 at 11:14
add comment

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:

share|improve this answer
    
Ok, thanks for reply but what is the default password for root then ? Where can I find it ? –  Patryk Aug 14 '11 at 11:41
    
@lordmonkey root doesn't have a password, and doesn't need one. sudo -i will give you a login shell like su - would, except sudo asks for your password, not root's. –  geirha Aug 14 '11 at 13:39
add comment

If the machine is not your personal computer but belong to a company and running a critical application, and you are only one of many system admins, then using sudo to run bash or sh can result in security issue.

Sudo is not a secure privilege delegation software.

share|improve this answer
3  
Welcome to Ask Ubuntu! This is a confusing answer. Many companies do configure sudo, in a secure manner, to allow employees to run some commands and not others. So actually sudo is can be used as "secure privilege delegation software." (Also, how does this distinguish sudo from the other commands asked about?) Maybe you can reword this to clarify exactly what you mean? –  Eliah Kagan Oct 9 '12 at 6:03
add comment

Your Answer

 
discard

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.