Readers like you help support Cloudbooklet. When you make a purchase using links on our site, we may earn an affiliate commission.
Learn how to switch users in the Linux terminal using the "su" command, allowing you to execute commands and access different user accounts for various tasks and troubleshooting.
Switching users in Linux is a unique process that is not found in other operating systems. The su command, which stands for “Switch users,” is a Linux utility that allows you to execute commands with the privileges of another user.
In this article will show you how to utilize the su command and its practical applications, particularly when the root user is unable to connect in via SSH or the GUI display manager.
Table of Contents
A Linux-based computer system
Terminal window/command-line access (Ctrl-Alt-T).
What is SU command?
The su command is a strong tool for switching to another user’s account and gaining their rights. While the “su” command has a lot of power, you should use it with caution and make sure you have the permissions to switch to another user’s account.
In addition, the “sudo” command is often preferable for running commands as another user because it allows for greater level of monitoring and auditing.
In Linux, you can use the “su” command as follows:
su [options] [username [arguments]
If no username is specified, the “su” command assumes the superuser or root account. To change to a specific user, add the username in the command.
Command Options for su
Specifies the user account to switch to.
Runs a single command as the specified user.
Specifies the shell interpreter to use.
Preserves the environment of the current user.
How to switch Users in Linux?
Follow carefully to use the below setup.
Switch to a Another User
You can use the following command to change the Linux user in a terminal window:
su -l [other_user_name]
Replace [other_user_name] with the username of the user account you want to switch to. For example, to switch to the user “cloudbooklet,” enter the command:
su -l cloudbooklet
After running the program, you will be requested to input the target user’s password. Enter the password and hit “Enter.” The terminal session will switch to the selected user account if the password is correct.
It’s important to note that the -l option launches a new login shell, which loads the target user’s login scripts and environment variables.
You can also use the below command to switch to a different user.
sudo su username
This command instantly switches to the other user in the same terminal and doesn’t require a password.
Run Command as Different User
In Linux, you may use the following command to run a command as a different user:
su –c [command] [other_user_name]
Replace [command] with the command to be executed and [other_user_name] with the username of the user account under which the command should be run. For instance, if you want to run the “ls” command as “cloudbooklet,” the command would be:
su -c "ls" cloudbooklet
Using the -c option, you can run a single command as another user without switching to their account.
You can also use the below command with sudo to skip the password prompt.
sudo -u username ls /var
This command outputs the contents of the var directory using the specified user.
Same Environment for Different Users
You can use the following command to switch to a different user while retaining the current user’s environment:
su -p [other_user_name]
[other_user_name] should be replaced with the username of the user account you want to switch to.
For example, to switch users “cloudbooklet,” enter the command:
su -p cloudbooklet
The user account will be changed to the provided user when you run this command, but your current home directory will stay unchanged.
The echo $HOME command can be used to confirm that you are still in the same home environment. It will show the home directory of the current user. If the output matches your expected home directory, you are still working in the same environment.
Finally, the “su” command in the Linux terminal allows you to switch between users, run commands as multiple users, and maintain a secure and controlled environment for diverse tasks and troubleshooting needs.
Please feel free to share your thoughts and feedback in the comment section below.
Greetings, I am a technical writer who specializes in conveying complex topics in simple and engaging ways. I have a degree in computer science and journalism, and I have experience writing about software, data, and design. My content includes blog posts, tutorials, and documentation pages, which I always strive to make clear, concise, and useful for the reader. I am constantly learning new things and sharing my insights with others.
Welcome to our technology blog, where we explore the latest advancements in the field of artificial intelligence (AI) and how they are revolutionizing cloud computing. In this blog, we dive into the powerful capabilities of cloud platforms like Google Cloud Platform (GCP), Amazon Web Services (AWS), and Microsoft Azure, and how they are accelerating the adoption and deployment of AI solutions across various industries. Join us on this exciting journey as we explore the endless possibilities of AI and cloud computing.