As a new Linux user, you may always face confusion regarding the difference between `su` command and `su -` command. In Linux, the `su` command is used to switch to another user account. However, there are two variations of the `su` command: `su` and `su -` (su hyphen).
Linux User Environment: Linux’s systems are multi-user environments. Whenever the Linux operating system creates a new shell session (after a new terminal is started on Linux) it starts preparing an environment for itself. This environment basically holds the Environment variable (Environmental depends on shell type, Bash is generally used by most of the Linux distributions).
For example, pwd command is an environmental variable.
The key difference between `su` and `su -` Command in Linux
|| Retains the current user’s environment variables
||Resets the environment variables to those of the target user
||Keeps the current working directory.
||Changes the working directory to the target user’s home directory.
||Retains the current user’s shell settings.
||Resets the shell settings to those of the target user.
||The target user’s PATH variable is not updated
||The target user’s PATH variable is updated to include the user-specific directories.
The major difference between `su` & `su -` commands in Linux
su command is an abbreviation for “substitute user” because it is used for switching to another user during a normal login session, but it is often mistaken as an abbreviation for “super user” as mainly su command is used for getting “super user” privileges as when su command is run without a username, we automatically become the superuser after giving the correct root password and after that, we will enter root’s default environment, by only typing the following command:
give the root user password
Now logged in as root user/super user
root user/super user
Directory Differences between `su` & `su -` command in Linux
When we switch from one user to another user using the normal `su` command the current directory remains the same as of the previous user like this
When we switch from one user to another user using su — command the current directory changes to the home directory of the target user like this:
Path Differences between `su` & `su -` commands in Linux
`su` command does not create a new User Environment (in the simple term they pretend to be the target user) but `su -` creates a totally new User Environment (in the simple term they are actually the target user) that we can check with environment variables like:
For `su` command:
For su – command
For more info check the `su` manual by using the following command
Frequently Asked Question
When to use which command in between `su` and `su -`?
`su` command is used when we need to switch to another user account temporarily and want to retain our current environment. For example, when we are executing specific commands as a different user.
`su -` command is used when we want to fully log in as another user and work within their environment. For example, as if we have logged in directly with their account.
It’s important to note that both commands require the appropriate user’s password to complete the switch.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!