Skip to main content

su Cheatsheet

By Dejan Panovski Updated on Download PDF

Quick reference for switching users and running commands with su in Linux

The `su` command lets you switch to another user account or run commands with another user's privileges. This cheatsheet covers login shells, one-off commands, environment handling, and the most common `su` patterns you will use on Linux systems.

Basic Syntax

Core su command forms.

CommandDescription
suSwitch to root using the current shell environment
su -Switch to root with a full login shell
su usernameSwitch to another user
su - usernameSwitch to another user with a login shell
su --helpShow available options

Login Shell vs Current Shell

Choose whether to keep the current environment or start a clean login session.

CommandDescription
suKeep most of the current environment
su -Load the target user’s login environment
su - usernameChange to the target user’s home directory and shell
whoamiConfirm the current effective user
pwdCheck whether the working directory changed

Run a Command as Another User

Use -c to run a single command without starting a full interactive shell.

CommandDescription
su -c 'whoami'Run a single command as root
su -c 'id' usernameRun a command as another user
su -c 'ps aux'Run a quoted command with spaces
su -c 'cd /tmp && pwd' usernameRun a compound shell command
su -c 'echo $HOME' usernameCheck the target user’s home in a non-login shell

Shell and Environment

Adjust the shell or preserve the caller’s environment when needed.

CommandDescription
su -s /bin/bash usernameUse Bash as the target shell
su -s /usr/bin/zsh usernameUse Zsh as the target shell
su -pPreserve the current environment
echo $HOMECheck the current home directory
echo $SHELLCheck the current shell

Root Access Patterns

Compare common root-shell workflows.

CommandDescription
suPrompt for the root password
su -Prompt for the root password and start a login shell
sudo su -Use sudo to start a root login shell
sudo -iStart a root login shell without calling su directly
exitLeave the current su shell

Troubleshooting

Quick checks for common su issues.

IssueCheck
Authentication failureVerify the target user’s password, not your own
su: user username does not existConfirm the account exists with id username
This account is currently not availableCheck the target shell in /etc/passwd; service accounts often use /usr/sbin/nologin
su keeps the old PATHUse su - to start a clean login shell
Root access fails on UbuntuThe root account may be locked; use sudo -i instead

Use these guides when working with users, passwords, and privilege escalation.

GuideDescription
su Command in LinuxFull su guide with examples and sudo comparison
sudo Command in LinuxRun commands with elevated privileges
How to Change User Password in LinuxSet or reset passwords
useradd cheatsheetCreate and manage user accounts
How to Add User to Group in LinuxManage group membership