الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

su

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

المستوى
امتحان RHCSA
المجموعة
المستخدمون والمجموعات
يحتاج root
للتغييرات
الحزمة
util-linux

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

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.

طريقة الكتابة

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

الخيارات 12

الأكثر استخدامًا

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

أمثلة 11

$ مستخدم عادي# بصلاحية root (أو مع 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).

انتبه

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

الملفات

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

نصائح للحفظ

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

رموز الخروج

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.