The different shades of sudo

How you call the sudo command will dictate what environment variables you have, and what profile is used. Let's find out which is which.

The different shades of sudo

If you're a Linux administrator, chances are you make use of sudo on a daily basis. Why? Because it's the best way of safely running admin tasks without actually logging out and logging in as the root user. 

Although you've probably used sudo for a long time, you might not know of the different "shades" of sudo. What do I mean by that? Simple--you can call upon sudo with different types of shells, each of which can be used for specific situations. What are the different ways to use sudo? Let me explain. 

SEE: Choosing your Windows 7 exit strategy: Four options (TechRepublic Premium)

The command sudo su calls bash with an interactive non-login shell. In this instance, bash only executes .bashrc and you do not have the root user's environment variables. The command sudo su - calls an actual login shell, so /etc/profile, .profile, and .bashrc are executed. 

When this is used, you'll find yourself in the root home directory with root's environmental variables in place. The command sudo -i is nearly the same as sudo su -, only it runs the shell specified by the password database entry of the target user as a login shell. This means that login-specific resource files such as .profile, .bashrc, or .login will be read and executed by the shell. 

The command sudo /bin/bash uses a non-login shell, so all dot-files are not executed. However, bash does read the .bashrc of the calling user and your environmental variables remain the same.

 Effectively, you are the root user, but using the environment of the calling user. The command sudo -s only reads the $SHELL variable and executes the content. And those, my friends, are the different shades of sudo. Which way do you sudo?

Also see


Image: Jack Wallen