Linux Command Reference ยท Special Files and Directories
The /etc/passwd file
Reference for /etc/passwd: the seven colon-separated fields, system versus normal accounts, login shells, how to read it with getent and awk, and safe ways to edit it.
/etc/passwd is the plain-text database of user accounts on a Linux system. Each line describes one account: its login name, numeric user ID, primary group, home directory and login shell. The name is historical: passwords used to be stored here, but today the hash lives in /etc/shadow, which only root can read, while /etc/passwd stays world-readable because many programs need to translate user IDs into names (ls -l does it for every file owner).
Do not read or edit it with ad-hoc tools if you can avoid it. Use getent passwd to look accounts up (it also consults LDAP and other sources), and useradd, usermod and userdel to change them.
name:password:UID:GID:comment:home:shell root:x:0:0:root:/root:/bin/bash
Try it
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 read or cat), it gets the next line from this box.
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 read or cat), it gets the next line from this box.
Syntax
One account per line, seven fields separated by colons:
name:password:UID:GID:GECOS:home:shell
| Field | Description |
|---|---|
name |
Login name. Lowercase letters, digits, _ and -, usually at most 32 characters; it must not start with a digit or a dash. |
password |
Normally x, meaning "the hash is in /etc/shadow". An empty field means no password at all, which is dangerous. A * or ! means password login is disabled. |
UID |
Numeric user ID. 0 is root. 1-999 are system accounts and 1000 and up are normal users on most distributions (set by UID_MIN in /etc/login.defs). |
GID |
Numeric ID of the primary group, which must exist in /etc/group. |
GECOS |
Free-text comment: full name, office, phone, separated by commas. Shown by finger and chfn. |
home |
Absolute path of the home directory, the starting directory for a login and the value of $HOME. |
shell |
Program started at login. /usr/sbin/nologin or /bin/false prevent interactive logins. Empty means /bin/sh. |
Returns: reading the file has no status; getent passwd NAME returns 0 when the account is found and 2 when it is not.
Errors and edge cases: a malformed line (wrong number of fields, duplicate names or IDs) can lock people out; pwck checks the file for consistency.
Usage notes
- Permissions. The file is owned by root with mode
644. If it were writable by others, anyone could create a root account. - Edit with
vipw. If you must edit by hand, usevipw, which locks the file against concurrent changes, and runpwckafterwards. Keep a root shell open while testing. - Service accounts. Daemons such as
www-data,postgresorsshdget their own system account with anologinshell, so a compromised service cannot be used to log in. - Several accounts can share a UID, but the kernel and file permissions only know the number, so this is almost always a mistake. Looking up an ID returns only the first matching name.
- Usernames in output.
ls -landpstranslate numeric IDs through the system's name service, not just this file. On a machine using LDAP or SSSD,/etc/passwdlists only the local accounts, so usegetent. - Home directory is not created automatically by editing the file.
useradd -mcreates it and copies/etc/skel. - Comments. The GECOS field can hold any text except a colon or a newline; it is not used for authentication.
- Containers. Minimal images often have only a handful of lines; a Dockerfile
USERline needs an entry here (or a numeric UID) to resolve the name.
Related
/etc/shadow,/etc/group,useradd,usermod,passwd- Lessons: Users and Permissions, Permissions, Hardening