Understanding the Differences Between su, sudo su, sudo su -, and sudo -i

Understanding the Differences Between su, sudo su, sudo su -, and sudo -i

In the world of Linux and Unix-like systems, understanding user privileges and how to switch between them is crucial. Tools like su, sudo su, sudo su -, and sudo -i help manage user permissions, but they can be confusing for beginners. This article will break down these commands, explaining their differences and when to use each one.

1. The su Command

The su command stands for “substitute user” or “switch user.” It allows a user to switch to another user account without logging out of their current session. By default, when you type su, it switches to the root user (the superuser with full administrative privileges). Here’s how it works:

  • Syntax: su [username]
  • Example: If you want to switch to the root user, you would simply type su and enter the root password. If you want to switch to another user, for example, john, you would type su john.

When you use su, you inherit the environment of the target user but remain in the current directory. This means that if you were in /home/user before running su, you will stay in /home/user even after switching to another user.

Key Points:

  • su switches users, typically to the root user.
  • You inherit the target user’s environment.
  • You stay in the current working directory.

2. The sudo su Command

The sudo command allows a permitted user to execute a command as the superuser or another user, as specified in the sudoers file. When you combine sudo with su, you essentially tell the system to switch users with root privileges without needing the root password.

  • Syntax: sudo su [username]
  • Example: Typing sudo su will switch you to the root user, provided your user account has sudo privileges. You will need to enter your own password, not the root password.

Using sudo su gives you root privileges, but like su, it keeps you in the same working directory and does not fully load the root user’s environment.

Key Points:

  • sudo su switches to the root user using your sudo privileges.
  • You use your own password instead of the root password.
  • The current directory remains unchanged, and you inherit the target user’s environment.

3. The sudo su - Command

The sudo su - command is a slight variation of sudo su, but with a critical difference: the - (hyphen). This command not only switches to the root user but also initiates a login shell. A login shell loads the full environment of the target user, including their PATH, environment variables, and default directory.

  • Syntax: sudo su - [username]
  • Example: Typing sudo su - switches to the root user and takes you to the root’s home directory (/root). It loads the root user’s environment fully, as if you logged in as root directly.

This command is particularly useful when you want to simulate a complete login session as the root user. For example, if you need to ensure that you are using the exact environment that the root user would have, sudo su - is the appropriate command.

Key Points:

  • sudo su - switches to the root user and loads a full login shell.
  • You use your own password with sudo privileges.
  • The working directory changes to the root’s home directory, and the environment is fully loaded.

4. The sudo -i Command

The sudo -i command is another way to initiate a root shell with sudo privileges. It is similar to sudo su - but with a few nuanced differences. The -i stands for “interactive,” and this command mimics the behavior of logging in as the root user interactively.

  • Syntax: sudo -i
  • Example: When you type sudo -i, you switch to the root user, just like with sudo su -. However, sudo -i is designed to offer an interactive root shell that may behave slightly differently from sudo su -.

One key difference is how sudo -i handles shell configurations. While sudo su - loads the root user’s environment, including all shell initialization files like .bashrc and .profile, sudo -i might not source all of these files in the same way, depending on your system’s configuration.

Key Points:

  • sudo -i provides an interactive root shell.
  • It mimics logging in as the root user, similar to sudo su -.
  • The environment is loaded, but the behavior of shell initialization files may differ slightly.

5. When to Use Each Command

Knowing the differences between these commands helps you decide which to use in various scenarios:

  • su: Use this when you need to switch to another user account temporarily without changing the current working directory or loading a full environment.
  • sudo su: This is handy when you need to switch to the root user and have sudo privileges, but you don’t want to load a full login shell or change directories.
  • sudo su -: Opt for this when you need to fully assume the root user’s environment, including all environment variables and the root’s home directory. It’s the closest thing to logging in as root directly.
  • sudo -i: Use this for an interactive root shell, especially when you need to execute multiple root-level commands in a session that behaves like a direct root login.

6. Conclusion

Understanding the differences between su, sudo su, sudo su -, and sudo -i is crucial for effective system administration on Linux and Unix-like systems. Each command serves a specific purpose, from simple user switching to fully assuming the root user’s environment. By choosing the right command for the task, you can work more efficiently and avoid potential pitfalls, especially when dealing with critical system operations

Fedya Serafiev

Fedya Serafiev

Fedya Serafiev owns the website linuxcodelab.eu. He finds satisfaction in helping people solve even the most complex technical problems. His current goal is to write easy-to-follow articles so that such problems do not arise at all.

Thank you for reading the article! If you found the information useful, you can donate using the buttons below: