Beginner · Permissions, users and sudo · topic 7 of 57 · 30 min
Users, groups and the files behind them
Explanation
Why users exist
A server is shared by many people and many programs. Users keep them apart: the web server should not read the database files, and an intern should not be able to delete the payroll data. Linux decides what is allowed by checking which user and group a process runs as.
Three kinds of user
| Kind | UID on RHEL 9 | Example |
|---|---|---|
| Superuser | 0 | root: can do anything |
| System users | 1 to 999 | apache, sshd, chrony: run services, usually cannot log in |
| Regular users | 1000 and up | asha, ravi: people |
The name is for humans; the kernel only uses the number. If two names had the same UID, Linux would treat them as one user. The ranges come from UID_MIN and UID_MAX in /etc/login.defs.
/etc/passwd: the user list
asha:x:1000:1000:Asha Rao:/home/asha:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin- username (asha)
- password field: x means "the hash is in /etc/shadow"
- UID (1000)
- GID of the primary group (1000)
- GECOS: a comment, usually the full name
- home directory (/home/asha)
- login shell (/bin/bash). /sbin/nologin stops interactive logins, which is right for service accounts.
Everyone can read /etc/passwd, because many programs need to turn UIDs into names. That is exactly why the password hashes were moved out of it.
/etc/shadow: password hashes and ageing
asha:$6$Jx2...long-hash...:20015:0:90:7:::The fields are: name, password hash, the day the password was last changed (counted in days since 1 January 1970), minimum days between changes, maximum days before a change is forced, warning days, inactive days, account expiry date, and a reserved field. A hash starting with $6$ is SHA-512. A ! or * in the hash field means the password is locked or unset, so password login is not possible.
Groups: /etc/group
wheel:x:10:asha,ravi
developers:x:1005:asha,meenaEach user has one primary group (the GID in /etc/passwd). New files get that group. A user can also be in supplementary (extra) groups, listed in /etc/group. On RHEL each new user gets a private group with the same name, so asha's primary group is asha.
$ id asha
uid=1000(asha) gid=1000(asha) groups=1000(asha),10(wheel),1005(developers)
$ getent passwd asha
asha:x:1000:1000:Asha Rao:/home/asha:/bin/bash
$ groups ravi
ravi : ravi wheelCommon mistakes
- Editing /etc/passwd or /etc/shadow by hand with a normal editor. One wrong colon can lock everyone out. Use useradd/usermod, or vipw and vigr, which lock the file and check it.
- Thinking a group change works at once: group membership is read at login. The user must log out and in (or run newgrp) to get a new group.
- Giving a service account a real shell. Services should have /sbin/nologin.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Users, groups and the files behind them · 1/6
Every process runs as a user. Linux checks the user and group before allowing anything.
Prefer a video by a teacher? Find free videos on “linux /etc/passwd /etc/shadow /etc/group explained” on YouTube ↗
In real life: An audit at a cooperative bank
An IT auditor at a cooperative bank in Gujarat asks: "Which accounts on this server have UID 0, and which human accounts can log in?" The admin answers in two lines: an awk command over /etc/passwd shows that only root has UID 0, and another lists the users with UID 1000 or more and a real shell. The auditor marks the check as passed.
$ awk -F: '$3 == 0 {print $1}' /etc/passwd
root
$ awk -F: '$3 >= 1000 && $7 !~ /nologin/ {print $1, $7}' /etc/passwd
asha /bin/bash
ravi /bin/bashPractice (0/3 done)
In /etc/passwd, what does the x in the second field mean?
Write parse_passwd(line) that turns one /etc/passwd line into a dict with the keys name, uid, gid, comment, home and shell. uid and gid must be int. Example: "asha:x:1000:1000:Asha Rao:/home/asha:/bin/bash" gives {"name": "asha", "uid": 1000, "gid": 1000, "comment": "Asha Rao", "home": "/home/asha", "shell": "/bin/bash"}.
Write login_users(passwd_text) that returns the names of human users who can log in: UID 1000 or more AND a shell that does not end in "nologin" or "false". Keep the order of the file and skip empty lines.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Creating and managing users and passwordsPermissions, users and sudo · same module
- File permissions: rwx, chmod and chownPermissions, users and sudo · same module
- umask and the special bits: SUID, SGID and stickyPermissions, users and sudo · same module
- Linux security basicsCyber Security: from zero to SOC analyst · Systems and cryptography
- Users, groups and permissionsDevOps: Git, Docker, Kubernetes and CI/CD · Linux for DevOps
- Storing passwords safelyBackend Development · Authentication and authorization
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What is the UID of root?
Answer: 0
root is always UID 0.
Which file can only root read?
Answer: /etc/shadow
/etc/shadow holds the password hashes, so it is readable by root only.
What is the 7th field of an /etc/passwd line?
Answer: Login shell
The last field is the login shell.
asha was just added to the developers group, but id in her open session does not show it. Why?
Answer: Group membership is read at login; she must log in again
Log out and in (or use newgrp) to pick up new groups.
Which shell should a service account like apache have?
Answer: /sbin/nologin
/sbin/nologin refuses interactive logins.
You change PASS_MAX_DAYS in /etc/login.defs to 90. Who is affected?Related
Answer: Only users created after the change
login.defs gives defaults to new accounts; existing ones keep their values.
Which file holds Linux password hashes?Related
Answer: /etc/shadow
/etc/shadow holds the hashes and is readable only by root (and the shadow group).
Which command adds user ravi to the docker group without removing his other groups?Related
Answer: sudo usermod -aG docker ravi
-a appends. Without it, -G replaces all extra groups.
Why use visudo instead of vim /etc/sudoers?Related
Answer: It checks the syntax and locks the file
A broken sudoers file can lock out all admins; visudo prevents saving errors.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 5 more topics in “Permissions, users and sudo” before its exam: Creating and managing users and passwords, File permissions: rwx, chmod and chown, umask and the special bits: SUID, SGID and sticky, Access control lists (ACLs), sudo and the sudoers file.
- Module 2 exam: Permissions, users and sudo · 14 questions · 25 min, without help. 6 topics of this module still to finish first.
- Then the next module, Processes, services and logs. 57 topics left in the course before the final exam.