Chapter 6

Users & Groups

Linux is a multi-user system. Many people (and services) can use the same machine safely. In this chapter you'll learn about user types, how to manage users and groups, and how to use sudo.

What are users and groups?

Linux users and groups

Every file and every running program in Linux belongs to a user. Users can be organized into groups so they can share access to the same files and folders.

Types of users

Linux has three types of user accounts:

TypeUIDDescription
root0The administrator. Has full power over the system. Don't use it for daily work.
System users1–999Used by services like web servers (www-data). They usually cannot log in.
Regular users1000+Normal people. Each gets a home directory and can log in.

How to check: Run id to see your own UID, GID, and groups. Run id alice to check another user.

Managing users

You need sudo for all of these because they change the system. Here are the most common user management commands:

What you want to doCommand
Create a new usersudo adduser alice
Set or change passwordsudo passwd alice
Add user to a groupsudo usermod -aG developers alice
Lock account (disable login)sudo usermod -L alice
Unlock accountsudo usermod -U alice
Delete user and their home foldersudo deluser --remove-home alice

Managing groups

Groups let you share access to files and folders. For example, everyone in the developers group can access the same project folder.

What you want to doCommand
Create a groupsudo addgroup developers
See which groups a user is ingroups alice
See members of a groupgetent group developers
Add a user to a groupsudo usermod -aG developers alice
Remove a user from a groupsudo gpasswd -d alice developers
Rename a groupsudo groupmod -n newname developers
Delete a groupsudo delgroup developers

Using sudo and switching users

sudo lets you run one command as root. This is safer than being root all the time because you only use admin power when you actually need it.

CommandWhat it does
sudo apt updateRun a single command as root.
sudo -iOpen a root shell (for running many commands as root). Type exit to leave.
su - aliceSwitch to user alice (you need their password). Type exit to go back.
su -Switch to root (you need the root password).

Safety tip: Never run random sudo commands from the internet without understanding them first. One wrong command as root can break your system.

Try it yourself (practical demo)

Open a terminal on your Linux machine (or WSL / VM) and follow these steps:

  1. Run whoami and id to see your current user.
  2. Create a new user: sudo adduser demouser (set a password when asked).
  3. Create a new group: sudo addgroup demogroup.
  4. Add the user to the group: sudo usermod -aG demogroup demouser.
  5. Verify it worked: groups demouser.
  6. Switch to the new user: su - demouser, then type exit to come back.
  7. Clean up: sudo deluser --remove-home demouser and sudo delgroup demogroup.
Terminal
whoami
id
sudo adduser demouser
sudo addgroup demogroup
sudo usermod -aG demogroup demouser
groups demouser
su - demouser
exit
sudo deluser --remove-home demouser
sudo delgroup demogroup

Useful commands (quick reference)

You will use these commands often when working with users and groups:

CommandWhat it does
whoamiPrint your current username.
idShow your UID, GID, and all groups.
groupsList groups you belong to.
getent passwdList all users on the system.
getent groupList all groups on the system.

Practice questions

Question 1

Run whoami and id in your terminal. Write down your username, UID, and at least two groups you belong to.

Hint: Look for uid=, gid=, and groups= in the output of id.

Question 2

Why is it better to use sudo for specific commands instead of logging in as root all the time?

Hint: Think about accidental mistakes, security, and keeping track of who did what.

Quiz

Test your understanding. Click an answer to see if it's correct.

1. What does the command whoami do?

  • Creates a new user.
  • Shows your current username.
  • Shows all users on the system.
  • Changes your password.

It simply prints the name of the current logged-in user.

2. Which user has full control over a standard Linux system?

  • guest
  • admin
  • root
  • any user

root is the superuser with permission to do anything on the system.

3. What is the purpose of the sudo command?

  • To permanently change your username.
  • To run a command with temporary elevated (root) privileges.
  • To reset the system.
  • To open the software store.

Use sudo when you need admin rights for a specific command.

4. Which command adds user alex to group developers?

  • sudo adduser alex developers
  • sudo useradd alex developers
  • sudo usermod -aG developers alex
  • sudo groupmod alex developers

usermod -aG appends a group without removing existing ones.