📖 Scenario: You are managing a Linux system where you need to switch between users to perform different tasks. Using the su command helps you switch to another user account temporarily.
🎯 Goal: Learn how to use the su command to switch users, set a target user variable, and verify the current user after switching.
📋 What You'll Learn
Create a variable holding the target username
Use the su command with the target username
Check the current user after switching
Print the current user to confirm the switch
💡 Why This Matters
🌍 Real World
System administrators often need to switch users to perform tasks with different permissions. Automating this with scripts saves time and reduces errors.
💼 Career
Knowing how to use the su command and automate user switching is useful for Linux system administrators, DevOps engineers, and anyone managing multi-user systems.
Progress0 / 4 steps
1
Set the target user variable
Create a variable called target_user and set it to the exact string guest.
Linux CLI
Hint
Use target_user = 'guest' to store the username.
2
Switch user using su command
Write a command string called switch_command that uses su with the -c option to run whoami as the user stored in target_user. Use double quotes and f-string style formatting exactly as switch_command = f"su {target_user} -c 'whoami'".
Linux CLI
Hint
Use f"su {target_user} -c 'whoami'" to build the command string.
3
Execute the switch command and capture output
Use the subprocess module to run the command stored in switch_command. Import subprocess and use subprocess.run with shell=True, capture the output, decode it to a string, and store it in a variable called current_user. Use current_user = subprocess.run(switch_command, shell=True, capture_output=True).stdout.decode().strip() exactly.
Linux CLI
Hint
Use import subprocess and subprocess.run(...).stdout.decode().strip() to get the output.
4
Print the current user
Write a print statement to display the value of current_user exactly as print(current_user).
Linux CLI
Hint
Use print(current_user) to show the switched 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
Step 1: Understand the purpose of su
The su command is used to switch from the current user to another user in the terminal.
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.
Final Answer:
Switches the current user to another user -> Option C
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
Step 1: Recall the syntax for full environment loading
Using su - username switches user and loads their full environment.
Step 2: Check options
su alice changes user without full environment, su --user alice uses invalid flag, su /alice is invalid syntax.
Final Answer:
su - alice -> Option A
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
Step 1: Understand what su - bob does
This command switches the current user to 'bob' and loads his environment after password verification.
Step 2: Check the output of whoami
After switching, whoami shows the current active user, which will be 'bob'.
Final Answer:
bob -> Option D
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
Step 1: Understand the error message
Authentication failure means the password entered was incorrect for the target user.
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.
Final Answer:
You entered the wrong password for john -> Option A
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
Step 1: Understand running commands as another user
Using su username -c 'command' runs the command as that user without opening a full shell.
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.
Final Answer:
su deploy -c './script.sh' -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.