Skip to content

Difference Between `su` and `su -` Commands in Linux

In Linux, the `su` command allows a user to switch to a different user account. The `su` command can be followed by the name of the user to switch to, and will prompt the user for the password of the user they are trying to switch to.

The `su` command has a number of options that can be used to modify its behavior. One of these options is `-`, which stands for “login shell”. When the `-` option is used with the `su` command, it causes the su command to run a login shell for the specified user. This means that the user’s environment variables and profile scripts will be sourced as if they had logged in to the system normally.

For example, to switch to the root user and run a login shell, you would use the following command:

su - root 

Check for the system environment after switching to the root user.

`su - root` command example
`su – root` command example

On the other hand, if you omit the `-` option and just use the `su` command by itself, it will switch to the specified user without running a login shell. This means that the user’s environment variables and profile scripts will not be sourced, and the user will be in the same environment as before they switched users.

For example, to switch to the root user without running a login shell, you would use the following command:

su root 

See the system environment after switching to the root user.

`su root` command example
`su root` command example

In summary, the difference between `su` and `su -` is that `su` switches to the specified user without running a login shell, while `su -` switches to the specified user and runs a login shell. It’s important to understand this difference, as it can affect the environment in which you are working when switching to a different user account.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

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


Back to top