What if a simple file holds the keys to your entire system's security?
Why /etc/passwd and /etc/shadow in Linux CLI? - Purpose & Use Cases
Imagine you are managing user accounts on a Linux system by opening text files manually to check usernames and passwords. You have to scroll through long lists in /etc/passwd and /etc/shadow files to find details.
This manual approach is slow and risky. Editing or reading these files directly can cause mistakes like exposing sensitive password hashes or corrupting user data, leading to security problems or locked accounts.
Understanding the roles of /etc/passwd and /etc/shadow helps you use commands and scripts safely to manage users. The system separates public user info from private password data, keeping security tight and management easier.
cat /etc/passwd sudo cat /etc/shadow
getent passwd sudo passwd -S username
This knowledge lets you automate user management securely without risking password exposure or system errors.
System administrators regularly check user account status and password expiry using these files indirectly to keep systems safe and users informed.
/etc/passwd stores public user info like usernames and IDs.
/etc/shadow stores encrypted passwords and security details, hidden from normal users.
Separating these files improves security and allows safer automation.
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
