FUNDAMENTALS A Complete Guide for Beginners
When a system has multiple users, it is necessary to maintain proper user environment security. Switching user features in Linux allows for making the multiple-user system interference-free. Moreover, Each user has their own set of permission and access, which protects the system and its resources from unauthorized access and modification. The su command in Linux enables you to switch from a current user to another user.
A. Description
The su is the short form of switch user. It is an excellent tool to secure multiple-user systems, enabling an easier way to switch from one user to another.
B. Syntax
The su command in Linux is a command that takes OPTIONS, and USER, as its argument. The syntax for su commands is given below.
su [options] [-] [user [argument...]]
Note: Here, the options, user, and argument are enclosed by square brackets, meaning that options, user, and argument are not mandatory for the command. Moreover, 3 dots after the argument, mean that the su command can take multiple arguments.
C. Options
Options/arguments can be added to the syntax of the su command to modify the command. I have listed some useful options below. If you do not find your desired option here, you can look for it on the man (manual) page. To go to the man page, type the following command and press ENTER.
man su
Useful Options
- -c/–command=command: It passes the command to the shell with the -c.
- -f/–fast: It passes -f to the shell, which may or may not be helpful, depending on the shell.
- -g/–group=group: It specifies the primary group. This option is available to the root user only.
- -G, –supp-group=group: It specifes a supplementary group. This option is available to the root user only. The first specified supplementary group is also used as a primary group if the –group option is not specified.
- –h/–help: It shows the help file for the su command.
- -/-l/–login: It starts as a login shell with an environment similar to a real login.
- –p/–preserve–environment: It preserves the shell environment.
- –s/–shell: It allows you to specify a different shell environment to run in.
Note: All options in Linux are case-sensitive. Therefore, you must be careful while using these.
Practical Examples of the “su” Command in Linux
The su command in Linux is a command which helps to switch from a current user to another user. The su command in Linux has many practical applications, and I have illustrated a few of them in the following examples.
Example 1: Switch to the Root User Using the “su” Command in Linux
The su command in Linux does the job of switching the user. If no username is mentioned after the su command, it will switch to the root user. Here, I will switch to the root user. To do so, follow the below procedures.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command in the command prompt.
sudo su
➌ Now, press the ENTER button.
Output >
The following image shows that the su command in Linux switches the user to the root user after taking the password.
Similar Readings
- The “usermod” Command in Linux [14+ Practical Examples]
- The “users” Command in Linux [4 Practical Examples]
- The “who” Command in Linux [6 Practical Examples]
Example 2: Switch to a Specific User Using the “su” Command in Linux
The su command in Linux can switch to any user you specify. Here, I will shift from root user to the user named susmit. However, It will not ask for the password, as the current user is the root user. To do so, follow the procedures given below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Copy the following command in the terminal.
su susmit
➌ Now, tap the ENTER button.
Output >
The below image depicts that the su command in Linux has switched the user to susmit without taking the password.
Example 3: Switch to a Specific User’s Home Directory Using the “su” Command in Linux
You can access the target user shell environment using the -l option with the su command in Linux. Here, I will switch from root user to susmit using the -l option of the su command to access susmit’s current shell environment. To do so, follow the procedures given below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Copy the following command in the terminal.
su -l susmit
➌ Now, tap the ENTER button.
Output >
The following image illustrates that the -l option of the su command in Linux switches the user from root user to the user named susmit accessing the susmit’s current shell environment.
Example 4: Executing a Command as a Different User With the “su” Command in Linux
It will not be permitted when you try to view the files and directories of /root if you are not a root user. You have to use the -c option with the su command. Here, I will list the files and directories of the /root as a root user without switching to it. To do so, follow the given steps.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Copy the following command in the terminal.
sudo su -c 'ls /root' root
Note: Here, the su command is doing a job as a root user, which is why the sudo command is needed to give the user for temporary elevated administrative permission.
➌ Now, tap the ENTER button.
Output >
Here, without switching to the root user I have been able to access the /root folder as a root user using the -c option with the su command, as the following image shows.
Example 5: Working in the Current Environment With a Different User
You can work with a different user in the current environment using the -p option with the su command in Linux. Here, I will switch to the user named rick but stay in the current environment. To accomplish so, follow the given steps.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command in the prompt.
su -p rick
➌ Now, press the ENTER button.
Output >
The following image shows the switching to rick user in the current environment using the su command in Linux.
Note: The user will be switched to rick, but the shell environment will be preserved, and this will be open on the /home/susmit directory. Without the -p option, it will be opened on the /home/rick directory. This will keep the environment variable unchanged.
Example 6: Working With a Different Shell Using the “su” Command in Linux
You can switch to a different shell using the -s option with the su command in Linux. Here I will switch to root user from susmit and move to a new shell /bin/sh. To do the task, follow the given steps.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Then copy the following command in the prompt.
sudo su -s /bin/sh
➌ Now, press the ENTER button.
Output >
The following image shows that the su command in Linux switches to the root user and moves to a new shell /bin/sh.
Comparison Between “su” & “sudo” Command
The sudo command in Linux allows you to run commands with elevated power and return the current user’s regular permission. It is a limited-time permission of root functionality for e regular user. To get it, the user must be added to the sudo group.
On the other hand, the su command in Linux allows you to switch from a current user to another user. You can run a command as a different user (non-root user) using the -l options. Here, the su command does an administrative task which can not be possible without administrative permission. In the second case, the sudo command gives it administrative permission to do that job.
Conclusion
In this article, I have demonstrated switching operations from a current user to any other user using the su command in Linux. I hope you’ll be competent enough to explore more things with the help of these practical examples.
Similar Readings