Learn / Linux / Users & Permissions
Lesson 17 of 74

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.

25 min read Report a problem Hands-on lab included 6-question quiz

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-data user 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

Userswho you are (a UID)
rohanUID 1000
priyaUID 1506
member of
Groupsteams (a GID)
devopsrohan, priya
dockerrohan
get access to
Files and processesowned by a user and a group
/srv/deployowner rohan, group devops

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 rangeUsed for
0root: the superuser. The kernel gives UID 0 every privilege, whatever the name.
1 – 999system / service accounts, created by packages (www-data is 33 on Ubuntu)
1000 and upnormal users (people). On Red Hat systems also from 1000.
65534nobody: 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

bash
tail -n 3 /etc/passwd
text
anita: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
FieldExampleMeaning
1 · nameanitathe login name
2 · passwordxplaceholder: the real hash is in /etc/shadow
3 · UID1001user ID
4 · GID1003the primary group's ID
5 · comment (GECOS)Anita Desaifull name or description
6 · home/home/anitahome folder
7 · shell/bin/bashprogram 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:

bash
grep -E '^(root|rohan|temp-contractor):' /etc/shadow
text
root:*:20781:0:99999:7:::
rohan:!:20781:0:99999:7:::
temp-contractor:$y$j9T$THonCcKT2EZSLr.tZ/E04.$WxQRkODjj0.WC86jd5fNKrP8J1MzPLg3iyX1rlHBo9.:20781:0:99999:7:::
FieldExampleMeaning
1temp-contractoruser name
2$y$j9T$...the password hash. $y$ = yescrypt, $6$ = SHA-512. * or ! = no password login possible; a ! in front of a hash = locked.
320781date of the last change, in days since 1 January 1970
40minimum days before the password may be changed again
599999maximum days before it must be changed (99999 = never)
67days 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

text
docker:x:1001:rohan
devops:x:1507:rohan,priya
FieldMeaning
1group name
2password placeholder (rarely used; /etc/gshadow)
3GID
4supplementary 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: docker to use Docker, sudo (Ubuntu) or wheel (Red Hat) to use sudo, a team group for shared folders.
bash
id rohan
text
uid=1000(rohan) gid=1002(rohan) groups=1002(rohan),1000(qa),1001(docker)

Primary group rohan (gid), plus qa and docker.

Looking up users

CommandShows
whoamiyour user name
id [user]UID, GID and all groups
groups [user]group names only
getent passwd userthe passwd entry, also from LDAP/AD if the server uses a directory
getent group devopsa group and its members
who / wwho is logged in right now (w also shows what they're doing)
lastrecent logins (from /var/log/wtmp)
lastlogeach user's most recent login
Try it · Task 1 of 4

Who is on this server?

Find out which account uses the user ID 1505.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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?

bash
id
id rohan
groups rohan
CommandShows
idyour own UID, primary group (GID) and all groups
id rohanthe same, for another user
groups rohanonly the group names
text
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 docker
  • uid=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 groups qa and docker.

2. Read /etc/passwd

bash
tail -n 6 /etc/passwd
text
sshd: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/nologin

Seven fields separated by ::

#Fieldrohan's value
1user namerohan
2password placeholder (the real hash is in /etc/shadow)x
3UID1000
4primary group ID1002
5comment, usually the full name (also called GECOS)Rohan Mehta
6home folder/home/rohan
7login 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

bash
awk -F: '$3 == 1505 {print $1}' /etc/passwd
getent passwd anita
CommandDoes
awk -F: '$3 == 1505 {print $1}'prints the name of the line whose 3rd field (UID) is 1505
getent passwd anitalooks the user up the way the system does, including central directories like LDAP if configured
2 · Your task

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
text
awk -F: '$3 == 1505 {print $1}' /etc/passwd > /root/uid-1505.txt

What each part does

  • -F: /etc/passwd fields are colon-separated. $3 == 1505: the line whose UID (field 3) is 1505. {print $1}: print its user name.
First start the lab machine in the terminal panel on the right.

Creating users: useradd

text
useradd [options] NAME
OptionMeaning
-mcreate the home folder (copies /etc/skel)
-s /bin/bashlogin shell
-c "Full Name"comment
-G devops,dockersupplementary groups
-u 1600a specific UID
-d /srv/appa different home folder
-ra system account (UID below 1000, no ageing, no home unless -m)
-e 2026-12-31account expiry date
bash
useradd -m -s /bin/bash -c "Priya Sharma" priya
grep priya /etc/passwd
ls -ld /home/priya
text
priya:x:1506:1506:Priya Sharma:/home/priya:/bin/bash
drwxr-x--- 2 priya priya 4096 Nov 24 15:45 /home/priya

UID 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:

text
useradd -r -s /usr/sbin/nologin -d /opt/shop -c "Shop app" shop

System UID, no login, home set to the app's folder.

Passwords: passwd, chpasswd, chage

CommandDoes
passwdchange your own password
passwd priya(root) set priya's password
echo "priya:S3cret" | chpasswdset passwords non-interactively, for scripts
passwd -S priyastatus: P usable password, L locked, NP no password
passwd -e priyaexpire it: priya must choose a new one at next login
chage -l priyashow password ageing settings
chage -M 90 priyarequire a change every 90 days
text
$ passwd -S priya
priya P 2026-11-24 0 99999 7 -1

P (usable password), last changed 2026-11-24, minimum 0 days, maximum 99999 (never expires), 7 days warning, -1 no inactivity limit.

Try it · Task 2 of 4

Create a user

Create Priya's account with a home folder, bash, her full name and a password.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
useradd -m -s /bin/bash -c "Priya Sharma" priya
grep priya /etc/passwd
ls -ld /home/priya
ls -A /home/priya
OptionMeaning
-mmake the home folder (/home/priya), filled with default files from /etc/skel
-s /bin/bashthe login shell
-c "Priya Sharma"the comment field: her full name
priyathe user name
text
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
.profile
  • useradd picked UID 1506: the next free number after the highest normal UID in use (1505, the service account).
  • It also created a group priya with the same number.
  • The home folder belongs to priya, with the standard shell settings copied from /etc/skel.

2. Set a password

bash
passwd priya

What it does: asks for the new password twice (nothing is shown as you type). In scripts, set it without prompts instead:

text
echo "priya:Welcome@2026" | chpasswd

chpasswd reads user:password lines from its input.

3. See what was stored

bash
grep priya /etc/shadow | cut -c1-40
passwd -S priya
text
priya:$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 -S gives a summary: P = has a usable Password (L = locked, NP = no password), the date it was set, and the ageing rules.
2 · Your task

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
text
useradd -m -s /bin/bash -c "Priya Sharma" priya
echo "priya:Welcome@2026" | chpasswd

What 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.
First start the lab machine in the terminal panel on the right.

Changing users: usermod

OptionChanges
-aG groupadds supplementary group(s)
-G groupreplaces all supplementary groups (dangerous)
-g groupthe primary group
-s shelllogin shell
-c "text"comment
-d /new/home -mhome folder, moving its contents
-l newnamerename the account
-L / -Ulock / unlock the password
-e YYYY-MM-DDaccount expiry (-e 1 expires it immediately)

usermod -G devops rohan without -a. It removes Rohan from every other supplementary group:

text
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 devops

If 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

CommandDoes
groupadd devopscreate a group (-f: don't fail if it exists, -g 2000 set the GID)
gpasswd -a rohan devopsadd a member
gpasswd -d rohan devopsremove a member
groupdel devopsdelete a group
newgrp devopsin 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.

Try it · Task 3 of 4

Groups

Create the devops group and add Rohan and Priya without removing Rohan's other groups.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
groupadd devops
grep devops /etc/group
text
devops: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.)

bash
id rohan
usermod -G devops rohan
id rohan
text
uid=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

bash
usermod -aG qa,docker,devops rohan
id rohan
OptionMeaning
-aappend: add to the existing groups instead of replacing them
-G qa,docker,devopsthe supplementary group(s), comma-separated, no spaces
text
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.

2 · Your task

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
text
groupadd -f devops
usermod -aG qa,docker,devops rohan
usermod -aG devops priya

groupadd -f doesn't fail if the group already exists.

What each part does

  • groupadd -f devops: create the group. -f means don't fail if it already exists.
  • usermod -aG qa,docker,devops rohan: append rohan to these three groups, keeping his existing ones. Forgetting -a would remove him from all the others.
  • Check with id rohan.
First start the lab machine in the terminal panel on the right.

Locking and deleting accounts

CommandEffect
usermod -L userlock the password (! in front of the hash)
usermod -s /usr/sbin/nologin userrefuse interactive logins, including with SSH keys
chage -E 0 userexpire the account: no logins at all
userdel userdelete the account, keep the home folder
userdel -r userdelete 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:

text
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 home
Try it · Task 4 of 4

Lock and remove accounts

Lock the contractor's account and delete the old intern's account with its home folder.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

When someone leaves, there are two options:

OptionUse 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

bash
usermod -L temp-contractor
passwd -S temp-contractor
grep temp-contractor /etc/shadow | cut -c1-30
CommandDoes
usermod -LLock: puts a ! in front of the password hash, so no password can match it
passwd -Sshow the account status
text
temp-contractor L 2026-11-24 0 99999 7 -1
temp-contractor:!$y$j9T$THonCc

Status 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

bash
ls -la /home/old-intern
ps -u old-intern
find / -user old-intern -not -path "/proc/*" 2>/dev/null | head
CommandChecks
ls -la /home/old-internwhat's in the home folder (anything to keep?)
ps -u old-internrunning 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

bash
userdel -r old-intern
id old-intern
ls /home
OptionMeaning
-ralso remove the home folder and mail spool. Without it, /home/old-intern stays behind.
text
userdel: old-intern mail spool (/var/mail/old-intern) not found
id: 'old-intern': no such user
anita
priya
rohan
temp-contractor

The first line is just a notice: there was no mail spool to delete. The account and its home folder are gone.

2 · Your task
  1. Lock the account temp-contractor (don't delete it).
  2. Delete the account old-intern together 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
text
usermod -L temp-contractor
userdel -r old-intern

What 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. -U unlocks.
  • userdel -r old-intern: delete the user and (-r) their home folder and mail.
First start the lab machine in the terminal panel on the right.

Switching users: su

CommandGives you
su - anitaa login shell as anita: her home folder, PATH and settings. Root needs no password; others need anita's password.
su anitaa 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
text
$ 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

FileControls
/etc/skel/files copied into every new home folder (.bashrc, .profile)
/etc/login.defsUID/GID ranges, password ageing defaults, home folder permissions
/etc/default/useradddefault 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, then passwd/chpasswd. Service accounts: useradd -r -s /usr/sbin/nologin.
  • Change: usermod -aG (never -G alone), -s, -L/-U. Groups: groupadd, gpasswd -a/-d.
  • Remove: lock first, check processes and files, then userdel -r.
  • su - user for a login shell as another user; id, getent, who, last to inspect.
Check your understanding

Quick quiz

Feedback