su (switch user) in Linux CLI - Time & Space Complexity
We want to understand how the time it takes to switch users with su changes as we use it more or with different inputs.
Specifically, how does the command's execution time grow when switching users repeatedly or with different user data?
Analyze the time complexity of the following command usage.
for user in user1 user2 user3 user4 user5; do
su - $user -c 'echo Hello from $(whoami)'
done
This script switches to each user in a list and runs a simple command to print a greeting.
Look at what repeats in the script.
- Primary operation: The
sucommand runs once per user in the list. - How many times: It runs exactly as many times as there are users (5 times here).
Each time we add one more user to the list, the script runs su one more time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times running su |
| 100 | 100 times running su |
| 1000 | 1000 times running su |
Pattern observation: The total work grows directly with the number of users; doubling users doubles the work.
Time Complexity: O(n)
This means the time to run the script grows in a straight line with the number of users you switch to.
[X] Wrong: "Switching users with su takes the same time no matter how many users I switch to."
[OK] Correct: Each su command runs separately, so more users mean more commands and more total time.
Understanding how repeated commands scale helps you write scripts that run efficiently and predict how long tasks will take.
What if we replaced the simple echo command with a longer script inside su -c? How would that affect the time complexity?
Practice
su command do in Linux?Solution
Step 1: Understand the purpose of
Thesusucommand 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 CQuick Check:
su= switch user [OK]
- Confusing su with commands like ls or groups
- Thinking su updates software
- Assuming su shows files
alice and load her full environment?Solution
Step 1: Recall the syntax for full environment loading
Usingsu - usernameswitches user and loads their full environment.Step 2: Check options
su alicechanges user without full environment,su --user aliceuses invalid flag,su /aliceis invalid syntax.Final Answer:
su - alice -> Option AQuick Check:
Use dash (-) to load full environment [OK]
- Omitting the dash (-) to load environment
- Using wrong flags like --user
- Adding slashes before username
su - bob and enter the correct password?whoami
Solution
Step 1: Understand what
This command switches the current user to 'bob' and loads his environment after password verification.su - bobdoesStep 2: Check the output of
After switching,whoamiwhoamishows the current active user, which will be 'bob'.Final Answer:
bob -> Option DQuick Check:
After su, whoami = new user [OK]
- Expecting root instead of target user
- Thinking it shows original user
- Assuming permission denied without password
john using su john but get the error: Authentication failure. What is the most likely cause?Solution
Step 1: Understand the error message
Authentication failuremeans 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 AQuick Check:
Authentication failure = wrong password [OK]
- Assuming user does not exist
- Thinking sudo is required for su
- Believing only root can switch users
deploy without fully switching to their environment. Which command is best to use?Solution
Step 1: Understand running commands as another user
Usingsu 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.shwrong flag order,su - deploy ./script.shstarts login shell,su deploy ./script.shstarts non-login shell. Onlysu deploy -c './script.sh'runs directly.Final Answer:
su deploy -c './script.sh' -> Option BQuick Check:
Use -c to run command as user [OK]
- Placing - before username incorrectly
- Omitting -c flag for command
- Confusing argument order
Ready to prove it?
Pass the test and this pattern moves unknown
