/etc/passwd vs /etc/shadow: Roles in User Management

How /etc/passwd and /etc/shadow work together in Linux: the 7 passwd fields, UIDs, NSS and PAM at login, group files, vipw, pwck and audits.

Aug 2, 2025 - 11:33
Nov 5, 2026 - 17:36
 0  176
TOPICDevOps BasicsPractical guide
/etc/passwd vs /etc/shadow: Roles in User Management

In Linux user management, /etc/passwd is the world-readable account directory: it maps each username to a UID, primary GID, home directory and login shell. /etc/shadow is the root-only companion that holds the password hash and the password ageing rules. Login programs read both, through NSS and PAM, to decide who you are and whether you may sign in.

This guide is about how the two files work together as a system: the seven fields of /etc/passwd, how a username becomes a UID, how the shadow and group files fit in, and the safe commands for changing accounts. The field-by-field detail of /etc/shadow (hash formats, ageing dates, lock markers) has its own guide, What Is the /etc/shadow File in Linux Authentication?, so it gets a summary here. For the bigger picture of Linux administration on Red Hat systems, start with the RHEL 10 beginner's setup guide.

Key takeaways

  • /etc/passwd has seven colon-separated fields per account. The second field is almost always x, which means the real hash is in /etc/shadow.
  • The kernel only knows numbers. /etc/passwd is how tools turn the UID on a file or process into a name, which is why it must be readable by everyone (mode 644).
  • /etc/shadow is readable only by root (mode 000 or 600 on RHEL, 640 with group shadow on Debian and Ubuntu).
  • /etc/group and /etc/gshadow do the same split for groups.
  • Programs do not read the files directly. They call NSS (getent, getpwnam()) and PAM, which may also pull accounts from LDAP or Active Directory through SSSD.
  • Change accounts with useradd, usermod, passwd, chage, or vipw. Check consistency with pwck and grpck.

Why Linux uses two files

Early Unix kept password hashes in /etc/passwd itself. That file has to be readable by every user, because commands such as ls -l, ps and id need to turn numeric user IDs into names. Anyone could therefore copy all the hashes and try to crack them offline. The shadow password system moved the hashes to a separate file that only root can read and left the rest of the account record public. Linux distributions have used shadow passwords by default for decades, and the split is documented in the passwd(5) and shadow(5) manual pages.

/etc/passwd/etc/shadow
PurposeWho the account isHow the account proves it, and for how long
Main contentsUsername, UID, GID, comment, home, shellPassword hash, last change date, min/max age, warning, inactivity, expiry
Readable byEveryoneroot only (or the shadow group on Debian/Ubuntu)
Typical mode (RHEL)-rw-r--r-- (644)---------- (000)
Fields per line79
Safe editorvipwvipw -s
Consistency checkpwckpwck (checks both)
Group counterpart/etc/group/etc/gshadow

The seven fields of /etc/passwd

Each line describes one account. Here is a typical entry:

priya:x:1001:1001:Priya Sharma,,,:/home/priya:/bin/bash
#FieldExampleMeaning
1UsernamepriyaLogin name. Must be unique on the system.
2Passwordxx means "look in /etc/shadow". An empty field means no password, which is dangerous.
3UID1001Numeric user ID. This is what the kernel stores on files and processes.
4GID1001Primary group ID. Must exist in /etc/group.
5GECOS / commentPriya Sharma,,,Full name and optional contact details. Free text.
6Home directory/home/priyaWorking directory after login.
7Login shell/bin/bashProgram started at login. /sbin/nologin blocks interactive logins.

UID ranges

UID 0 is root. Any account with UID 0 has full root privileges whatever its name, which is why an extra UID 0 entry is one of the first things an audit looks for. System and service accounts get low UIDs, and human users start at the value of UID_MIN in /etc/login.defs, which is 1000 on current RHEL, Fedora, Debian and Ubuntu releases. Check your own system rather than assuming:

grep -E '^(UID_MIN|UID_MAX|SYS_UID_MIN|SYS_UID_MAX)' /etc/login.defs

Service accounts and nologin

Daemons such as nginx, postgres or sshd run under their own accounts so a compromise of one service does not hand over files owned by another. These accounts usually have a shell of /sbin/nologin or /usr/sbin/nologin and a locked password in /etc/shadow. The nologin program prints a message and exits, so nobody can get an interactive shell even if they authenticate somehow.

sudo useradd --system --shell /sbin/nologin --home-dir /var/lib/myapp myapp

/etc/shadow in one section

Each shadow line has nine fields: username, password hash, date of last change, minimum age, maximum age, warning period, inactivity period, account expiry date and a reserved field. Dates are counted in days since 1 January 1970. The hash field starts with an identifier such as $6$ (SHA-512 crypt) or $y$ (yescrypt, the default on recent Fedora, RHEL 9 and later, Debian and Ubuntu). A leading ! or * means password login is disabled.

priya:$y$j9T$...hash...:20380:0:90:7:14::

The /etc/shadow guide explains each field, the special values, and how to manage ageing with chage.

How the two files are used at login

When someone signs in at a console or over SSH with a password, roughly this happens:

  1. The login program asks NSS for the account by name. NSS checks the sources listed for passwd in /etc/nsswitch.conf, usually files (meaning /etc/passwd) followed by sss or systemd.
  2. PAM runs the authentication stack. The pam_unix module reads the hash from /etc/shadow (it runs with root privileges, or through a helper), hashes the typed password with the same algorithm and salt, and compares the results.
  3. PAM's account phase checks the shadow ageing fields: is the password expired, is the account expired, has the inactivity window passed?
  4. The session starts with the UID and GID from /etc/passwd, supplementary groups from /etc/group, the home directory and the shell.

SSH key logins skip step 2 but still use /etc/passwd for the UID, home directory (where ~/.ssh/authorized_keys lives) and shell, and still run PAM's account checks when UsePAM is enabled. That is why locking only the password does not always stop a key-based login. To disable an account fully, also expire it (usermod --expiredate 1) or set its shell to nologin. The pam_unix(8) page documents the module.

Look up accounts the way programs do

Because NSS can merge several sources, grepping /etc/passwd may miss users who come from LDAP or Active Directory. getent asks NSS directly:

getent passwd priya        # account record, from any configured source
getent group developers    # group record
id priya                   # UID, primary GID and all groups
grep '^passwd' /etc/nsswitch.conf

The group files

/etc/group lists each group with four fields: name, password placeholder, GID and a comma-separated list of supplementary members. /etc/gshadow holds group passwords (rarely used) and group administrators, and like /etc/shadow it is readable only by root.

developers:x:2001:priya,rahul

A user's primary group comes from field 4 of /etc/passwd and is not repeated in the member list of /etc/group. Supplementary groups come only from /etc/group. Run id after changes, and remember that a running session only picks up new groups after the user logs in again (or runs newgrp).

Managing accounts safely

Use the shadow-utils commands rather than editing the files by hand. They lock the files, keep passwd and shadow in step, and create home directories with the right ownership.

TaskCommandFiles changed
Create a user with a home directorysudo useradd -m -c "Priya Sharma" priyapasswd, shadow, group, gshadow
Set or change a passwordsudo passwd priyashadow
Add to a supplementary groupsudo usermod -aG developers priyagroup, gshadow
Change the login shellsudo usermod -s /bin/zsh priyapasswd
Lock password loginsudo usermod -L priyashadow
Expire the account entirelysudo usermod --expiredate 1 priyashadow
Set password ageingsudo chage -M 90 -W 7 priyashadow
Delete a user and home directorysudo userdel -r priyaall four files, plus home

Note the -a in usermod -aG. Without it, -G replaces the user's supplementary group list instead of adding to it, a common way to accidentally remove someone from wheel or sudo. Step-by-step examples on Red Hat systems are in the RHEL 10 user management tutorial.

When you must edit by hand

Use vipw for /etc/passwd and vipw -s for /etc/shadow (vigr and vigr -s for the group files). They lock the file so a concurrent passwd run cannot overwrite your change, open a copy in your editor, and install it only when you save. Then run the checkers:

sudo pwck -r     # read-only check of passwd and shadow
sudo grpck -r    # read-only check of group and gshadow

pwck reports wrong field counts, duplicate names, missing home directories, invalid shells and users present in one file but not the other. If the two files have drifted, pwconv creates or updates /etc/shadow from /etc/passwd.

Permissions and ownership to verify

ls -l /etc/passwd /etc/shadow /etc/group /etc/gshadow

On RHEL and Fedora, expect /etc/passwd and /etc/group as root root 644, and /etc/shadow and /etc/gshadow as root root 000. Root can still read a mode 000 file because root bypasses normal permission checks. On Debian and Ubuntu, shadow and gshadow are typically root shadow 640. If your files differ, restore the packaged values rather than guessing: on RPM systems rpm --setperms setup resets files owned by the setup package. SELinux labels matter too on RHEL; restorecon -v /etc/shadow fixes a wrong context. For more on how permission bits work, see chmod vs chown vs chgrp.

Security checks worth automating

# Accounts other than root with UID 0
awk -F: '$3 == 0 && $1 != "root"' /etc/passwd

# Accounts with an empty password field in shadow
sudo awk -F: '$2 == ""' /etc/shadow

# Password field in passwd that is not "x"
awk -F: '$2 != "x"' /etc/passwd

# Human accounts (UID 1000 and up) with an interactive shell
awk -F: '$3 >= 1000 && $7 !~ /nologin|false/' /etc/passwd

# Duplicate UIDs
cut -d: -f3 /etc/passwd | sort | uniq -d

Each of these should normally return nothing (except the human account list, which you compare with who should have access). The CIS Benchmarks for Linux distributions include similar checks. To get alerted when the files change, watch them with auditd:

sudo auditctl -w /etc/passwd -p wa -k identity
sudo auditctl -w /etc/shadow -p wa -k identity
sudo ausearch -k identity

Put the rules in a file under /etc/audit/rules.d/ to make them permanent. Privileged access is the other half of account security; see securing sudo access.

Central accounts: LDAP, FreeIPA and Active Directory

On fleets of servers, local files do not scale. Organisations keep users in a directory (LDAP, Red Hat Identity Management/FreeIPA, or Active Directory) and run SSSD on each host. SSSD adds an sss source to nsswitch.conf and a PAM module, so getent passwd and logins see directory users alongside local ones. Those users never appear in /etc/passwd or /etc/shadow. Local files still hold root and the service accounts, which is why they still matter on a directory-joined host. Red Hat's RHEL authentication and authorization guide covers SSSD and authselect.

Users in containers

Container images carry their own /etc/passwd and /etc/group. When a Dockerfile says USER 10001 and the image has no matching entry, processes still run with that UID, but tools show no name and some programs that look up their home directory fail. Create the account in the image, or at least an entry, so the UID resolves:

RUN groupadd --gid 10001 app && \
    useradd --uid 10001 --gid app --no-create-home --shell /sbin/nologin app
USER 10001

Images usually contain no password hashes at all, because nobody logs into a container with a password.

Troubleshooting

"user does not exist" but the name is in a directory

Run getent passwd name. If it returns nothing, check that SSSD is running and that sss is listed for passwd in /etc/nsswitch.conf.

Nobody can log in after an edit

Usually a broken line in /etc/passwd or wrong permissions on /etc/shadow. Boot to a root shell or rescue mode, run pwck -r, and compare against the backup copies /etc/passwd- and /etc/shadow- that shadow-utils keeps.

Files show a number instead of an owner name

The UID on the file has no matching entry, often after a user was deleted or a disk was moved from another host. Use find / -nouser to list them and reassign ownership.

Learn it in a lab

Account management and the passwd/shadow files are core objectives for Red Hat's RHCSA exam. The RHCSA training and the Linux administration course practise these tasks on real RHEL machines, in the Kharadi, Pune classroom or live online. For more questions in this area, see the Linux system administration interview questions.

Frequently asked questions

What is the difference between /etc/passwd and /etc/shadow?

/etc/passwd stores public account details (username, UID, GID, comment, home directory, shell) and is readable by everyone. /etc/shadow stores the password hash and ageing rules and is readable only by root.

What does the x in /etc/passwd mean?

It means the password hash is stored in /etc/shadow. An empty second field would mean the account has no password at all.

Why is /etc/passwd readable by all users?

Commands such as ls, ps and id need to translate numeric UIDs into usernames. Since the hashes were moved to /etc/shadow, the remaining data is not secret.

How many fields are in /etc/passwd and /etc/shadow?

/etc/passwd has seven fields per line. /etc/shadow has nine.

How should I edit /etc/passwd safely?

Use useradd, usermod and userdel where possible. For manual changes use vipw (vipw -s for shadow), then run pwck -r to check consistency.

What happens if a non-root user has UID 0?

That account has full root privileges, because the kernel checks the number, not the name. Treat any extra UID 0 entry as a serious finding.

Why does a user from LDAP not appear in /etc/passwd?

Directory users come through SSSD and NSS, not the local files. Use getent passwd username to see them.

What are /etc/group and /etc/gshadow?

They are the group equivalents: /etc/group lists group names, GIDs and members, and /etc/gshadow holds group passwords and administrators with root-only access.

Official references

Interested in learning more?Get a callback from our training advisors.

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Angry Angry 0
Sad Sad 0
Wow Wow 0
Mridul I am a passionate technology enthusiast with a strong focus on DevOps, Cloud Computing, and Cybersecurity. Through my blogs at DevOps Training Institute, I aim to simplify complex concepts and share practical insights for learners and professionals. My goal is to empower readers with knowledge, hands-on tips, and industry best practices to stay ahead in the ever-evolving world of DevOps.