Linux Command Reference ยท Permissions and Users
Linux su command
Reference for the Linux su command: start a shell as another user, use the login option -, run one command with -c, and understand why su asks for a password.
su ("substitute user", often read as "switch user") starts a new shell or runs a command as a different account. Without a user name it means root. It asks for the password of the account you are switching to, unless you are already root, in which case it never asks.
The most important detail is the single dash: su - (or su -l) makes a full login shell with the target user's environment, home directory and PATH. Plain su keeps most of your current environment, which often leads to confusing results. Today many administrators prefer sudo because it uses your own password and leaves a better audit trail.
su [-] [USER]
su [-c COMMAND] [USER]
Try it
The sandbox runs as an unprivileged user without passwords, so these examples show the parts that do not need a successful switch: help text, the error when authentication is not possible, and how to read the shell a user would get.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
su [OPTION]... [-] [USER [ARG]...]
| Option | Description |
|---|---|
-, -l, --login |
Start a login shell: clear most of the environment, set HOME, SHELL, USER, LOGNAME and PATH, and change to the target's home directory. |
-c COMMAND, --command=COMMAND |
Run COMMAND with the target user's shell (shell -c COMMAND) instead of an interactive shell. |
-s SHELL, --shell=SHELL |
Use SHELL instead of the one in the account database (needs the shell to be listed in /etc/shells unless you are root). |
-m, -p, --preserve-environment |
Keep the current environment, apart from PATH handling that the system may still override. |
-g GROUP, --group=GROUP |
Set the primary group (root only). |
-G GROUP, --supp-group=GROUP |
Add a supplementary group (root only). |
Returns: the exit status of the shell or command that ran. If authentication fails, su prints Authentication failure and exits with 1; if the command could not be started, 126 or 127 as for any shell.
Usage notes
suversussu -. Use the dash when you want to work as the other user for a while. Without it,PATHcan still be yours, and files like~/.bashrcof the target user may not be read.- Accounts without a login shell fail. A service user whose shell is
/usr/sbin/nologinor/bin/falseprints a polite refusal. Override withsu -s /bin/bash -c 'cmd' service(as root) to run something as that account. - Quote the command given to
-c. The string goes to the target shell, so the current shell must not expand variables inside it:su -c 'echo $HOME' alice. - Passing options through. Arguments after the user name go to the shell:
su alice -c 'ls'andsu alice -- -c 'ls'differ on some systems, so keep-cbefore the user. - Locked root. On Ubuntu and similar distributions the root password is locked, so
sufails for ordinary users. Usesudo -iorsudo su -instead. - Restricting
su. PAM can limit it to members of thewheelgroup (pam_wheel.soin/etc/pam.d/su); see PAM and sudoers. - Security. Typing the root password into a shell session is riskier than
sudo, because the whole shell stays privileged. Exit withexitor Ctrl-D when you are done. - Not logged in
wtmp. A switch withsudoes not show inlast; the attempt is recorded in the authentication log.
Related
sudo,id,whoami,passwd,newgrp- Lessons: Users and permissions, PAM and sudoers