sudo and su are two important commands in Unix-based systems like Linux that let you do administrative tasks. Although they seem similar, the commands sudo -i and sudo su – have key differences.
To understand these differences, we first need to look at what sudo and su are and how they work. Then, we’ll explain sudo -i
and sudo su -
and highlight their differences.
What are sudo and su?
In Unix and Unix-like systems, sudo (short for “superuser do”) lets users run programs with the security privileges of another user (usually the superuser or root). Its main purpose is to improve system security. Instead of sharing the root password with someone who needs to do a few admin tasks, you can give them limited root access with sudo.
su (short for “substitute user” or “switch user”) is a command used to switch to a different user in the system. If you don’t specify a username, su switches to the root account.
The sudo -i
Command
The sudo -i command starts a root shell with root’s environment variables. This is like the root user logging into a shell session. The -i option stands for “login shell”. This command opens 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).
- Get the root’s shell (which might be different from your user’s shell, like bash or sh).
- Use root’s environment variables (not keeping the user’s environment).
The sudo su -
Command
The sudo su – command combines sudo and su to mimic a root login shell. The – after su means a login shell, similar to -i 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.
- Get the root’s shell.
- Use the root’s environment variables and also inherit the environment variables of the su command.
Differences Between sudo -i
and sudo su -
While sudo -i and sudo su – both give a shell with root privileges, they handle the user environment differently:
- Shell initialization files: The commands read different shell initialization files. `sudo -i` reads .bashrc of the root user. `sudo su -` reads .bash_profile, .bash_logout, and .bashrc of the root user.
- Environment Variables: `sudo -i` uses only the environment variables of the root user. `sudo su -` uses the environment variables of the current user, which are then replaced by those of the root user. This can cause differences in command behavior.
- Command History: With `sudo -i`, commands are stored in the root’s command history. With `sudo su -`, commands are stored in the invoking user’s command history.
- Command Usage: `sudo -i` is a single command, while `sudo su -` 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, they have slightly different behaviors. Your choice between the two commands will depend on your specific needs. Always use these commands responsibly, as they provide high-level access and control.