`sudo` and `su` are two important commands in Unix-based systems like Linux that give you access to administrative tasks. But while they seem to perform a similar function, the commands `sudo -i` and `sudo su -` have some significant differences under the hood.
To understand these differences, we first need to delve into what sudo and su are and how they function. After this, we’ll dig into the specifics of `sudo -i` and `sudo su -` and outline their differences.
What are sudo and su?
In Unix and Unix-like operating systems, sudo (which stands for “superuser do”) is a command that allows users to run programs with the security privileges of another user (normally the superuser, or root). Its primary purpose is to enhance system security. Instead of sharing the root password with a user who needs to perform a few administrative tasks, you can grant limited root access to that user with sudo.
su (short for “substitute user” or “switch user“), on the other hand, is a command used to switch to a different user in the system. When used without specifying a username, su defaults to switching to the root account.
sudo -i Command
The `sudo -i` command is designed to start a root shell with root’s environment variables. This is similar to a root user logging in to a shell session, hence the `-i` option, which stands for “login shell”. This command initiates a new shell session with the environment variables of the root user, including any path updates or user-specific settings.
When you run `sudo -i`, you:
- Switch to the root home directory (/root).
- Are given the root’s shell (which could be different from your user’s shell, e.g., bash or sh).
- Get root’s environment variables (not retaining the user’s environment).
sudo su - Command
The `sudo su -` command is a combination of sudo and su that aims to mimic a root login shell. The – after su signifies a login shell, much like `-i` does for sudo. However, `sudo su -` handles the user environment differently than `sudo -i`.
When you run `sudo su -`, you:
- Switch to the root home directory.
- Are given the root’s shell.
- Get the root’s environment variables and also inherit the environmental variables of the su command.
sudo -i and
sudo su -
While `sudo -i` and `sudo su -` both provide a shell with root privileges, they differ in how they handle the user environment:
- Shell initialization files: The commands read different shell initialization files. For `sudo -i`, it reads .bashrc of the root user. On the other hand, `sudo su -` reads .bash_profile, .bash_logout, and .bashrc of the root user.
- Environment Variables: `sudo -i` uses the environment variables of the root user exclusively. Conversely, `sudo su -` uses the environment variables of the current user, which are then replaced by those of the root user. This could lead to subtle differences in command behavior if the two sets of environment variables differ significantly.
- Command History: With `sudo -i`, commands entered are stored in the root’s command history, whereas `sudo su -` stores commands in the invoking user’s command history.
- Command Usage: From a usage perspective, `sudo -i` is a single command, while `sudo -i` is a combination of two commands. This makes sudo -i slightly more efficient.
In conclusion, while `sudo -i` and `sudo su -` can often be used interchangeably, it’s important to remember that they have slightly different behaviors. Your choice between the two commands will depend on your specific needs at any given time. However, it’s always crucial to use these commands responsibly, given the high level of access and control they provide.