1. Introduction
The su command stands for substitute user or switch user and allows switching from the current user to another. By default, su switches to the root user if no username is specified. Additionally, it’s essential for tasks that require different user permissions or environments.
System administrators primarily use this tool to gain root privileges or switch to other user accounts to perform administrative tasks.
In this tutorial, we’ll cover common options and examples of the su command to understand its functionality and use cases. Notably, we’re going to run all the commands in this tutorial on Kali Linux. However, the same process applies to other Linux distributions as well.
2. Common su Command Options
The basic syntax and structure of the su command are straightforward:
$ su [options] [username]
Let’s now understand what each of the components of the su command means:
- [options]: these are the various flags or switches that affect the behavior of the command
- [username]: the user account we want to switch to or execute the command as
Now, let’s explore common options that are available with the su command:
Options
Description
-m, -p
Prevents the environment variables from resetting when switching users
*-P
*
Creates a new pseudo-terminal for the user session
-s
Runs the specified shell after switching users
-f
Starts a shell quickly by skipping reading the initialization files (~/.bashrc, ~/.bash_profile)
-c
Passes a command to the shell after switching users
–session-command
Passes a command to the shell after switching users but doesn’t create a new session
-, -l
Makes the shell a login shell, which means it reads the user’s login profile and initializes the environment as if the user had logged in directly
-g
Sets the primary group for the new user session
-G
Determines a supplemental group for the new user session
-w
Specifies a list of environment variables that shouldn’t be reset when switching users
-h
Prints the help information for the su command
-V
Displays the version information for the su command
3. Common su Command Examples
Let’s dive into some practical examples of using the su command.
3.1. Switching to a Different User
To switch to a different user, we use the su command followed by the username. For example, let’s switch to the user named test:
$ su test
Password:
As is apparent from the output, we’re prompted to enter the password for the user we’re switching to.
3.2. Preserving the Environment While Using the su Command
To preserve the current user’s environment variables while switching users, we use the -m or -p option:
$ su -m test
Password:
$ echo $USER
kali
By running this command, the current user switches to the test user. However, environment variables such as USER remain unchanged, indicating the original user’s environment.
3.3. Using su With the sudo Command
We can use the su command in combination with sudo to switch to another user without needing to know their password. Instead, we enter the current user’s password.
For example, let’s switch to the root user:
$ sudo su -
[sudo] password for kali:
This command elevates the current user’s privileges to root, providing full administrative access.
3.4. Using a Different Shell With the su Command
To use a different shell when switching users, we use the -s option followed by the path to the shell:
$ su -s /bin/bash test
Password:
This command switches to the test user and starts a new session with the specified shell, which is /bin/bash in this case.
3.5. Executing a Command as a Different User
We can execute a specific command as a different user using the -c option. For example, let’s list the contents of test‘s home directory:
$ su -c "ls /home/test" test
Password:
This command switches to the test user and runs the ls /home/test command to display the contents of test‘s home directory.
3.6. Using the su Command Non-Interactively in a Shell
We can use the su command in a shell script to switch users. However, su will typically prompt for the user’s password, making it interactive.
To run a command directly as another user without entering an interactive shell, we can use the su -c option. However, we still need to provide the user’s password in this case.
If there’s a strict requirement to use su non-interactively, we can use the expect utility to pass the password to the su command.
To achieve this, first, let’s create a shell script file named my_script that contains the following su command used with expect:
$ cat my_script
#!/usr/bin/expect -f
set user [lindex $argv 0]
set password [lindex $argv 1]
spawn /bin/su $user
expect "Password:"
send "$password\r"
interact
This script automates the process of passing the password to the su command.
For example, let’s switch to the root user, non-interactively:
$ chmod +x my_script
$ ./my_script root kali
spawn /bin/su root
Password:
In this example, the script switches to the root user non-interactively by passing the password kali to the su command.
4. Conclusion
In this article, we’ve explored the su command, which is a powerful tool for switching users and executing commands as different users in Linux. Additionally, we covered various options and practical examples, demonstrating how to use su effectively for different scenarios.
By understanding and utilizing these examples, we can enhance our system administration skills and streamline user management tasks in Linux.