03 — Users & Groups¶
Navigation: ← 02 — File System | Notes Index | Next → 04 — File Permissions Exercise: Exercise 03
Why Does Linux Have Users?¶
Linux was designed from the start to be used by many people at the same time (it's a multi-user system). Each person gets their own account with their own files, settings, and level of access. Even if only one person uses the machine, the user system still matters — it controls which programs can access which files.
Every file, folder, and running program in Linux is "owned" by a user. Ownership determines who can read, modify, or run it.
The Root User (Administrator)¶
There is a special user called root. Root has unrestricted access to everything on the system — it can read any file, delete anything, install software, and change any setting.
Think of it like this: regular users are employees in a building; root is the building manager who has a master key to every room.
You will see the prompt change when you are root:
Because root is so powerful, you should only use it when necessary. The sudo command lets you run a single command as root without fully becoming root.
User IDs (UID)¶
Internally, Linux identifies users by a number called the UID (User ID), not by name.
| UID | Who it is |
|---|---|
| 0 | root (the administrator) |
| 1–999 | System accounts (for software like nginx, mysql) — not real people |
| 1000+ | Regular human user accounts |
When you create a new user, they automatically get the next available UID starting from 1000.
Where User Information is Stored¶
Linux stores user information in plain text files:
| File | What it contains |
|---|---|
/etc/passwd |
Basic info: username, UID, home folder, shell |
/etc/shadow |
Encrypted passwords (only root can read this) |
/etc/group |
Group definitions and members |
Reading /etc/passwd¶
Run cat /etc/passwd and you'll see lines like this:
Each field is separated by :. Here's what each part means:
jahid ← username
x ← password placeholder (actual password is in /etc/shadow)
1000 ← UID (User ID)
1000 ← GID (primary Group ID)
Jahid Khan ← full name / comment
/home/jahid ← home directory
/bin/bash ← shell this user uses
Checking Who You Are¶
Prints your current username. Simple and useful.
Prints more detail — your UID, your primary group, and all groups you belong to.
Example output:
Reading this: you are user jahid (UID 1000), your primary group is jahid, and you are also a member of adm, sudo, and developers.
Managing Users¶
All the commands below require
sudobecause creating and deleting users is an administrator task.
Create a new user¶
On Ubuntu/Debian, adduser is the friendliest way — it asks you questions:
It will ask for a password, full name, and a few other things. It also creates the home directory /home/alice automatically.
The lower-level command useradd does the same thing but silently, using flags:
-m— create a home directory (/home/alice)-s /bin/bash— set the shell to bash-c "Alice Smith"— add a comment (the user's full name)alice— the username
After useradd, you must set the password separately:
It will prompt you to type and confirm a new password.
View information about a user¶
Shows alice's UID, GID, and group memberships.
Modify an existing user¶
usermod changes an existing user. Here, -s changes alice's shell to zsh.
Lock an account (the user can no longer log in):
-L stands for "Lock". The account still exists but the password is disabled.
Unlock it again:
-U stands for "Unlock".
Delete a user¶
This removes the user account but keeps the home directory (/home/alice) on disk.
The -r flag also removes the home directory and mail spool. Use this when you want a clean removal.
Groups¶
A group is a collection of users. Groups let you give the same permissions to multiple people at once.
Example: You have a folder of project files. Instead of setting permissions for each person individually, you create a group called developers, add everyone to it, and set the folder's group to developers.
Every user has: - One primary group (set when the user is created, usually has the same name as the username) - Zero or more supplementary groups (extra groups for additional access)
View your groups¶
Example output:
These are all the groups you belong to.
Create a group¶
Add a user to a group¶
-ameans "append" (add to, don't replace existing groups)-Gspecifies the group
Important: Always include
-awhen adding to a group. If you forget it and just use-G, it will replace all of alice's existing groups with onlydevelopers, removing her from everything else.
The change takes effect the next time alice logs in.
Remove a user from a group¶
gpasswd -d removes alice from the developers group.
See who is in a group¶
Example output:
Delete a group¶
Switching Between Users¶
su — Switch User¶
This switches your session to alice's account. You will be prompted for alice's password.
The - (dash) is important — it means "simulate a full login". Without it, you switch to alice's account but keep your current environment (wrong home directory, wrong settings). Always use su -.
Switch to root:
You'll need the root password.
sudo — Run One Command as Root¶
sudo is the preferred way to do admin tasks. Instead of switching to root entirely, you run a single command with root privileges, then return to your normal user:
You'll be asked for your own password (not root's). After that, sudo remembers you for a few minutes so you don't have to keep typing it.
Good habit: Use
sudofor individual commands rather than switching to root withsu. This way, you cannot accidentally run something destructive while forgetting you're root.
The sudoers file — Who can use sudo?¶
The file /etc/sudoers controls who is allowed to use sudo. Never edit it directly — use visudo instead:
visudo checks for syntax errors before saving. A mistake in /etc/sudoers can lock you out of sudo entirely.
To give a user full sudo access, add a line like this:
Reading this: alice can run any command (ALL) on any host (ALL) as any user ((ALL:ALL)).
On Ubuntu, adding a user to the sudo group is simpler and has the same effect:
Password Policies¶
You can control how often users must change their password using chage (change age).
The -l flag lists alice's current password policy.
Example output:
Last password change : Jun 01, 2024
Password expires : never
Minimum number of days between changes : 0
Maximum number of days between changes : 99999
Set the password to expire every 90 days:
-M sets the maximum number of days a password is valid.
Force alice to change her password the next time she logs in:
Setting the "last change date" to 0 (January 1, 1970) makes the password immediately expired, so alice must change it on her next login.
The /etc/skel Directory¶
When you create a new user, Linux automatically populates their home directory with some starter files. These come from /etc/skel/:
Example output:
These are default shell configuration files. If you want every new user to start with a custom .bashrc or a welcome document, put those files in /etc/skel/ and they will be copied automatically when new users are created.
Navigation: ← 02 — File System | Notes Index | Next → 04 — File Permissions Exercise: Exercise 03