• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Switch Users In Linux

Sarah Majeed

Published: 5/10/2024

About Terminus

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 su (short for “substitute user”) as follows:

$ su <user>

Run in Warp

Where:

  • <user>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 john to the user account named alice:

$ whoami
john
$ su alice
Password:
$ whoami
alice

Run in Warp

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.

Switching to a user without using their password

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

$ sudo su <user>

Run in Warp

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 john account to the alice account without using the target user's password:

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

Run in Warp

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.

Run in Warp

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.

Executing a login shell as another user

By default, when logging in as another user, the su command will not change the current working directory and only set the environment variables HOME and SHELL (plus USER and LOGNAME 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 su command with the --login flag as follows:

$ su --login <user>

Run in Warp

Once executed, it will:

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

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

$ sudo -i -u <user>

Run in Warp

Switching to the root user account

To switch to the root user account, you can use the su command without arguments as follows:

$ su

Run in Warp

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 su command alone will certainly result in an authentication error with a message like su: Authentication failure

Running a command as a different user

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

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

Run in Warp

For example, this command will execute the ls command using the login shell environment and permissions of the user account named alice:

$ su --login -c "ls" alice

Run in Warp

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

Easily retrieve this command using Warp’s AI Command Suggestions

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

Thumbnail for

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

Written by

Sarah Majeed

Filed Under

Related Articles

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

UnixLinux
Thumbnail for Razvan LudosanuRazvan Ludosanu

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

LinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

LinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

Linux Chmod Command

Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.

Linux
Thumbnail for Razvan LudosanuRazvan Ludosanu

POST JSON Data With Curl

How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.

BashUnixLinux
Thumbnail for Neeran GulNeeran Gul

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Linux

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

Linux

Remover Users in Linux

Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.

Linux

Delete Files In Linux

Learn how to selectively delete files in Linux based on patterns and properties using the rm command.

Linux

Find Files In Linux

Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.

Linux

Copy Files In Linux

Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.

Linux

Create Files In Linux

Learn how to create regular files in Linux using command line interface commands like touch, echo, cat, printf, and in-terminal text editors like Vim.

Linux

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Request demo
Thumbnail for null