Difference Between su and su -

What is Difference Between su and su – Command in Linux?

Linux has a sea of commands for different purposes; for optimal use and management of Linux, you must be familiar with different commands. We try to familiarize you with the functions of various Linux commands so that after purchasing a Linux VPS, you can benefit from the features of Linux and achieve your goals. In previous articles, we introduced you to the sudo command and its difference from the su command so that you can become an expert in managing user permissions and improving Linux security.

The su command is used to switch to another user account in Linux. Thesu command is known as the switch command among Linux users because it changes the user ID during a normal login session. The su command, like other Linux commands, has different options for different purposes. The option “-,” which stands for “login shell,” is one of the common options to change the behavior of the su command. Using the “-” argument forces the su command to run the login shell for the specified user and changes the environment variables and working directory for the current user account.

The difference between su and su- commands boils down to environment and environment variables. Since the difference between su and su – is unclear to the users, we briefly review su vs. su- in this article. To better understand the difference between su and su – read this article till the end.

Definition of environment variables

Environment variables in Linux are dynamic values used to define the characteristics of the environment that executes the shell processes. Also,  Environment variables influence the configuration and customization of the operating system’s behavior and various programs. Linux environment variables are used to specify default paths, set system parameters, set priorities, and more.

Linux environment variables contain information about user settings, system settings, and operating system-related data. Since the variables are part of the shell environment, users can access them throughout the system by any program or process and change the environment variables. The Linux system includes many variables that, in this part of the article, we will talk about the environment variables related to the su command.

You can run theexportcommand to access a list of your system variables.

Some Linux environment variables such as PATH (to specify search paths for executable files), USER (current user name or user ID), HOME (providing the user’s home directory), SHELL (providing the executed shell to the system), LOGNAME (identifier of the user you are logged in with) are predefined, and some of them are created by users to store custom data. You can use the ~/.bashrc or ~/.bash_profile configuration file to set environment variables.

To access environment variables, use the following syntax:

$VARIABLE_NAME or ${VARIABLE_NAME}

For example, to access the PATH value, use the following command:

$PATH

Or

echo $PATH

Using PATH variable values, the system can find the path and location of executed commands or programs.

Note: The /etc/profile file contains the default PATH variables.

To access the values of the environment variables that affect the su command, such as HOME, SHELL, USER, LOGNAME, and PATH, you can use the syntax we used to access the values of the PATH variable.

su vs. su -: what is the difference?

As we explained at the beginning of the article, the su and su- commands are two different methods to change the user account and switch to another user account. By using thesucommand without the “-” argument, you can switch to another user account while preserving the environment variables belonging to the original user. But thesu - command, while switching you to another user account, clears environment variables. In the following, we will compare su and su- in more detail:

su command:

By running the su command, you can be transferred to another user account that you specify, and if you do not specify a user account, you will enter the Root user account by default, while your current user environment, environment variables, and profile scripts will be preserved. By running the su command, you switch to another user account without running the login shell, and the environment variables set for your current user account or working directory remain unchanged.

For example, to log in to a specific user account such as Opera while no changes are applied to the environment variables, enter the following command:

$ su Opera

By running the previous command, you will be transferred to the Opera user account without running the login shell, and the environment variables and profile scripts will be preserved. It is as if the user is in the same environment before changing the user account and just logging in to another user account.

Note: To switch to the Opera user account, you must know the user account password you are transferring to(Opera) because you will be asked.

su –  command:

If you want to change your user environment in addition to switching to another user account, the su – command will be helpful. If you use the su – command to switch to your desired user account, the values of the environment variables and the directory of your desired user account will replace the values related to your current working environment, and all the environment variables belonging to the main user will be deleted.

The su – command is the best way to configure the environment completely. Also, the su -l, and su –login commands have the same function as ]su – and execute the login shell to transfer to the desired user account or Root account. For example, to switch to the Opera user account by running the login shell, run the following command:

$ su - Opera

If the su command is not used to switch to another user and you do not specify a specific user account along with these commands, these commands will request superuser privileges in the Linux system, and by default, your user environment will change to the Root user environment.

Therefore, if you want to enter the Root user account, you can run the su command without mentioning the username to be automatically transferred to the Root user account:

$ su

FAQ

sudo su and su are executed to switch to another user account (usually superuser). To use the su command when switching to another user, you must know the target user's password, but the sudo command asks for your password (if you have sudo privileges). Also, by running the sudo su command, in addition to the switch to the desired user, you will get the high privileges granted by sudo. The sudo su command grants you temporary privileges to execute the command, and you will become the root user temporarily if don't mention user name. In contrast using the su command, you will have full access to the desired user account. In general, sudo su is recommended for more control.

By typing exit in the Linux terminal, the user switch and the current root session will be closed and you will return to your main user account.

Conclusion

Understanding the difference between the su and su – has an important impact on changing the environment and environment variables in Linux when switching between user accounts. In short, the su command allows you to keep the current user environment when switching to another user account. In contrast, the su – command executes the login shell to transfer to the specified user account and generally changes the environment variables and the working directory set in the user’s environment.

Leave a Reply

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