Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

/etc/passwd — The File That Defines Every Account on Your System

/etc/passwd is the plain-text database of every user account on a Linux system — one line per account, seven colon-separated fields, world-readable.


What is /etc/passwd, and Why Care?

It’s not a command — it’s a file, and one of the most important on any Linux machine. Every account that can log in, run a service, or own a file has a line here. When you run id, whoami, or ls -l, the names in the output are looked up from this file (and /etc/group).

Each line looks like this:

alice:x:1000:1000:Alice Smith,,/home/alice:/bin/bash

Why Learn /etc/passwd?

  • It’s where the identity model becomes concrete — users, groups, home directories, and shells all in one readable file.
  • It explains half the output you see — id, ls -l, ps, and login prompts all pull names from here.
  • It’s a debugging tool — “does this account exist? what shell? what home?” is a grep away.
  • It’s safe to read — world-readable by design; the passwords are not here (they’re in /etc/shadow).

Think of /etc/passwd as the system’s phone book: it maps every account number to a name, a home, and a shell. Reading it is how you stop guessing about who and what is running on your machine.


1. Reading the Seven Fields

Each line has exactly seven colon-separated fields:

alice : x : 1000 : 1000 : Alice Smith,,/home/alice : /bin/bash
  1     2    3      4            5                    6

Wait — six fields? Count again, splitting field 5 properly:

1. username       — the login name
2. password placeholder — always `x` (the real hash lives in /etc/shadow)
3. UID            — numeric user ID; 0 is root
4. GID            — numeric primary group ID
5. GECOS          — comment: full name, office, phone (free text)
6. home directory — where the user lands on login
7. login shell    — the program run at login

Example:

$ head -3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
  • Field 2 being x is universal on modern systems — it means “password hash is elsewhere”.
  • Field 7 of /usr/sbin/nologin means the account cannot be logged into interactively — that’s how services are locked down.

2. Finding Accounts

grep is the everyday tool for querying the file:

# Does this account exist?
grep '^bob:' /etc/passwd

# Show all "real" human accounts (UID >= 1000)
awk -F: '$3 >= 1000 {print $1}' /etc/passwd

# Which accounts have a real login shell?
grep -v nologin /etc/passwd | grep -v /bin/sync

Example:

$ grep '^alice:' /etc/passwd
alice:x:1000:1000:Alice Smith,,/home/alice:/bin/bash

$ awk -F: '$3 >= 1000 {print $1, $7}' /etc/passwd
alice /bin/bash
bob /bin/bash
  • The ^name: anchor prevents partial matches (e.g., bob matching bobby).
  • Filtering out nologin shells is the quick way to list accounts that can actually log in.

3. Who Wrote This Line, and When?

The file is edited by tools, not by hand:

ToolWhat it does to /etc/passwd
useraddAdds a new account line
usermodModifies a field (shell, home, etc.)
userdelRemoves the account line
passwdWrites to /etc/shadow, not here
vipwSafely edits the file with checks

Example:

# See what useradd just created
$ sudo useradd -m -s /bin/bash testuser
$ grep testuser /etc/passwd
testuser:x:1004:1004::/home/testuser:/bin/bash
  • Direct edits are possible but discouraged — use vipw, which locks the file and validates syntax.
  • A corrupted /etc/passwd can prevent anyone from logging in, which is why the safe-edit tool exists.

4. The Companion Files

/etc/passwd is one member of a small family:

$ grep alice /etc/group
alice:x:1000:
sudo:x:27:alice,dev

$ sudo grep alice /etc/shadow
alice:$y$j9T$Qz9...:19700:0:99999:7:
FileContentsReadable by
/etc/passwdAccounts: names, UIDs, shellsEveryone
/etc/shadowPassword hashes + expiry policyRoot only
/etc/groupGroups and their membersEveryone
/etc/gshadowGroup passwords (rarely used)Root only
  • Together these four files are the identity system — everything id prints comes from them.
  • The split into shadow exists for security: hashes must not be world-readable, or they could be cracked offline.

5. UID Ranges at a Glance

The numeric ID in field 3 tells a story about the account:

0          → root (full admin)
1–999      → system/service accounts (created with useradd -r)
1000–65533 → regular human users
65534      → nobody (the least-privileged account)

Example:

# Service accounts live in the low range
$ awk -F: '$3 < 1000 && $3 != 0 {print $1, $3}' /etc/passwd | head -5
daemon 1
bin 2
sys 3
sync 4

# The nobody account
$ grep nobody /etc/passwd
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
  • When a file is owned by a UID with no matching line (e.g., after deleting a user), ls -l shows the raw number instead of a name — a classic sign of an orphaned account.

Common Pitfalls

  • Looking for passwords here. Field 2 is just x; the hashes are in /etc/shadow. If you find a real hash in /etc/passwd, the system is misconfigured (or ancient).
  • Editing the file by hand. Use vipw (or the useradd/usermod tools) — a stray character can lock everyone out.
  • Assuming every line is a person. Most lines are services (daemon, www-data, postgres) with nologin shells; filter before concluding.
  • Confusing GECOS with “comment”. Field 5 is structured free text (full name, office, phone) — some tools parse it, so don’t put arbitrary junk there.
  • Deleting a user without cleaning files. userdel without -r leaves files owned by a UID that no longer has a line here — ls -l will show bare numbers.
  • Trusting names over numbers. Permissions are enforced by UID/GID; if names look right but access fails, compare the numeric IDs with id.

PageWhy it’s related
useraddCreates the lines that appear in this file
passwdThe password tool whose name points here (historically)
idPrints identity info looked up from this file
whoamiThe username every line of this file defines
suSwitching between accounts defined here
lsls -l resolves owner names through this file

/etc/passwd Cheatsheet

CommandMeaning
cat /etc/passwdView every account line
grep '^alice:' /etc/passwdLook up one exact account
awk -F: '{print $1, $3, $7}' /etc/passwdList usernames, UIDs, and shells
awk -F: '$3 >= 1000' /etc/passwdList human (non-system) accounts
grep -v nologin /etc/passwdAccounts that can log in interactively
grep nobody /etc/passwdThe least-privileged account
sudo vipwSafely edit the file with validation
getent passwd aliceResolve an account (works with LDAP too)