Switching to user root

Why switch to root ?

When in a shell as a non-root user, you have to use sudo to execute a command which requires root privileges e.g.:

sudo chown henribenoit:_www *

That’s the clean way of doing it: use a non-root user and only use sudo when required. But sometimes it just drives me crazy to prefix most commands with a sudo and I’d just rather switch to the root user, do all I need to do and then switch back.

The alternatives

There are a couple of ways to do this:

  • sudo -i
  • sudo su
  • sudo -s
  • sudo bash

All of them will allow you to switch to the root user but give you an environment more or less mixed with the previous user’s environment. Let’s see what each of these commands actually does.

In the sections below, I’ll execute the following command after switching to root with the respective commands:

env | grep "SHELL=
USER=
PATH=
PWD=
HOME=" | grep -v OLDPWD | sort -u

When running it before switching to root, I do get:

HOME=/Users/henribenoit
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/henribenoit
PWD=/Library/WebServer/Documents
SHELL=/bin/bash
TESTPWD=/tmp
USER=henribenoit

Note that I’ve updated the PATH adding the directory /Users/henribenoit to check whether the PATH is being reset and I’ve also added a dummy environment variable TESTPWD to check whether it’s still in there after switching to root. I made these 2 changes executing the following:

export PATH=$PATH:/Users/henribenoit
export TESTPWD=/tmp

sudo -i

Of all four options, sudo -i is the one which will give you the purest environment.

Running the command above, you will get:

HOME=/var/root
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/henribenoit
PWD=/var/root
SHELL=/bin/sh
SUDO_USER=henribenoit
USER=root

So it did take over the PATH I had defined before switching users but changed the home directory, the current directory, the shell used and the current user. Also the additional dummy variable was not taken over.

sudo su

Running the command above after using sudo su, returns the following:

HOME=/var/root
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/henribenoit
PWD=/Users/henribenoit
SHELL=/bin/sh
SUDO_USER=henribenoit
USER=root

Here the PATH and the current directory were not changed after switching to root. The rest was changed and the dummy variable is also not present anymore.

So the difference between sudo -i and sudo su seems to be that sudo -i additionally changes the current directory.

sudo -s

This command takes over even more of the environment before the user switch:

HOME=/Users/henribenoit
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/henribenoit
PWD=/Users/henribenoit
SHELL=/bin/bash
SUDO_USER=henribenoit
USER=root

So the home directory, the path, the current directory and the shell did not change after using sudo -s. So only the current user was changed to root and the dummy variable is not available anymore.

The difference between sudo su and sudo -s is that the latter does not change the shell and home directory.

sudo bash

This one gives you the exact same environment as the previous one:

HOME=/Users/henribenoit
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/henribenoit
PWD=/Users/henribenoit
SHELL=/bin/bash
SUDO_USER=henribenoit
USER=root

Even if you check all variables returned by env, you will see that there is no difference.

Which one to use ?

As always, it depends on what you will do after switching to root.

Changing the home folder will:

  • change the command history you are accessing
  • have an impact on some program installers

I usually do not have an issue with installers referencing the HOME directory. But in some cases I find it useful to keep the history I had with the previous user. In this case, I rather go for sudo -s or sudo bash.

Obviously using a different shell will force you to potentially use a different syntax. That’s why I only use sudo su if I need to the HOME directory to change to root’s home directory.

If you’ve already navigated to the directory you want to work in, the you shouldn’t use sudo -i as it will change the current directory. If it’s the first command you run after opening a shell, the it probably doesn’t really matter.

So in most cases I do use sudo -s (it’s 2 characters shorter than sudo bash ;-)) and in some rare cases sudo su. I personally never use sudo -i (anyway keeping sudo -s and sudo su in mind is already a lot for my small brain and remembering a third option will cause some overflow…).

And remember the additional password check which gets on your nerve when prefixing your commands with sudo once in a while might save you some day (especially if you start cleaning up files early in the morning before even having your first coffee of the day). So only switch to the root user if you really have to. Otherwise execute all commands you can with another user and only the ones which need root privileges with the root user.

Leave a Reply

Your email address will not be published. Required fields are marked *