Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 37 ยท Users, Groups and Permissions

Linux Users and the passwd, shadow and group Files

Understand how Linux stores accounts: UIDs, GIDs, /etc/passwd, /etc/shadow, /etc/group, system versus regular users and the special root account.

Every process on Linux runs as a user, and every file belongs to a user and a group. The kernel only knows numbers: the user ID (UID) and group ID (GID). Names are a convenience stored in a few text files. Understanding those files lets you read, audit and repair accounts.

UIDs and GIDs

  • UID 0 is root, the superuser. The kernel skips most permission checks for it.
  • Low numbers (below 1000 on most distributions) are system accounts for services such as www-data or sshd. They usually cannot log in.
  • Numbers from 1000 upward are regular users. Some distributions start at 500. Check UID_MIN in /etc/login.defs.
  • Every user has one primary group and may belong to many supplementary groups.
Terminal
id
id dana
getent passwd dana
getent group sudo

getent asks all configured sources (local files, LDAP, and so on), so it is the right tool in scripts.

/etc/passwd

One line per account, seven colon-separated fields:

TEXT
dana:x:1001:1001:Dana Lee,,,:/home/dana:/bin/bash
Field Meaning
1 Login name
2 Password placeholder x: the real hash is in /etc/shadow
3 UID
4 Primary GID
5 Comment (GECOS): full name and contact info
6 Home directory
7 Login shell

The file is world-readable because programs need to translate UIDs to names. That is why password hashes were moved out of it.

A shell of /usr/sbin/nologin or /bin/false means the account cannot log in interactively. That is correct for service accounts.

/etc/shadow

Readable by root only. Nine fields:

TEXT
dana:$y$j9T$...hash...:19800:0:90:7:14::
Field Meaning
1 Login name
2 Password hash, or ! or * when locked or unset
3 Day of last change (days since 1970-01-01)
4 Minimum days between changes
5 Maximum days a password is valid
6 Warning days before expiry
7 Inactivity days after expiry
8 Account expiry date (days since epoch)
9 Reserved

The hash begins with an id such as $y$ (yescrypt), $6$ (SHA-512) or $2b$ (bcrypt), then a salt and the digest. Never reuse or paste a real hash into a chat or ticket.

/etc/group

TEXT
sudo:x:27:dana,lee
developers:x:1002:dana

Fields: group name, password placeholder, GID, comma-separated members. A user's primary group is set in /etc/passwd and need not be listed here.

flowchart LR
    A["Login name"] --> B["/etc/passwd: UID, GID, home, shell"]
    B --> C["/etc/shadow: password hash"]
    B --> D["/etc/group: extra groups"]

Three files describe an account.

Safe ways to edit

Never edit these files with a normal editor on a busy system. Use commands that lock and validate:

Terminal
sudo vipw           # edits /etc/passwd with locking
sudo vipw -s        # edits /etc/shadow
sudo pwck           # checks passwd and shadow for consistency
sudo grpck          # checks group files

Normally you use useradd, usermod and friends (Managing users and groups).

Who is logged in and who was

Terminal
who
w
last -n 10
lastb | head         # failed logins (root only)
lslogins

A stand-in: parse passwd

This model parses sample lines the way a script would, to separate system accounts from regular ones and to list who has a login shell. The data is made up.

Python Stand-in: audit passwd

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A stand-in: decode shadow ages

The ages in /etc/shadow are day counts. This model converts them into dates. It uses an invented line and a made-up "today".

Python Stand-in: when does the password expire?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Giving a service account a real shell and password.
  • Reusing UIDs; a new user could then own a deleted user's old files.
  • Editing /etc/passwd by hand and breaking its seven fields, which can lock everyone out.
  • Treating usernames as secret; they are not, hashes are.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the UID of the root user?

    UID 0 identifies the superuser.
  2. 2. Why is the password field in /etc/passwd just an x?

    Splitting the hash into shadow keeps it away from ordinary users.
  3. 3. What does a login shell of /usr/sbin/nologin do?

    It is used for service accounts that must not log in.
  4. 4. Which tool should you use to inspect an account from scripts regardless of directory service?

    getent consults every configured source, not only local files.

Summary

  • The kernel uses UIDs and GIDs; names map to them via /etc/passwd and /etc/group.
  • /etc/shadow holds hashes and password aging, readable by root only.
  • System accounts have low UIDs and no login shell.
  • Use vipw, pwck and getent instead of risky manual edits.

Next: Managing Users and Groups