> ## Content Index
> Fetch the complete content index at: https://itsfoss.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Switching to Root User in Ubuntu
- URL: https://itsfoss.com/ubuntu-switch-root-user/
- Published: 2024-11-05T10:36:09.000Z
- Updated: 2024-11-05T13:21:46.000Z
- Description: Learn various of switching to the root user in Ubuntu and understand the difference between them.
- Author: Kabir Thapar
- Tags: Quick Tip, #hide

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
```

![Easiest way to switch to root account in Ubuntu without setting up a root account](https://itsfoss.com/content/images/2024/10/Easiest-way-to-switch-to-root-account-in-Ubuntu-without-setting-up-a-root-account.png)

In fact, there are multiple ways you can switch to [root user](https://itsfoss.com/root-user-ubuntu/) 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
```

![](https://itsfoss.com/content/images/2024/10/Use-sudo--i-to-switch-to-root-account-in-Ubuntu.png)

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 -
```

![](https://itsfoss.com/content/images/2024/10/Use-the-sudo-su---command-to-log-in-as-a-root-user-in-Ubuntu.png)

### 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.

[Netdata: Monitoring and troubleshooting transformed.Slash your time to detect, troubleshoot and resolve infrastructure performance anomalies. Netdata: architected for speed, automated for easy.![](https://itsfoss.com/content/images/icon/favicon-1.ico)netdata![](https://netdatacloud20.kinsta.cloud/wp-content/uploads/2022/03/netdata-cloud-dashboard-1.png)](https://netdata.cello.so/IYNENvCf9Rr?ref=itsfoss.com)

## 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
```

![Use the su command to log in as a root user in Ubuntu](https://itsfoss.com/content/images/2024/10/Use-the-su-command-to-log-in-as-a-root-user-in-Ubuntu.png)

🚧

In a default Ubuntu installation, the root account is locked. This means the `su` command won't work unless you've specifically [set a root password](https://itsfoss.com/ubuntu-set-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 -
```

![Use the su - command to log in as a root user in Ubuntu](https://itsfoss.com/content/images/2024/10/Use-the-su---command-to-log-in-as-a-root-user-in-Ubuntu.png)

However, using `su` in any form bypasses sudo's logging mechanisms, making it harder to track who accessed root and when. 

📋

This method is most useful in recovery situations or when sudo is not available, but it's generally not recommended for regular Ubuntu system administration.

## 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
```

![Use the sudo sudo -s command to log in as a root user in Ubuntu](https://itsfoss.com/content/images/2024/10/Use-the-sudo-sudo--s-command-to-log-in-as-a-root-user-in-Ubuntu.png)

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.