| title | Linux Command Tutorial: su | ||||
|---|---|---|---|---|---|
| date | 2026-09-14 00:00:00 +0000 | ||||
| categories |
|
||||
| tags |
|
||||
| draft | false | ||||
| slug | linux-su-tutorial | ||||
| description | Authoritative reference tutorial for su (Substitute User), detailing identity switching, login shell simulation, PAM integration, and security boundaries. | ||||
| upstream_suite | util-linux | ||||
| upstream_version | util-linux 2.40 | ||||
| posix_standard | None | ||||
| research_date | 2026-09-14 |
The Linux Command Tutorial series provides rigorous, upstream-verified references for essential system commands across Linux distributions and UNIX-like environments. Each article focuses on a single executable, combining exhaustive option documentation, verified real-world examples, security boundaries, and best practices directly derived from official source documentation and POSIX standards.
Upstream: util-linux | POSIX: None | Safety Tier: privileged-system-destructive | Scope: user-management
su allows you to run commands with a substitute user and group ID. When called without arguments, su defaults to running an interactive shell as the root user.
- Upstream Project & Provenance: Originally part of AT&T UNIX. On modern Linux,
suis typically provided by the util-linux suite (formerly part of GNU Coreutils or shadow-utils). - Portability & Standards Baseline:
suis a classic UNIX command but is strictly not defined in POSIX.1-2024, as user identity switching mechanisms are considered implementation-defined. - Target Research Implementation: Audited against util-linux 2.40.
- Applicability & Lifecycle: A fundamental tool for identity switching. While often superseded by
sudofor administrative workflows,suremains essential for directly assuming service accounts and full root login environments.
su [options] [-] [user [argument...]]surequests the password of the target user, unlikesudowhich requests the password of the invoking user.- By default,
suspawns a new shell but retains the environment variables of the invoking user. - To simulate a full login (reading
.bash_profile, resetting$HOME,$PATH, etc.), the-(or--login) flag must be used. - Authentication and session management are completely delegated to PAM (Pluggable Authentication Modules).
| Short Flag | Long Flag | Description | POSIX Defined |
|---|---|---|---|
- / -l |
--login |
Start the shell as a login shell with an environment similar to a real login. | No |
-c |
--command=COMMAND |
Pass a single command to the invoked shell using -c. |
No |
-s |
--shell=SHELL |
Specify the shell to run instead of the user's default shell in /etc/passwd. |
No |
-m / -p |
--preserve-environment |
Do not reset environment variables (e.g., $HOME, $USER). |
No |
-g |
--group=GROUP |
Specify the primary group. Available only if invoked by root. | No |
-G |
--supp-group=GROUP |
Specify a supplementary group. Available only if invoked by root. | No |
| Operation | Command | Notes |
|---|---|---|
| Switch to root (keep env) | su |
Prompts for root password. Retains current directory and variables. |
| Switch to root (full login) | su - |
Simulates a fresh root login, changing to /root and resetting $PATH. |
| Switch to a specific user | su - alice |
Prompts for Alice's password and simulates her login. |
| Run a command as user | su -c 'ls -l' alice |
Executes one command as Alice and returns. |
| Override default shell | su -s /bin/bash postgres |
Useful when target user has /sbin/nologin. |
$ su
Password:
# pwd
/home/alice
# echo $HOME
/home/alice(Notice that without -, the $HOME and working directory remain the invoking user's.)
$ su -
Password:
# pwd
/root
# echo $HOME
/rootSystem administrators frequently need to run commands as unprivileged service accounts (like postgres or nginx). Since these accounts often have /sbin/nologin or /bin/false as their shell, standard su fails. Use -s to override the shell (must be run as root):
# su -s /bin/bash postgres
bash-5.1$ psqlTo execute a command pipeline as another user without opening an interactive session:
# su -c "pg_dump database > backup.sql" postgresIf you need to switch to root but require an environment variable exported in your normal user session (e.g., DISPLAY for X11 applications, though not recommended for security):
$ su -pUsing su within a script can be problematic because it inherently expects a TTY for password input. However, if the script is running as root, su does not prompt for a password.
#!/bin/bash
# Backup script running as root
echo "Starting backup as postgres user..."
su -c "/usr/bin/pg_dumpall > /backups/all.sql" postgres
echo "Backup complete."In highly restricted environments, administrators can use su to bypass certain login restrictions. For instance, if a user is restricted from SSH access via PAM, an administrator can SSH as root and su into the user.
| Exit Code | Meaning |
|---|---|
0 |
Success (if invoked with -c, returns the exit code of the executed command). |
1 |
General failure (e.g., incorrect password, PAM failure). |
126 |
Command found but not executable. |
127 |
Command not found. |
| Path | Purpose |
|---|---|
/etc/pam.d/su |
PAM configuration file governing authentication rules for su. |
/etc/pam.d/su-l |
PAM configuration specifically for su --login. |
/etc/login.defs |
Contains global parameters (like SU_WHEEL_ONLY) that influence su behavior. |
/etc/passwd |
Read to determine the target user's home directory and default shell. |
Warning
Running su without - (or --login) is dangerous. It preserves the invoking user's environment, including $PATH and $LD_LIBRARY_PATH. A malicious user could set $PATH to point to a compromised binary, tricking the root user into executing it. Always use su - for administrative tasks.
On many enterprise systems (e.g., RHEL), su to root is restricted via PAM to users who belong to the wheel group. If a user is not in the wheel group, su will fail immediately, even if the user knows the root password.
su requires sharing the root password, which violates the principle of individual accountability. sudo is widely preferred because it authenticates the invoking user, allows granular command restrictions, and logs execution to /var/log/auth.log.
- Always Use Login Shells for Root:
- Guidance: Always invoke as
su -rather thansu. - Authoritative Justification: The util-linux manual explicitly notes that avoiding
--logincan cause unpredictable behavior due to environment pollution.
- Guidance: Always invoke as
- Prefer
sudoOversufor Routine Admin Tasks:- Guidance: Use
sudo -iorsudo commandinstead of sharing the root password viasu. - Authoritative Justification: Centralizes audit logging and adheres to least-privilege principles.
- Guidance: Use
- Use
-sand-cfor Service Contexts:- Guidance: Use
su -s /bin/bash -c "cmd" userto invoke actions as service accounts without changing their secure/sbin/nologindefault.
- Guidance: Use
- util-linux su Manual Page:
man 1 su - Debian Wiki - su: https://wiki.debian.org/su
- Linux PAM System Administrator's Guide: https://www.linux-pam.org/Linux-PAM-html/Linux-PAM_SAG.html