Bird
Raised Fist0
Linux CLIscripting~10 mins

su (switch user) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - su (switch user)
User runs 'su'
↓
Prompt for password
↓
User enters password
↓
Password correct?
No→Access denied, exit
Yes↓
Switch to target user shell
↓
User works as new user
↓
Exit shell to return to original user
The 'su' command asks for the target user's password, then switches the shell to that user if the password is correct.
Execution Sample
Linux CLI
su - alice
# Enter password when prompted
whoami
Switch to user 'alice' and then check which user is active.
Execution Table
StepActionInput/ConditionResultOutput
1Run 'su - alice'Command enteredPrompt for alice's password
2Enter passwordPassword correct?Yes
3Switch userShell changes to aliceUser is now alice
4Run 'whoami'Check current userOutput usernamealice
5Exit shellUser types 'exit'Return to original user shell
💡 User exits alice's shell to return to original user
Variable Tracker
VariableStartAfter su commandAfter passwordAfter switchAfter exit
current_useroriginal_useroriginal_useroriginal_useraliceoriginal_user
Key Moments - 3 Insights
Why does 'su' ask for a password even if I am already logged in?
Because 'su' switches to another user, it requires that user's password for security, as shown in step 2 of the execution_table.
What happens if I enter the wrong password?
The command denies access and does not switch users, stopping at step 2 with 'Password correct?' = No.
How do I return to my original user after switching?
You type 'exit' to leave the switched user's shell, returning to your original user as in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of 'whoami' after switching user?
Aoriginal_user
Balice
Croot
Dsu
💡 Hint
Check step 4 in the execution_table where 'whoami' is run after switching user.
At which step does the shell actually change to the new user?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Result' column in the execution_table for when the user changes.
If the password entered is incorrect, what happens?
AAccess denied and exit su
BSwitch user anyway
CPrompt for password again indefinitely
DSwitch to root user
💡 Hint
Refer to key_moments and the flow where password correctness is checked.
Concept Snapshot
su (switch user) command:
- Syntax: su [options] [username]
- Prompts for target user's password
- On success, opens a shell as that user
- Use 'exit' to return to original user
- Commonly used to switch to root or other users
Full Transcript
The 'su' command lets you switch from your current user to another user on the system. When you run 'su' with a username, it asks for that user's password. If you enter the correct password, your shell changes to that user, letting you work with their permissions. You can check who you are by running 'whoami'. When done, type 'exit' to go back to your original user. If the password is wrong, access is denied and you stay as your original user.

Practice

(1/5)
1. What does the su command do in Linux?
easy
A. Updates the system packages
B. Shows the current user's groups
C. Switches the current user to another user
D. Displays the current directory contents

Solution

  1. Step 1: Understand the purpose of su

    The su command is used to switch from the current user to another user in the terminal.
  2. Step 2: Compare options

    Updating system packages uses apt or yum, showing user's groups uses 'groups', displaying directory contents uses 'ls'. These are unrelated to user switching.
  3. Final Answer:

    Switches the current user to another user -> Option C
  4. Quick Check:

    su = switch user [OK]
Hint: Remember: su means switch user [OK]
Common Mistakes:
  • Confusing su with commands like ls or groups
  • Thinking su updates software
  • Assuming su shows files
2. Which of these is the correct syntax to switch to user alice and load her full environment?
easy
A. su - alice
B. su alice
C. su --user alice
D. su /alice

Solution

  1. Step 1: Recall the syntax for full environment loading

    Using su - username switches user and loads their full environment.
  2. Step 2: Check options

    su alice changes user without full environment, su --user alice uses invalid flag, su /alice is invalid syntax.
  3. Final Answer:

    su - alice -> Option A
  4. Quick Check:

    Use dash (-) to load full environment [OK]
Hint: Use dash (-) after su for full environment [OK]
Common Mistakes:
  • Omitting the dash (-) to load environment
  • Using wrong flags like --user
  • Adding slashes before username
3. What will be the output of the following commands if you run su - bob and enter the correct password?
whoami
medium
A. root
B. Permission denied
C. current user name
D. bob

Solution

  1. Step 1: Understand what su - bob does

    This command switches the current user to 'bob' and loads his environment after password verification.
  2. Step 2: Check the output of whoami

    After switching, whoami shows the current active user, which will be 'bob'.
  3. Final Answer:

    bob -> Option D
  4. Quick Check:

    After su, whoami = new user [OK]
Hint: whoami shows switched user after su [OK]
Common Mistakes:
  • Expecting root instead of target user
  • Thinking it shows original user
  • Assuming permission denied without password
4. You try to switch to user john using su john but get the error: Authentication failure. What is the most likely cause?
medium
A. You entered the wrong password for john
B. User john does not exist
C. You forgot to use sudo before su
D. You need to be root to use su

Solution

  1. Step 1: Understand the error message

    Authentication failure means the password entered was incorrect for the target user.
  2. Step 2: Analyze other options

    User existence error would say 'unknown user'. Using sudo is not required for su. You do not need to be root to switch users if you know the password.
  3. Final Answer:

    You entered the wrong password for john -> Option A
  4. Quick Check:

    Authentication failure = wrong password [OK]
Hint: Authentication failure means wrong password [OK]
Common Mistakes:
  • Assuming user does not exist
  • Thinking sudo is required for su
  • Believing only root can switch users
5. You want to run a script as user deploy without fully switching to their environment. Which command is best to use?
hard
A. su -c deploy ./script.sh
B. su deploy -c './script.sh'
C. su deploy ./script.sh
D. su - deploy ./script.sh

Solution

  1. Step 1: Understand running commands as another user

    Using su username -c 'command' runs the command as that user without opening a full shell.
  2. Step 2: Check options for correct syntax

    su -c deploy ./script.sh wrong flag order, su - deploy ./script.sh starts login shell, su deploy ./script.sh starts non-login shell. Only su deploy -c './script.sh' runs directly.
  3. Final Answer:

    su deploy -c './script.sh' -> Option B
  4. Quick Check:

    Use -c to run command as user [OK]
Hint: Use su -c 'command' to run without full switch [OK]
Common Mistakes:
  • Placing - before username incorrectly
  • Omitting -c flag for command
  • Confusing argument order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard