Software & AppsOperating SystemLinux

The Difference Between su -, sudo bash, and sudo sh Explained

Ubuntu 15

In the world of Unix and Linux, understanding how to navigate and control your environment is crucial. This includes knowing how to switch users and run commands with elevated privileges. In this article, we will delve into the differences between three commonly used commands: su -, sudo bash, and sudo sh.

Quick Answer

The main difference between su -, sudo bash, and sudo sh is that su - is used to switch to another user, sudo bash runs a Bash shell with root privileges, and sudo sh runs a shell (usually Dash) with root privileges. The key distinction is that su - requires the root password, while sudo uses the user’s password.

Understanding User Privileges

Before we dive into the specifics of each command, it’s important to understand the concept of user privileges in Unix and Linux systems. By default, most actions are performed as a regular user, which has limited permissions to protect the system from accidental damage or security breaches. However, certain tasks require higher privileges, typically those of the root user, which has unrestricted access to the system.

The su - Command

The su - command is used to switch to another user account, typically the root user. The su stands for “substitute user” or “switch user”. The - option is used to simulate a full login, which means that the environment variables and settings of the target user are loaded. Here’s an example:

su - root

In this example, you’re attempting to switch to the root user. You’ll be prompted to enter the root password. If you don’t have the root password, you won’t be able to use su - to log in as root.

The sudo bash Command

The sudo bash command runs the Bash shell with elevated privileges using the sudo command. The sudo command stands for “superuser do”, and it allows authorized users to run programs as the root user without needing to know the root password. Here’s an example:

sudo bash

When you run this command, you’re starting a new Bash shell with root-level privileges. You’ll be prompted to enter your own user password, not the root password. This is a key difference between sudo and su -.

The sudo sh Command

The sudo sh command runs the sh shell (usually Dash) with elevated privileges using sudo. Here’s an example:

sudo sh

Similar to sudo bash, it allows you to run a shell with root-level privileges. The specific shell that is executed may vary depending on your system configuration. Again, you’ll be prompted for your user password, not the root password.

Key Differences

In summary, su - is used to switch to another user, sudo bash runs a Bash shell with root privileges, and sudo sh runs a shell (usually Dash) with root privileges. The main difference between su - and sudo is that su - requires the root password, while sudo uses the user’s password.

Furthermore, sudo bash and sudo sh differ in the type of shell they run. Bash is a more feature-rich shell, while sh (Dash) is more minimal and faster. The choice between them depends on your specific needs and preferences.

Understanding these commands and their differences is an essential part of mastering Unix and Linux systems. We hope this article has shed some light on this topic and will serve as a helpful guide in your journey.

Can I use `su -` to switch to any user, or only the root user?

You can use su - to switch to any user, not just the root user. Simply replace "root" with the desired username in the command.

What is the difference between `sudo bash` and `sudo sh`?

The main difference between sudo bash and sudo sh is the type of shell they run. sudo bash runs the Bash shell, which is more feature-rich, while sudo sh runs the sh shell, usually Dash, which is more minimal and faster.

Do I need to know the root password to use `sudo bash` or `sudo sh`?

No, you do not need to know the root password to use sudo bash or sudo sh. Instead, you will be prompted to enter your own user password.

Can I use `su -` or `sudo bash` to switch to another user without knowing their password?

No, you need to know the password of the user you want to switch to when using su - or sudo bash. These commands require authentication to ensure the security of the system.

Which shell should I choose between Bash and sh (Dash)?

The choice between Bash and sh (Dash) depends on your specific needs and preferences. If you require a more feature-rich shell with advanced functionality, Bash is a good choice. However, if you prefer a minimal and faster shell, sh (Dash) is a suitable option.

Leave a Comment

Your email address will not be published. Required fields are marked *