The command su - creates a new shell logged in as the root user. Other methods, such as sudo -i and sudo su -, use the sudo privilege escalation mechanism to achieve the same result. The appropriate command depends on the system's setup and the user's authentication context.
Detailed explanation of root shell commands
In Linux and other Unix-like operating systems, the root user is a special administrative account with complete system control. It is often necessary to obtain a shell with root privileges for system administration tasks. Several commands accomplish this, each with a different approach to privilege management and session handling.
1. The su command
The su command, short for "substitute user" or "switch user," is the traditional method for switching to another user account.
- Command:
su- By default, running
suwithout a username attempts to switch to therootuser. - Authentication: This method requires entering the
rootuser's password. - Environment: A basic
sucommand does not load therootuser's environment. The new shell inherits most of the environment variables from the original user, which can sometimes cause problems.
- By default, running
- Command:
su -- Adding the hyphen (
-) or--loginflag tellssuto start a full login shell for the target user. - Behavior: This changes the directory to the
rootuser's home directory (/root), loads the root's environment variables (e.g.,PATH,HOME,SHELL), and provides a clean root session. This is the recommended way to usesuwhen performing administrative work. - Authentication: The
rootuser's password must be provided.
- Adding the hyphen (
2. The sudo command
The sudo command ("superuser do") is the modern, more controlled alternative to su. It allows a user with the proper permissions to execute a command as another user (by default, root), authenticated with their own password.
- Command:
sudo -i- The
-iflag (short for--login) runs an interactive login shell as therootuser. - Authentication: The user must enter their own password, not the
rootpassword. - Behavior: This command simulates a new login session for the
rootuser, moving to the/roothome directory and loading therootenvironment. It provides a full, clean root shell, similar tosu -. - Security: This is generally the most secure and recommended method for obtaining a root shell on modern Linux distributions. It uses the
sudoersfile for access control and logs all activities, enhancing accountability.
- The
- Command:
sudo -s- The
-sflag (short for--shell) runs an interactive shell as therootuser but without creating a login session. - Authentication: Requires the user's own password, as configured in the
sudoersfile. - Behavior: The shell maintains the user's current directory and environment variables, similar to a simple
sucommand. This can be convenient for running a quick series of privileged commands without changing the working environment.
- The
- Command:
sudo su -- This command combines
sudowith the behavior ofsu -. - Behavior: The
sudocommand executessu -with root privileges. Becausesuis run byroot, it no longer requires a password. Thesu -then creates a full login shell for therootuser. - Authentication: This method only requires the user's own password to run the initial
sudocommand. The result is identical tosudo -i.
- This command combines
Comparison of root shell commands
| Command | Authentication | Environment | Use Case |
|---|---|---|---|
su |
root password |
Inherits user's | For quick, non-disruptive changes, but less secure. |
su - |
root password |
Loads root's |
When a full, standard root session is needed and the root password is known. |
sudo -i |
Your own password | Loads root's |
The recommended, most secure way for a permitted user to get a full root shell. |
sudo -s |
Your own password | Inherits user's | For running a sequence of root commands without changing the working environment. |
sudo su - |
Your own password | Loads root's |
Functionally identical to sudo -i, but less direct. |
Best practices and security considerations
- Minimize root access: Avoid running as root longer than necessary. After completing administrative tasks, use the
exitcommand orCtrl-Dto return to the normal user account. - Use
sudoinstead ofsu: Most modern Linux distributions disable therootaccount's password for security reasons. Usingsudoallows for better accountability, as a log is kept of which users performed which privileged actions. - Manage permissions carefully: Access to
sudois controlled by the/etc/sudoersfile. Administrators can use thevisudocommand to safely edit this file and define which users or groups can run specific commands with elevated privileges. - Use the right tool for the job:
- For a single administrative command, simply use
sudo <command>. - For a full root shell, use
sudo -i. - For a sequence of commands without changing the environment, use
sudo -s.
- For a single administrative command, simply use