How to Switch User in Linux [Linux Change User]

How do I switch user in Linux? What is the su command? How does it differ from sudo? If you ever had these questions, you are in the right place.

Updated: 03 Dec, 23 by Antoniy Yushkevych 5 Min

List of content you will read in this article:

Linux has a unique and different way of switching users than other operating systems (OS) have. To switch or change users in Linux, simply input the command "su newuser", ensuring to replace "newuser" with the username of the account you wish to switch to. If you're new to Linux, you may wonder how to switch users in Linux from the command line. The "su" command is a simple way to change which user you are logged in as. In this article, we'll show you how to use the su command for Linux change user.

To Linux switch user, simply type "su" followed by the username of the user you want to switch to. For example, if you wanted to switch to the user "mvm", you would type:

su mvm

You will then be prompted for that user's password. Once entered, you will be logged in as that user.

If you want to switch users temporarily, you can use the "-l" option. This stands for "login shell." It will give you a full login shell for the specified user. For example:

su -l mvm

This is useful if you need to run a command as another user and don't want to leave your current session. Remember that when using the "-l" option, you will need to enter the password for the other user twice (once for su and once for login).

Su allows you to change the existing user to some other user. Use the –l [username] method to define a user account if you need to execute a command as someone other than root. Switching between shell interpreters on one go is also possible using the su command. Su is a more powerful but older command. It can mimic sudo's features by passing a single command to the shell with the –c option.

The su command in Linux is short for "substitute user or switch user" and allows a user to gain temporary access to another user's account. This can be useful if you need to run a command as another user or troubleshoot an issue and need to access another user's account.

To use the su command, type the following into the command prompt:

su [options] [username [arguments]]

su defaults to the superuser if no username is specified (root). Locate the required user and include it in the su command syntax.

Use -h to see a list of command options in su:

su –h

You can use the su command with the following options:

  • Username – Change your username with the username you'd like to use to log in. Any consumer, not just root, can do this.
  • –command or –c [cmd] – Executes a command as the user-defined.
  • –login or –l [username] – Changes the username by running a login script. For that person, you are required to enter a password.
  • –s or –shell [shell] – Allows you to execute the command in a separate shell context.
  • –h or –help – Displays the su command's help file.
  • –p or ––preserve-environment – Protect the shell environment (HOME, SHELL, USER, LOGNAME).

Linux  Virtual Server
Linux Virtual Server

Starting From $5.99/Monthly

Change Linux user to Another User

Enter the following command to change user Linux in this terminal window:

su –l [other_user_name]

A password would be needed. When you type it in, the username will be changed to that account.

As a Different User, Execute a Specific Command

Using the –c alternative to execute a particular command as a separate user:

su –c [command] [other_user_name]

The system would then prompt you for your username. When you execute this command, the system will run the ls (list the directory contents) command for the defined account.

Switching to a Different Shell Environment

Enter the following to use a new shell or operating environment:

su –s /usr/bin/zsh

In Z shell, this command creates a root user account.

In the Same Environment, Use a Different User

With the –p alternative, you can hold the current user account's environment:

su –p [other_user_name]

[another user] can be replaced with the real username you choose to use. The user account will change, but your home directory will remain the same. This is helpful if you execute a command as a new user but need access to the current user's files.

Use the echo $HOME command to show the directory you're operating in to ensure you're still in the same home environment.

In conclusion, the SU command is a powerful tool that can be used to switch users in Linux. This article has provided a comprehensive guide on using Linux change user command and some useful tips and tricks. We hope that you have found this information helpful and that you can now use the SU command confidently.

People are also reading:

Antoniy Yushkevych

Antoniy Yushkevych

Master of word when it comes to technology, internet and privacy. I'm also your usual guy that always aims for the best result and takes a skateboard to work. If you need me, you will find me at the office's Counter-Strike championships on Fridays or at a.yushkevych@monovm.com