Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

su

Start a shell (or one command) as another user. You type the password of that user.

Level
RHCSA exam
Group
Users and groups
Needs root
For changes
Package
util-linux

What it does

su means "substitute user". Without a name it switches to root. You must know the password of the target user — only root can switch without a password.

Always use su - (or su -l): it starts a full login shell with the environment and home folder of the new user. Without the dash you keep your own PATH and variables, which can give strange results.

On RHEL, sudo is the better tool for admin work: it uses your own password and logs every command. su is still useful for root itself, to test what another user sees.

How to write it

su [OPTION]... [-] [USER [ARGUMENT]...]

Options 12

Most used

-, -l, --login
Start a login shell: clean environment, home folder and profile of the new user.
-c, --command=COMMAND
Run only this command as the user, then return.
-s, --shell=SHELL
Use this shell instead of the user's login shell (works only if the shell is in /etc/shells or you are root).
-m, -p, --preserve-environment
Keep your current environment (do not set HOME, SHELL, USER, LOGNAME).
-g, --group=GROUP
Set the primary group (root only).
-G, --supp-group=GROUP
Add a supplementary group (root only).
-P, --pty
Create a new pseudo-terminal for the session (better isolation from the calling terminal).
-w, --whitelist-environment=LIST
With --login, keep these variables (comma list).
-f, --fast
Pass -f to the shell (only csh/tcsh: do not read the start files).
--session-command=COMMAND
Like -c but without a new session (not recommended).
-h, --help
Show help.
-V, --version
Show the version.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Become root with a full login shell

    su -
    Password:
    pwd
    /root

    Type the root password. exit brings you back.

  2. 2Switch to another user

    su - lisa

    You need lisa's password (root does not).

  3. 3Run one command as root

    su -c 'dnf -y update' -

    Runs the command and returns to your normal shell.

  4. 4Test what a user can see

    su - lisa -c 'ls -l /srv/share'

    root can check permissions as the user without knowing the password.

  5. 5Use a shell for an account with nologin

    su -s /bin/bash -c 'whoami; id' apache
    apache

    Service accounts have /sbin/nologin; -s gives a shell for a test.

  6. 6See the difference with and without the dash

    su
    echo $HOME; pwd
    /root
    /home/student

    Without - you stay in your folder with your environment.

  7. 7Check which accounts may not log in

    su - carol
    This account is currently not available.

    The shell /sbin/nologin refuses the login.

  8. 8Keep one variable with a login shell

    su - -w HTTPS_PROXY lisa

    Useful when a proxy setting must be kept.

  9. 9Start the session in its own terminal (more secure)

    su -P - lisa

    Protects against tricks where a program writes into your terminal (TIOCSTI).

  10. 10Find failed su attempts (security)

    grep 'su:' /var/log/secure | grep -i fail

    Many failures for root can mean somebody guesses passwords.

  11. 11Allow su to root only for wheel members

    grep pam_wheel /etc/pam.d/su
    #auth required pam_wheel.so use_uid

    Remove the # to allow su only for members of wheel (hardening).

Watch out

  • Without - the environment is not changed. Use su -.
  • On many cloud images root has no password, so su - fails. Use sudo -i.

Files

/etc/pam.d/su
PAM rules for su (like pam_wheel).
/var/log/secure
Log of su and sudo use.

Tips to remember

  • su = password of the target user; sudo = your own password.
  • sudo -i gives the same root login shell as su -.

Exit codes

0The command ran (or the exit code of the command run with -c).
1Error (for example a wrong password).
126The command could not be run.
127The command was not found.