Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

su

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

Niveau
RHCSA-examen
Groep
Gebruikers en groepen
Root nodig
Voor wijzigingen
Pakket
util-linux

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

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.

Zo schrijf je het

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

Opties 12

Meest gebruikt

-, -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.

Voorbeelden 11

$ als gewone gebruiker# als root (of met 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).

Let op

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

Bestanden

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

Tips om te onthouden

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

Exitcodes

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