etc passwd and etc shadow: Read Every Field Safely
In this article
To read /etc/passwd and /etc/shadow safely, you need to know that /etc/passwd contains world-readable user account information (username, UID, GID, home directory, shell) with a placeholder in the password field, while /etc/shadow stores the actual hashed passwords and password policy data, readable only by root and the shadow group. This article explains every field in both files, how to interpret them, and how to avoid common security pitfalls when examining them on a Linux system.
Background: Why Two Files?
Originally, Unix systems stored all user account data, including the password hash, in a single world-readable file: /etc/passwd. Because every user needed to look up usernames, UIDs, and home directories, the file had to be readable by everyone. But that also meant password hashes were exposed to any local user, making offline brute-force attacks trivial.
The solution, introduced in the 1980s with SunOS and later adopted by Linux, was to split the data. The public information stayed in /etc/passwd, and the sensitive password hashes moved to /etc/shadow, a file readable only by root (and sometimes a dedicated shadow group). The password field in /etc/passwd was replaced with an 'x' to indicate that the real hash lives in /etc/shadow.
Reading /etc/passwd Field by Field
Each line in /etc/passwd represents one user account and contains seven colon-separated fields. Here's what each one means:
- Username: The login name, unique on the system (e.g.,
jdoe). - Password placeholder: Usually
xor*. Anxmeans the real hash is in /etc/shadow. A*often means the account is locked or has no password. - UID (User ID): A numeric identifier. 0 is root, 1-999 are typically system accounts, and 1000+ are regular users (on most distributions).
- GID (Group ID): The primary group's numeric ID for the user.
- GECOS field: A comma-separated comment field, usually holding the user's full name, phone number, or other contact info. It's optional and often empty.
- Home directory: The absolute path to the user's home directory (e.g.,
/home/jdoe). - Login shell: The shell launched at login (e.g.,
/bin/bash). System accounts often have/usr/sbin/nologinor/bin/falseto prevent interactive logins.
Example line: jdoe:x:1000:1000:John Doe:/home/jdoe:/bin/bash
Reading /etc/shadow Field by Field
/etc/shadow also has colon-separated fields, but they are more cryptic. There are nine fields:
- Username: Matches the username in /etc/passwd.
- Password hash: The hashed password, or one of several special values:
!or*means the account is locked;!!means no password has ever been set. A hash like$6$salt$hashindicates SHA-512 (the$6$prefix). Other prefixes:$1$is MD5,$2y$is bcrypt,$5$is SHA-256. - Last password change: Days since January 1, 1970 (the Unix epoch) when the password was last changed.
- Minimum password age: Minimum number of days before the password can be changed again.
- Maximum password age: Maximum number of days after which the password must be changed.
- Warning period: Days before the password expires that the user is warned.
- Inactivity period: Days after password expiration that the account remains usable before being locked.
- Expiration date: Days since the epoch when the account expires (not the password).
- Reserved field: Unused, often empty.
Example line: jdoe:$6$abc123$def456...:18001:0:99999:7:30:19000:
How to Read These Files Safely
The biggest safety concern is never exposing the contents of /etc/shadow to unauthorized users. Here are practical guidelines:
- Use
catorlessfor /etc/passwd – it's world-readable, so any user can view it. - Use
sudo cat /etc/shadoworsudo less /etc/shadow– you need root privileges. Never chmod /etc/shadow to make it readable. - Avoid copying these files to insecure locations – never paste a shadow file into a chat, forum, or email.
- Use
getentcommands –getent passwdandgetent shadowshow the same data but also work with NIS or LDAP. - Be careful with
grepon /etc/shadow – if you must search, usesudo grepand avoid outputting to a file.
Common Misconceptions and Pitfalls
One frequent mistake is thinking the hash in /etc/shadow is the password itself. It's a one-way hash; you can't reverse it. Another is assuming all systems have a shadow file – some minimal containers or embedded systems may not, and then the hash appears directly in /etc/passwd's second field.
Another pitfall: the last password change field is often misinterpreted as a date. It's an integer counting days since 1970-01-01. To convert: date -d '1970-01-01 + 18001 days'.
Also, a locked account doesn't mean the user can't log in via SSH keys – it only prevents password authentication. Always check the account status with passwd -S username to see if it's locked, has a password, or is expired.
Comparing /etc/passwd and /etc/shadow
Here's a quick comparison:
- Permissions: /etc/passwd is 644 (world-readable), /etc/shadow is 640 or 600 (root-only).
- Purpose: passwd holds identity data; shadow holds authentication data.
- Fields: passwd has 7 fields; shadow has 9.
- Who reads them: Any user can read passwd; only root or shadow group can read shadow.
- Editing: Use
vipwfor passwd andvipw -sfor shadow to edit safely with locking.
Security Best Practices
To keep your system secure:
- Never weaken permissions on /etc/shadow – keep it root:shadow with mode 640.
- Use strong password hashes – prefer
yescryptorargon2on modern systems; avoid MD5. - Monitor for unauthorized changes – use auditd or tripwire to watch these files.
- Lock unused accounts – use
usermod -Lorpasswd -l. - Backup securely – if you back up /etc, ensure the backup is encrypted and access-restricted.
Frequently Asked Questions
What does the 'x' in /etc/passwd mean?
The 'x' in the password field of /etc/passwd indicates that the actual password hash is stored in /etc/shadow. It's a placeholder; if you see a hash instead, your system isn't using shadow passwords.
How do I check if a user account is locked?
Look at the password hash field in /etc/shadow. If it starts with ! or *, the account is locked. You can also run passwd -S username as root to see the status.
What is the difference between UID and GID?
UID is the user identifier, unique to each user. GID is the primary group identifier. A user can belong to multiple groups, but the GID in /etc/passwd is the primary one, used for file ownership by default.
Why does my /etc/shadow show '!!' in the password field?
'!!' means no password has ever been set for that account. The account is effectively locked because there's no password to authenticate with, though it could still use SSH keys if configured.
Can I edit /etc/shadow directly?
You shouldn't edit it directly with a text editor. Use vipw -s which locks the file and prevents concurrent edits. For changing passwords, use passwd or chage.