You probably already know that Ubuntu does not give you an option to create a root password while you install Ubuntu.
It uses the sudo mechanism and if you have to run a command as root, you add sudo in front of it and use your account's password:
sudo command_to_run
This mechanism works fine. But if you are in a situation where you have to actually switch to the root user, instead of running a command as root with sudo, you can do that too.
The easiest way to switch to root user which does not require you to setup a root account in Ubuntu is to use the sudo command with the -i
flag as shown here:
sudo -i
In fact, there are multiple ways you can switch to root user and each reacts differently, specifically for the directory, the environment you will log in, the shell, and what password it will require.
Here's a brief comparison of each command:
Method | Home Dir | Environment | Shell | Password Required |
---|---|---|---|---|
sudo -i |
/root | Clean root | root's | User password |
sudo su - |
/root | Clean root | root's | User password |
su - |
/root | Clean root | root's | Root password |
su |
Current | Mixed | root's | Root password |
sudo -s |
Current | User's | User's | User password |
Now, let's take a look at each method in a detailed manner.
1. Using sudo -i command (recommended)
By far, the sudo -i
is one of the most secure and recommended ways to switch to the root user in Ubuntu.
When you execute this command, it simulates an initial login to the root account. This means you'll get a completely clean environment, just as if root had logged in directly to the system.
sudo -i
The best part of using the method is this command will prompt for your user password (not the root password). When executed successfully, it will change your working directory to the root's home directory (/root).
2. Using sudo su - command
When you execute the sudo su -
command, you'd first need to authenticate with your own user password through sudo, then the system will switch to the root user as if you had run su -
.
The hyphen (-) is important here as it ensures you get a clean environment, loading root user's profile and environment variables.
sudo su -
Difference between sudo -i and sudo su -
In case you are wondering about the difference between the sudo -i
command and the sudo su -
command, there are two significant differences: process handling and efficiency.
When you use the sudo -i
command it creates a direct single process this way, it gets you into a root shell and gives you a clean environment as if the root user had just logged in.
On the other hand, the sudo su -
command creates a chain of processes where sudo first elevates privileges to run su, which then creates the root shell - essentially going through two steps to achieve the same result.
While both commands get you a root shell with a clean environment, sudo -i
is more efficient with cleaner logging (single log entry versus multiple entries for sudo su -).
In practice, the sudo su -
command is mainly used for compatibility with legacy systems and scripts where traditional su behavior is expected.
3. Using su/su - command
The traditional su
command switches directly to the root user account, but it requires the root password to be set.
su
su
command won't work unless you've specifically set a root password. The thing to note here is that when you execute the su
command without any flags, it will maintain your current environment variables and working directory, which can sometimes cause confusion or problems.
This is the reason why I recommend executing the su
command with hyphen (su -
). This way, you get a clean environment and it also changes to root's home directory and load root's environment variables.
su -
However, using su
in any form bypasses sudo's logging mechanisms, making it harder to track who accessed root and when.
4. Using sudo -s command
Unlike sudo -i
, this command starts a root shell while maintaining your current user's environment variables and staying in your current working directory. It uses your defined shell (as specified in /etc/passwd) rather than root's default shell.
sudo -s
This method is particularly useful when you need root privileges but want to maintain your customized environment settings, aliases, and functions. This can be beneficial during development or when you need to work with files in your current directory structure.
Wrapping Up...
In this tutorial, I went through multiple ways you can switch to a root account in Ubuntu. I highly recommend not setting up a root account as it is not recommend and somewhat poses a security threat.