What is the Difference Between ‘su -‘ and ‘su root’?

In Linux, the “su” command is utilized to switch to another user account from the current one. When used with the “” option or followed by a user name like “su root”, it allows the user to assume the privileges of the target user. Among the “su” command options, the “su -” and “su root” are used to switch to root user with some distinctive features. 

To explore that, this post will address the difference between the “su -” and the “su root” as follows:

  • What is the “su -” Command?
  • What is the “su root” Command?
  • What is the Difference Between “su -” and “su root” Commands?

What is the “su -” Command?

The “su –” command is utilized to switch to the root user account and create a login shell session. It means that it loads the environment of the target user, including the home directory, PATH, and other environment variables. 

Here are some examples of su – commands in Linux:

Example 1: Switch to the Root User Using the “su -” Command

To switch to the root user and run a command with root privileges, use the basic “su -” command:

$ sudo su -

It prompts to enter the root user password. Once you enter the password, it is switched to the root user, and the prompt changes to root@ubuntu. Now, users can execute commands with root privileges.

Example 2: Switch to Another User’s Account

To switch to another user’s account and start a new shell with their environment, use the “su – <username>” command. In our case, specify the “roger” as a username in the below command:

$ sudo su - roger

The output above shows that the user has been switched to roger user. 

What is the “su root” Command?

The “su root” command switches to the root user account without creating a login shell. It means that it does not load the root user’s environment. Here is the example of the su root command in Linux:

Example: Switch to the Root User Using the “su root” Command

To switch to the root user and run a single command with root privileges, use the “su root” command:

$ sudo su root

The output above shows that the user has been switched to root but the home directory is of the current user. This also represents the difference between the su – and su root.

Difference Between “su -” and “su root” Commands

The “su -” and “su root” commands in Linux are used to switch to the root user account. The significant differences between the two are:

su – Commands

  • This command switches the user to the root user account and sets the environment variables to those of the root user.
  • It also changes the current directory to the root user’s home directory.
  • This command is often used to execute commands as the root user.

su root Commands

  • This command switches the user to the root user account without changing the current environment variables or the current directory.
  • It requires the user to enter the root user’s password to authenticate.
  • This command is often used to perform administrative tasks as the root user without affecting the current environment.

Conclusion

In Linux, the “su –” is utilized to switch to a user account with a login shell session, and the “su root” switches to the root user account without a login shell. Both commands prompt to enter the root user password. Once you enter the password, it is switched to the root user.

This article has explained the difference between ‘su -‘ and ‘su root’ commands in Linux.