0
0
Linux CLIscripting~10 mins

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

Choose your learning style9 modes available
Concept Flow - su (switch user)
User runs 'su'
Prompt for password
User enters password
Password correct?
NoAccess 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.