Loading…
Warp is now open-source Learn more
Loading…
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.
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>Where:
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
aliceTo get the list of available users on the system you can read our other article on how to list users and groups in Linux.
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>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
aliceNote 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.
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>Once executed, it will:
Alternatively, you can also achieve the same result using the sudo command with the -i and -u flags as follows:
$ sudo -i -u <user>To switch to the root user account, you can use the su command without arguments as follows:
$ suWhich 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
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>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" aliceNote that the specified command will be executed using the shell of the user who initiated the su command and not the target user's.
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

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.
Comments will help make your scripts more readable
Via command line arguments and prompting users for input
Use cURL to send data to a server
Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.
Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.
Learn how to manually and automatically create and list groups 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.
Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.
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.
Learn how to filter and format the content of files and the output of commands in Linux using the awk command.
Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.
Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.
$ su <user>$ whoami
john
$ su alice
Password:
$ whoami
alice$ sudo su <user>$ whoami
john
$ sudo su alice
[sudo] password for john:
$ whoami
aliceuser is not in the sudoers file.$ su --login <user>$ sudo -i -u <user>$ su -c "<command>" <user>$ su --login -c "ls" alice