Switch Users In Linux

Switch Users In Linux

In Unix-like operating systems such as Linux and macOS, as the system is shared among multiple concurrent users, you’ll often have to switch between users to perform actions with a different set of privileges.

The short answer

To temporarily switch to a different user account on the system and execute commands as this user, you can use [.inline-code]su[.inline-code] (short for “substitute user”) as follows:

$ su <user>


  • [.inline-code]<user>[.inline-code]is the name of the user you want to switch to.

Note that when switching from a regular user account to another regular user account, you will be prompted to enter the user's password, whereas when switching from the root account to any other account, the user's password is not required.

For example, these commands illustrate the switch from the user account named [.inline-code]john[.inline-code] to the user account named [.inline-code]alice[.inline-code]:

$ whoami
$ su alice
$ whoami

To get the list of available users on the system you can read our other article on how to list users and groups in Linux.

[#switch-without-using-a-password] Switching to a user without using their password [#switch-without-using-a-password]

To switch to a regular user account without having to enter their password, you can use the [.inline-code]su[.inline-code] command with the [.inline-code]sudo[.inline-code] command as follows:

$ sudo su <user>

Once executed, you will be prompted to enter your own password instead of the user's password.

For example, these commands illustrate how to switch from the [.inline-code]john[.inline-code] account to the [.inline-code]alice[.inline-code] account without using the target user's password:

$ whoami
$ sudo su alice
[sudo] password for john:
$ whoami

Note that your user account must be listed as a sudoer, as this command will otherwise fail with the following error message:

user is not in the sudoers file.

You can learn more about sudoers in Linux by reading our other articles on how to add a user to sudoers and how to spawn a root shell using sudo su.

[#execute-a-login-shell] Executing a login shell as another user [#execute-a-login-shell]

By default, when logging in as another user, the [.inline-code]su[.inline-code] command will not change the current working directory and only set the environment variables [.inline-code]HOME[.inline-code] and [.inline-code]SHELL[.inline-code] (plus [.inline-code]USER[.inline-code] and [.inline-code]LOGNAME[.inline-code] if the target user is not root).

To log in as another user and load their profile and environment similar to a real login, you can use the [.inline-code]su[.inline-code] command with the [.inline-code]--login[.inline-code] flag as follows:

$ su --login <user>

Once executed, it will:

  • Clear all the environment variables except for [.inline-code]TERM[.inline-code].
  • Initialize the environment variables [.inline-code]HOME[.inline-code], [.inline-code]SHELL[.inline-code], [.inline-code]USER[.inline-code], [.inline-code]LOGNAME[.inline-code], and [.inline-code]PATH[.inline-code].
  • Set the current working directory to the user's home directory.

Alternatively, you can also achieve the same result using the [.inline-code]sudo[.inline-code] command with the [.inline-code]-i[.inline-code] and [.inline-code]-u[.inline-code] flags as follows:

$ sudo -i -u <user>

[#switch-to-the-root-account] Switching to the root user account [#switch-to-the-root-account]

To switch to the root user account, you can use the [.inline-code]su[.inline-code] command without arguments as follows:

$ su

Which will prompt you to enter the root password.

However, since the root account is disabled by default on most Linux distributions—which means that the root password is not set, in order to prevent anyone from directly logging into it—using the [.inline-code]su[.inline-code] command alone will certainly result in an authentication error with a message like [.inline-code]su: Authentication failure[.inline-code]

[#run-commands-as-another-user] Running a command as a different user [#run-commands-as-another-user]

To execute a single command as another user without physically switching accounts, you can use the [.inline-code]su[.inline-code] command with the [.inline-code]-c[.inline-code] flag (short for [.inline-code]--command[.inline-code]) as follows:'

$ su -c "<command>" <user>

For example, this command will execute the [.inline-code]ls[.inline-code] command using the login shell environment and permissions of the user account named [.inline-code]alice[.inline-code]:

$ su --login -c "ls" alice

Note that the specified command will be executed using the shell of the user who initiated the [.inline-code]su[.inline-code] command and not the target user's.

[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering [.inline-code]run command as another user[.inline-code] in the AI command suggestions will prompt a [.inline-code]su[.inline-code] command that can then be quickly inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].