How to Switch Users in Linux Terminal

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.

Prerequisites

  • 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.

Syntax for su Command

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

How to switch Users in Linux?

Follow carefully to use the below setup.

See also  How to Create a New systemd Service on Linux: A Step-by-Step Guide

Switch to a Another User

You can use the following command to change the Linux user in a terminal window:

Method 1

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.

Method 2

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.

See also  10 Best Free Anime Websites to Watch Anime Online

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.

Command Comparison: su vs sudo

Also read: You might also find useful our guide on How to Add Users to Sudoers in Linux

Conclusion

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.