/etc/passwd and /etc/shadow in Linux CLI - Time & Space Complexity
When working with system files like /etc/passwd and /etc/shadow, it is important to understand how the time to read or search these files grows as they get larger.
We want to know how the time to find user information changes when the number of users increases.
Analyze the time complexity of the following command sequence.
# Search for a username in /etc/passwd
username="alice"
grep "^${username}:" /etc/passwd
# Search for the same username in /etc/shadow
grep "^${username}:" /etc/shadow
This code looks for a specific user's entry by scanning each line of the files /etc/passwd and /etc/shadow.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each line of the file one by one to check if it matches the username.
- How many times: Once for each line in the file, until the username is found or the file ends.
As the number of users (lines) in the file grows, the time to find a username grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 line checks |
| 100 | Up to 100 line checks |
| 1000 | Up to 1000 line checks |
Pattern observation: The time grows steadily as the file gets longer, because each line is checked one after another.
Time Complexity: O(n)
This means the time to find a user grows linearly with the number of users in the file.
[X] Wrong: "Searching for a username is instant no matter how many users there are."
[OK] Correct: The system reads each line until it finds the username, so more users mean more lines to check, which takes more time.
Understanding how searching through system files scales helps you reason about performance in real scripts and automation tasks.
"What if the file was indexed or stored in a database instead? How would the time complexity change?"
Practice
/etc/passwd file in Linux?Solution
Step 1: Understand the role of
The/etc/passwd/etc/passwdfile contains user account details like username, user ID, group ID, home directory, and shell. It is readable by all users.Step 2: Differentiate from
The encrypted passwords are stored separately in/etc/shadow/etc/shadowfor security, not in/etc/passwd.Final Answer:
To store user account information accessible to all users -> Option CQuick Check:
/etc/passwd= user info file [OK]
- Confusing /etc/passwd with /etc/shadow
- Thinking passwords are stored in /etc/passwd
- Assuming /etc/passwd stores system logs
/etc/shadow entry?Solution
Step 1: Identify
The/etc/shadowformat/etc/shadowfile stores encrypted passwords and related info. The password field looks like$6$randomsalt$encryptedpasswordfor SHA-512 encryption.Step 2: Compare options
The lineusername:$6$randomsalt$encryptedpassword:18000:0:99999:7:::matches the shadow file format with encrypted password (indicated by $6$) and password aging fields separated by colons. The other options resemble/etc/passwdformat or have incorrect password fields.Final Answer:
username:$6$randomsalt$encryptedpassword:18000:0:99999:7::: -> Option BQuick Check:
/etc/shadowentries have encrypted passwords with $ signs [OK]
- Confusing /etc/passwd format with /etc/shadow
- Expecting plain text passwords in /etc/shadow
- Missing fields after encrypted password
grep '^root:' /etc/passwd | cut -d':' -f1,3,7
What will be the output format?
Solution
Step 1: Understand the command parts
grep '^root:' /etc/passwdfinds the line starting with 'root:'.cut -d':' -f1,3,7extracts fields 1 (username), 3 (UID), and 7 (shell) separated by colons.Step 2: Identify root line fields
Typical root line:root:x:0:0:root:/root:/bin/bash. Fields: 1=root, 2=x, 3=0, 4=0, 5=root, 6=/root, 7=/bin/bash.Step 3: Extract fields 1,3,7
Fields 1=root, 3=0, 7=/bin/bash. Joined by colons:root:0:/bin/bash.Final Answer:
root:0:/bin/bash -> Option DQuick Check:
Fields 1,3,7 from /etc/passwd line = root:0:/bin/bash [OK]
- Mixing up field numbers in cut command
- Expecting full /etc/passwd line output
- Confusing shadow file format with passwd
sudo chage -l username
But it shows an error:
chage: user 'username' does not exist. What is the most likely cause?Solution
Step 1: Understand chage user check
Thechagecommand checks user password expiry info by looking up the username in/etc/passwd.Step 2: Analyze error cause
Error 'user does not exist' means the username is not found in/etc/passwd. If the user was missing there, chage cannot proceed.Final Answer:
The username is missing in /etc/passwd -> Option AQuick Check:
User must exist in /etc/passwd for chage to work [OK]
- Assuming /etc/shadow emptiness causes this error
- Thinking chage command missing causes user error
- Believing no password set triggers 'user does not exist'
/usr/sbin/nologin or /bin/false) by reading /etc/passwd. Which command snippet correctly filters these users?Solution
Step 1: Identify shell field in /etc/passwd
The 7th field in/etc/passwdis the user's login shell. We want to exclude/usr/sbin/nologinand/bin/false.Step 2: Analyze each option
awk -F':' '$7 != "/usr/sbin/nologin" && $7 != "/bin/false" {print $1}' /etc/passwd uses awk with field separator ':' and filters out unwanted shells, printing usernames (field 1). grep -v '/usr/sbin/nologin' /etc/shadow | cut -d':' -f1 wrongly reads /etc/shadow and uses grep -v incorrectly. cat /etc/passwd | grep '/bin/bash' | awk '{print $1}' filters only /bin/bash but misses other valid shells. sed '/nologin/d' /etc/passwd | cut -d':' -f7 deletes lines with 'nologin' but then prints shell field, not usernames.Final Answer:
awk -F':' '$7 != "/usr/sbin/nologin" && $7 != "/bin/false" {print $1}' /etc/passwd -> Option AQuick Check:
Use awk on /etc/passwd to filter shell field and print usernames [OK]
- Using /etc/shadow instead of /etc/passwd for shell info
- Printing wrong fields (shell instead of username)
- Using grep without proper field filtering
Ready to prove it?
Pass the test and this pattern moves unknown
