passwd (change password) in Linux CLI - Time & Space Complexity
When using the passwd command to change a password, it's helpful to understand how the time it takes grows with input size.
We want to know how the command's work changes as the password length or system size changes.
Analyze the time complexity of the passwd command process.
# User runs passwd to change password
passwd
# System prompts for current password
# User enters current password
# System prompts for new password
# User enters new password
# System verifies and updates password
This snippet shows the typical steps when changing a password using passwd.
Look for any repeated actions or checks during the password change.
- Primary operation: Password verification and update steps.
- How many times: Each step happens once per password change attempt.
The time to change a password grows mostly with the length of the password entered.
| Input Size (password length) | Approx. Operations |
|---|---|
| 10 characters | Small number of checks and updates |
| 100 characters | More checks proportional to length |
| 1000 characters | Much longer verification and update time |
Pattern observation: The work grows roughly in proportion to the password length.
Time Complexity: O(n)
This means the time to change a password grows linearly with the length of the password.
[X] Wrong: "Changing a password always takes the same time no matter how long it is."
[OK] Correct: The system must check every character, so longer passwords take more time to process.
Understanding how commands like passwd scale helps you think about efficiency in real systems.
This skill shows you can reason about how user input size affects system work.
"What if the system added extra password strength checks that scan the password multiple times? How would the time complexity change?"
Practice
passwd command do in Linux?Solution
Step 1: Understand the purpose of
Thepasswdpasswdcommand is designed to change user passwords securely on Linux systems.Step 2: Compare options with command function
Options B, C, and D relate to user management but not password changes. Only It changes the password of a user securely. correctly describespasswd.Final Answer:
It changes the password of a user securely. -> Option AQuick Check:
passwdchanges passwords = A [OK]
passwd is for changing passwords only [OK]- Confusing
passwdwith user creation commands - Thinking
passwdlists users - Assuming
passwddeletes accounts
alice as root?Solution
Step 1: Recall root usage of
Root can change any user's password by runningpasswdpasswd username.Step 2: Analyze options for correct syntax
Only passwd alice uses the correct syntax. Options A, B, and C use invalid or non-existent flags.Final Answer:
passwd alice -> Option DQuick Check:
Root changes user password withpasswd username= D [OK]
passwd username as root to change others' passwords [OK]- Adding wrong flags like -u or -p
- Using --change which is not a valid option
- Trying to change password without specifying username as root
passwd root?Solution
Step 1: Understand user permissions for
Only root can change other users' passwords. Normal users cannot change root's password.passwdStep 2: Predict command behavior for normal user
When a normal user triespasswd root, the system denies permission and shows an error.Final Answer:
The command will fail with a permission error. -> Option CQuick Check:
Normal user cannot change root password = A [OK]
- Assuming normal user can change root password
- Thinking user will be asked root's current password
- Believing user's own password changes instead
passwd alice as root but get the error: passwd: Authentication token manipulation error. What is the most likely cause?Solution
Step 1: Understand the error meaning
The error usually means insufficient permissions to update password files.Step 2: Identify cause based on command context
If not run as root or withsudo,passwdcannot change another user's password, causing this error.Final Answer:
You forgot to run the command withsudoor as root. -> Option BQuick Check:
Permission error means missing root privileges = C [OK]
passwd with root or sudo to avoid permission errors [OK]- Assuming user does not exist without checking
- Thinking password complexity causes this error
- Believing
passwdis missing from system
users.txt. Which script snippet correctly changes each user's password to NewPass123 using passwd?Solution
Step 1: Understand how to feed password to
Thepasswdpasswdcommand reads password from standard input when piped, requiring password twice (new and confirmation).Step 2: Analyze each script snippet
while read user; do echo "NewPass123" | passwd --stdin $user; done < users.txt uses--stdinwhich is not standard on all systems. for user in $(cat users.txt); do echo -e "NewPass123\nNewPass123" | passwd $user; done correctly usesecho -eto send password twice piped topasswd. for user in users.txt; do passwd $user NewPass123; done incorrectly treats file name as user list. cat users.txt | passwd -p NewPass123 misusespasswdwith pipe and-pwhich is invalid.Final Answer:
for user in $(cat users.txt); do echo -e "NewPass123\nNewPass123" | passwd $user; done -> Option AQuick Check:
Pipe password twice topasswdin loop = B [OK]
- Using non-standard
--stdinoption - Not sending password twice for confirmation
- Treating filename as username list without reading
- Misusing
passwdoptions
Ready to prove it?
Pass the test and this pattern moves unknown
