Users and Groups
Why Linux has users, how UIDs and GIDs work, every field of /etc/passwd, /etc/shadow and /etc/group, primary and supplementary groups, service accounts, and the commands to create, change, lock and delete users and groups, including the usermod mistake that locks people out.
Why Linux has users
Linux was built as a multi-user system: many people and programs share one machine, safely. Every process runs as a user, and every file belongs to a user. That gives you:
- Isolation: Asha can't read Vikram's files or kill his processes.
- Least privilege: a web server running as the unprivileged
www-datauser can't damage the system even if it's hacked. - Accountability: logs record who did what.
On servers, most "users" aren't people at all. They're service accounts: www-data for nginx, postgres for the database, systemd-network for networking, each with only the access it needs.
Users, groups and files
Permissions (next lesson) decide what the owner, the group and everyone else may do with each file.
UIDs and GIDs: users are numbers
The kernel doesn't know names. It knows numbers: every user has a UID (user ID) and every group a GID (group ID). Names exist for humans, and are looked up in /etc/passwd and /etc/group.
| UID range | Used for |
|---|---|
0 | root: the superuser. The kernel gives UID 0 every privilege, whatever the name. |
1 – 999 | system / service accounts, created by packages (www-data is 33 on Ubuntu) |
1000 and up | normal users (people). On Red Hat systems also from 1000. |
65534 | nobody: an account with no privileges at all |
The ranges are set in /etc/login.defs (UID_MIN, SYS_UID_MAX). Because ownership is stored as a number, when you delete a user, their leftover files show a bare number as owner in ls -l. And if a new user later gets the same UID, they silently inherit those files.
/etc/passwd: the list of accounts
tail -n 3 /etc/passwdanita:x:1001:1003:Anita Desai:/home/anita:/bin/bash
temp-contractor:x:1003:1005:Contractor (Q4):/home/temp-contractor:/bin/bash
payments-svc:x:1505:996:Payments service:/opt/payments:/usr/sbin/nologin| Field | Example | Meaning |
|---|---|---|
| 1 · name | anita | the login name |
| 2 · password | x | placeholder: the real hash is in /etc/shadow |
| 3 · UID | 1001 | user ID |
| 4 · GID | 1003 | the primary group's ID |
| 5 · comment (GECOS) | Anita Desai | full name or description |
| 6 · home | /home/anita | home folder |
| 7 · shell | /bin/bash | program started at login. /usr/sbin/nologin or /bin/false = no interactive login. |
/etc/passwd must be readable by everyone (that's how ls -l turns UIDs into names), which is exactly why passwords were moved out of it.
/etc/shadow: password hashes
Only root can read it:
grep -E '^(root|rohan|temp-contractor):' /etc/shadowroot:*:20781:0:99999:7:::
rohan:!:20781:0:99999:7:::
temp-contractor:$y$j9T$THonCcKT2EZSLr.tZ/E04.$WxQRkODjj0.WC86jd5fNKrP8J1MzPLg3iyX1rlHBo9.:20781:0:99999:7:::| Field | Example | Meaning |
|---|---|---|
| 1 | temp-contractor | user name |
| 2 | $y$j9T$... | the password hash. $y$ = yescrypt, $6$ = SHA-512. * or ! = no password login possible; a ! in front of a hash = locked. |
| 3 | 20781 | date of the last change, in days since 1 January 1970 |
| 4 | 0 | minimum days before the password may be changed again |
| 5 | 99999 | maximum days before it must be changed (99999 = never) |
| 6 | 7 | days of warning before it expires |
| 7, 8 | (empty) | inactivity period, and account expiry date |
A hash is a one-way fingerprint: Linux hashes what you type at login and compares it with the stored hash. The password itself is never stored.
/etc/group: groups and members
docker:x:1001:rohan
devops:x:1507:rohan,priya| Field | Meaning |
|---|---|
| 1 | group name |
| 2 | password placeholder (rarely used; /etc/gshadow) |
| 3 | GID |
| 4 | supplementary members, comma-separated |
Primary and supplementary groups
Every user has:
- one primary group (the GID in
/etc/passwd). New files they create get this group. On Ubuntu and Red Hat, each user gets a private primary group with their own name. - any number of supplementary groups (listed in
/etc/group), which give extra access:dockerto use Docker,sudo(Ubuntu) orwheel(Red Hat) to use sudo, a team group for shared folders.
id rohanuid=1000(rohan) gid=1002(rohan) groups=1002(rohan),1000(qa),1001(docker)Primary group rohan (gid), plus qa and docker.
Looking up users
| Command | Shows |
|---|---|
whoami | your user name |
id [user] | UID, GID and all groups |
groups [user] | group names only |
getent passwd user | the passwd entry, also from LDAP/AD if the server uses a directory |
getent group devops | a group and its members |
who / w | who is logged in right now (w also shows what they're doing) |
last | recent logins (from /var/log/wtmp) |
lastlog | each user's most recent login |
Who is on this server?
Find out which account uses the user ID 1505.
To the kernel, a user is just a number, the UID (user ID). Names are for humans: /etc/passwd maps each name to its UID. Ownership of files and processes is stored as numbers, so when you see a number instead of a name in ls -l, it means that UID has no entry in /etc/passwd.
1. Who am I, and who is Rohan?
id
id rohan
groups rohan| Command | Shows |
|---|---|
id | your own UID, primary group (GID) and all groups |
id rohan | the same, for another user |
groups rohan | only the group names |
uid=0(root) gid=0(root) groups=0(root)
uid=1000(rohan) gid=1002(rohan) groups=1002(rohan),1000(qa),1001(docker)
rohan : rohan qa dockeruid=1000(rohan): Rohan's user ID is 1000.gid=1002(rohan): his primary group, also called rohan (Ubuntu creates a private group for each user).groups=...: he's also in the supplementary groupsqaanddocker.
2. Read /etc/passwd
tail -n 6 /etc/passwdsshd:x:100:65534::/run/sshd:/usr/sbin/nologin
rohan:x:1000:1002:Rohan Mehta:/home/rohan:/bin/bash
anita:x:1001:1003:Anita Desai:/home/anita:/bin/bash
old-intern:x:1002:1004:Summer intern 2025:/home/old-intern:/bin/bash
temp-contractor:x:1003:1005:Contractor (Q4):/home/temp-contractor:/bin/bash
payments-svc:x:1505:996:Payments service:/opt/payments:/usr/sbin/nologinSeven fields separated by ::
| # | Field | rohan's value |
|---|---|---|
| 1 | user name | rohan |
| 2 | password placeholder (the real hash is in /etc/shadow) | x |
| 3 | UID | 1000 |
| 4 | primary group ID | 1002 |
| 5 | comment, usually the full name (also called GECOS) | Rohan Mehta |
| 6 | home folder | /home/rohan |
| 7 | login shell | /bin/bash |
Accounts like sshd and payments-svc are service accounts: low or special UIDs and the shell /usr/sbin/nologin, so nobody can log in as them.
3. Find a user by UID
awk -F: '$3 == 1505 {print $1}' /etc/passwd
getent passwd anita| Command | Does |
|---|---|
awk -F: '$3 == 1505 {print $1}' | prints the name of the line whose 3rd field (UID) is 1505 |
getent passwd anita | looks the user up the way the system does, including central directories like LDAP if configured |
Find the name of the account with UID 1505 and save it to /root/uid-1505.txt.
Expected result: cat /root/uid-1505.txt prints the account name.
Hint: the commands, explained
awk -F: '$3 == 1505 {print $1}' /etc/passwd > /root/uid-1505.txtWhat each part does
-F:/etc/passwdfields are colon-separated.$3 == 1505: the line whose UID (field 3) is 1505.{print $1}: print its user name.
Creating users: useradd
useradd [options] NAME| Option | Meaning |
|---|---|
-m | create the home folder (copies /etc/skel) |
-s /bin/bash | login shell |
-c "Full Name" | comment |
-G devops,docker | supplementary groups |
-u 1600 | a specific UID |
-d /srv/app | a different home folder |
-r | a system account (UID below 1000, no ageing, no home unless -m) |
-e 2026-12-31 | account expiry date |
useradd -m -s /bin/bash -c "Priya Sharma" priya
grep priya /etc/passwd
ls -ld /home/priyapriya:x:1506:1506:Priya Sharma:/home/priya:/bin/bash
drwxr-x--- 2 priya priya 4096 Nov 24 15:45 /home/priyaUID 1506: useradd takes the next number after the highest normal UID already in use. A matching private group was created too.
On Debian and Ubuntu, adduser priya is a friendlier, interactive wrapper that creates the home folder, asks for a password and the full name. Scripts usually use useradd because it's the same on every distribution.
A typical service account for an application:
useradd -r -s /usr/sbin/nologin -d /opt/shop -c "Shop app" shopSystem UID, no login, home set to the app's folder.
Passwords: passwd, chpasswd, chage
| Command | Does |
|---|---|
passwd | change your own password |
passwd priya | (root) set priya's password |
echo "priya:S3cret" | chpasswd | set passwords non-interactively, for scripts |
passwd -S priya | status: P usable password, L locked, NP no password |
passwd -e priya | expire it: priya must choose a new one at next login |
chage -l priya | show password ageing settings |
chage -M 90 priya | require a change every 90 days |
$ passwd -S priya
priya P 2026-11-24 0 99999 7 -1P (usable password), last changed 2026-11-24, minimum 0 days, maximum 99999 (never expires), 7 days warning, -1 no inactivity limit.
Create a user
Create Priya's account with a home folder, bash, her full name and a password.
useradd creates the account; by itself it doesn't create a home folder or set a password on every distribution, so you tell it exactly what you want with options. The account can't log in with a password until you set one.
1. Create the account
useradd -m -s /bin/bash -c "Priya Sharma" priya
grep priya /etc/passwd
ls -ld /home/priya
ls -A /home/priya| Option | Meaning |
|---|---|
-m | make the home folder (/home/priya), filled with default files from /etc/skel |
-s /bin/bash | the login shell |
-c "Priya Sharma" | the comment field: her full name |
priya | the user name |
priya:x:1506:1506:Priya Sharma:/home/priya:/bin/bash
drwxr-x--- 2 priya priya 4096 Nov 24 15:45 /home/priya
.bash_logout
.bashrc
.profileuseraddpicked UID 1506: the next free number after the highest normal UID in use (1505, the service account).- It also created a group
priyawith the same number. - The home folder belongs to priya, with the standard shell settings copied from
/etc/skel.
2. Set a password
passwd priyaWhat it does: asks for the new password twice (nothing is shown as you type). In scripts, set it without prompts instead:
echo "priya:Welcome@2026" | chpasswdchpasswd reads user:password lines from its input.
3. See what was stored
grep priya /etc/shadow | cut -c1-40
passwd -S priyapriya:$y$j9T$sqJin5NXKGffymyNWmRwh.$Nib6
priya P 2026-11-24 0 99999 7 -1- In
/etc/shadow, the second field is the password hash, never the password itself.$y$means the yescrypt algorithm. A!or*there instead means "no password login". passwd -Sgives a summary:P= has a usable Password (L= locked,NP= no password), the date it was set, and the ageing rules.
Create the user priya with a home folder, the shell /bin/bash, the comment Priya Sharma, and set a password for her (any password you like).
Expected result: getent passwd priya shows her home /home/priya and shell /bin/bash, and passwd -S priya shows P.
Hint: the commands, explained
useradd -m -s /bin/bash -c "Priya Sharma" priya
echo "priya:Welcome@2026" | chpasswdWhat each part does
useradd -m: create the user and their home folder/home/priya.-s /bin/bash: their login shell.-c "Priya Sharma": the full name (comment field).echo "user:password" | chpasswd: set the password without an interactive prompt, which is useful in scripts.
Changing users: usermod
| Option | Changes |
|---|---|
-aG group | adds supplementary group(s) |
-G group | replaces all supplementary groups (dangerous) |
-g group | the primary group |
-s shell | login shell |
-c "text" | comment |
-d /new/home -m | home folder, moving its contents |
-l newname | rename the account |
-L / -U | lock / unlock the password |
-e YYYY-MM-DD | account expiry (-e 1 expires it immediately) |
usermod -G devops rohan without -a. It removes Rohan from every other supplementary group:
uid=1000(rohan) gid=1002(rohan) groups=1002(rohan),1000(qa),1001(docker) # before
uid=1000(rohan) gid=1002(rohan) groups=1002(rohan),1507(devops) # after usermod -G devopsIf one of those groups was sudo, Rohan just lost admin rights, and if it was yours, you may have locked yourself out. Always type -aG, or use gpasswd -a rohan devops, which can only add.
Groups: groupadd, gpasswd, groupdel
| Command | Does |
|---|---|
groupadd devops | create a group (-f: don't fail if it exists, -g 2000 set the GID) |
gpasswd -a rohan devops | add a member |
gpasswd -d rohan devops | remove a member |
groupdel devops | delete a group |
newgrp devops | in your current shell, switch your active group (picks up new membership without logging out) |
Group changes apply at the next login: existing sessions keep the groups they started with.
Groups
Create the devops group and add Rohan and Priya without removing Rohan's other groups.
Groups let you give access to a team instead of to each person: put people in a group, give the group access, and adding or removing someone is a one-line change. The command to add a user to a group is usermod -aG GROUP USER, and the -a is critical: without it, usermod -G replaces all of the user's supplementary groups.
1. Create the group
groupadd devops
grep devops /etc/groupdevops:x:1507:/etc/group fields: name, password placeholder, GID, and the members (empty for now).
2. The classic mistake
Watch what happens if you forget -a. (Run this to see it, you'll fix it straight after.)
id rohan
usermod -G devops rohan
id rohanuid=1000(rohan) gid=1002(rohan) groups=1002(rohan),1000(qa),1001(docker)
uid=1000(rohan) gid=1002(rohan) groups=1002(rohan),1507(devops)Rohan is in devops now, but he lost qa and docker. On a real server that could mean he suddenly can't run Docker or reach the QA files. It's one of the most common admin mistakes.
3. Do it properly: -aG
usermod -aG qa,docker,devops rohan
id rohan| Option | Meaning |
|---|---|
-a | append: add to the existing groups instead of replacing them |
-G qa,docker,devops | the supplementary group(s), comma-separated, no spaces |
uid=1000(rohan) gid=1002(rohan) groups=1002(rohan),1000(qa),1001(docker),1507(devops)All three groups. gpasswd -a rohan devops is another safe way to add one group, and gpasswd -d rohan devops removes him from one.
Group changes apply to new logins. A user who is already logged in keeps the old group list until they log out and back in (or run newgrp devops in their shell). "I added him to the docker group but it still says permission denied" is usually this.
Make sure the group devops exists, and that both rohan and priya are members of it. Rohan must still be in qa and docker.
Expected result: id rohan shows qa, docker and devops; id priya shows devops.
Hint: the commands, explained
groupadd -f devops
usermod -aG qa,docker,devops rohan
usermod -aG devops priyagroupadd -f doesn't fail if the group already exists.
What each part does
groupadd -f devops: create the group.-fmeans don't fail if it already exists.usermod -aG qa,docker,devops rohan: append rohan to these three groups, keeping his existing ones. Forgetting-awould remove him from all the others.- Check with
id rohan.
Locking and deleting accounts
| Command | Effect |
|---|---|
usermod -L user | lock the password (! in front of the hash) |
usermod -s /usr/sbin/nologin user | refuse interactive logins, including with SSH keys |
chage -E 0 user | expire the account: no logins at all |
userdel user | delete the account, keep the home folder |
userdel -r user | delete the account and its home folder and mail |
Locking the password alone does not stop SSH key logins. To fully cut someone off, also expire the account or change the shell, and remove their keys from ~/.ssh/authorized_keys.
Before deleting, check what they leave behind:
ps -u old-intern # running processes
crontab -u old-intern -l # scheduled jobs
find / -user old-intern -not -path "/proc/*" 2>/dev/null # files outside their homeLock and remove accounts
Lock the contractor's account and delete the old intern's account with its home folder.
When someone leaves, there are two options:
| Option | Use when |
|---|---|
Lock the account (usermod -L) | they might come back, or you need to keep their files and history for now. Nothing is deleted. |
Delete it (userdel -r) | they're gone for good and their files aren't needed. Removes the account and its home folder. |
Many companies lock first and delete after a waiting period, after checking for files and running processes the user owns.
1. Lock the contractor
usermod -L temp-contractor
passwd -S temp-contractor
grep temp-contractor /etc/shadow | cut -c1-30| Command | Does |
|---|---|
usermod -L | Lock: puts a ! in front of the password hash, so no password can match it |
passwd -S | show the account status |
temp-contractor L 2026-11-24 0 99999 7 -1
temp-contractor:!$y$j9T$THonCcStatus L: locked. The hash now starts with !. usermod -U would unlock it. Note: locking the password doesn't stop SSH key logins. To fully block someone, also set their shell to /usr/sbin/nologin (usermod -s) or expire the account (usermod -e 1, or chage -E 0).
2. Check before deleting
ls -la /home/old-intern
ps -u old-intern
find / -user old-intern -not -path "/proc/*" 2>/dev/null | head| Command | Checks |
|---|---|
ls -la /home/old-intern | what's in the home folder (anything to keep?) |
ps -u old-intern | running processes (they'd keep running without an owner) |
find / -user old-intern ... | files they own outside their home folder |
3. Delete the intern's account
userdel -r old-intern
id old-intern
ls /home| Option | Meaning |
|---|---|
-r | also remove the home folder and mail spool. Without it, /home/old-intern stays behind. |
userdel: old-intern mail spool (/var/mail/old-intern) not found
id: 'old-intern': no such user
anita
priya
rohan
temp-contractorThe first line is just a notice: there was no mail spool to delete. The account and its home folder are gone.
- Lock the account
temp-contractor(don't delete it). - Delete the account
old-interntogether with its home folder.
Expected result: passwd -S temp-contractor shows L, id old-intern says "no such user", and /home/old-intern no longer exists.
Hint: the commands, explained
usermod -L temp-contractor
userdel -r old-internWhat each part does
usermod -L: Lock the account (it adds!in front of the password hash in/etc/shadow). Files stay; login with the password is blocked.-Uunlocks.userdel -r old-intern: delete the user and (-r) their home folder and mail.
Switching users: su
| Command | Gives you |
|---|---|
su - anita | a login shell as anita: her home folder, PATH and settings. Root needs no password; others need anita's password. |
su anita | a shell as anita, but keeping much of your current environment (confusing; prefer su -) |
su - | a login shell as root (needs root's password, which is often disabled; use sudo -i instead) |
su - anita -c 'whoami' | run one command as anita |
$ su - anita -c 'whoami; pwd; id'
anita
/home/anita
uid=1001(anita) gid=1003(anita) groups=1003(anita)
$ usermod -s /usr/sbin/nologin anita; su - anita
This account is currently not available.With the nologin shell, even root can't get an interactive shell as anita this way.
sudo (its own lesson) is the modern, safer way to run commands with other users' privileges.
Where new accounts get their defaults
| File | Controls |
|---|---|
/etc/skel/ | files copied into every new home folder (.bashrc, .profile) |
/etc/login.defs | UID/GID ranges, password ageing defaults, home folder permissions |
/etc/default/useradd | default shell and home base for useradd |
Put a standard .bashrc or company notice in /etc/skel, and every new user gets it.
"What's in /etc/passwd and /etc/shadow?" passwd: name, x, UID, GID, comment, home, shell for every account, readable by all. shadow: password hashes and ageing, readable only by root.
"How do you add an existing user to a group?" usermod -aG group user (with -a!), effective at their next login.
"What is UID 0?" root. Any account with UID 0 has full privileges, so a second UID 0 account is a security red flag (awk -F: '$3==0' /etc/passwd).
"How do you disable a user without deleting them?" usermod -L plus usermod -s /usr/sbin/nologin or chage -E 0, and remove their SSH keys.
Summary
- Every process and file belongs to a user; the kernel uses numbers (UID, GID). UID 0 is root; 1–999 are service accounts; people start at 1000.
/etc/passwd(7 fields),/etc/shadow(hashes and ageing, root only),/etc/group(members).- One primary group, any number of supplementary groups; changes apply at next login.
- Create:
useradd -m -s /bin/bash -c "Name" user, thenpasswd/chpasswd. Service accounts:useradd -r -s /usr/sbin/nologin. - Change:
usermod -aG(never-Galone),-s,-L/-U. Groups:groupadd,gpasswd -a/-d. - Remove: lock first, check processes and files, then
userdel -r. su - userfor a login shell as another user;id,getent,who,lastto inspect.
Quick quiz
You've practised everything in this lesson
Every task was checked on your own machine. You can end the lab, or keep experimenting in the terminal.