Chapter 4: Users and Groups

Foreword

We recommend that you do not use AI to do the exercises since you are in the learning phase.

Introduction

Linux is a true multi-user system! Several users can log in and run tasks at the same time. It also has a single-user mode managed by the kernel, used only for maintenance purposes. Users generally have:

Prerequisites (Repetition is educational 😜)

Info: If you don’t have a Linux environment available, you can register at https://killercoda.com and go here https://killercoda.com/playgrounds/scenario/ubuntu to get access to a virtual machine running Ubuntu 24.04 (without a graphical interface, of course!!) for 1 hour, renewable for free.

You’ll then see this view:




User Management

Intro

To test 👨🏾‍💻👩🏾‍💻:

Below is an example of the similar output you’ll get:


A bit of quick explanation.

Command: whoami

widal@j4rd1n-d3s-0mbr3s:~$ whoami
widal

Command: id

widal@j4rd1n-d3s-0mbr3s:~$ id
uid=1000(widal) gid=1000(widal) groupes=1000(widal),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),122(lpadmin),134(lxd),135(sambashare),136(vboxusers),141(libvirt),999(docker)

Command: who

widal@j4rd1n-d3s-0mbr3s:~$ who
widal    :1           2025-04-04 16:04 (:1)
widal    pts/0        2025-04-04 16:05 (j)
widal    pts/1        2025-04-04 16:05 (j)
widal    pts/2        2025-04-04 16:05 (j)

Command: w

widal@j4rd1n-d3s-0mbr3s:~$ w
 17:51:24 up 17 days,  1:47,  4 users,  load average: 1,46, 1,24, 1,20
UTIL.    TTY      DE               LOGIN@   IDLE   JCPU   PCPU QUOI
widal    :1       :1               04avril25 ?xdm?  21:01m  0.01s /usr/libexec/gdm-x-session ...
widal    pts/0    j                04avril25 17jours  0.00s  0.00s /bin/bash
widal    pts/1    j                04avril25  7jours  0.09s  0.09s /bin/bash
widal    pts/2    j                04avril25 13:53   0.02s  0.02s /bin/bash

What’s the /etc/passwd file?


Coffee break ☕
We said above that the password is most certainly salted. Yes indeed! the system puts a bit of salt on the password before storing it 🤣. In computer security, salting a password means adding a random value (the “salt”) to the password before encrypting it (or more precisely, hashing it). This prevents two users who chose the same password from having the same fingerprint stored in the system.


To test 👨🏾‍💻👩🏾‍💻:


🔁🃏 UNO Reverse !!!


Example:

djo:$y$j9T$3J4GSvuGv7bM4Vn4BRaOm1$3nuzvVPg0VJoidAVUKsMJvf2Hn3Q6.TbC0H5MnqA782:15051:0:99999:7:::

⚠️ It is not recommended to directly edit the /etc/passwd and /etc/shadow files, to avoid the risk of typos, which would make authentication impossible.

To test 👨🏾‍💻👩🏾‍💻:


User management commands

Points to note ⚠️

Here’s an example situation.

widal@j4rd1n-d3s-0mbr3s:~$ whoami
widal
widal@j4rd1n-d3s-0mbr3s:~$ useradd avrell
useradd: Permission denied.
useradd : impossible de verrouiller /etc/passwd ; réessayer plus tard.
widal@j4rd1n-d3s-0mbr3s:~$ sudo useradd avrell
[sudo] Mot de passe de widal : 
widal@j4rd1n-d3s-0mbr3s:~$ id avrell
uid=1002(avrell) gid=1002(avrell) groupes=1002(avrell)
widal@j4rd1n-d3s-0mbr3s:~$ 
widal@j4rd1n-d3s-0mbr3s:~$ echo "user avrell is there"
user avrell is there
widal@j4rd1n-d3s-0mbr3s:~$ 


1. useradd – Add a new user

sudo useradd username

Example:

sudo useradd -m alice

2. usermod – Modify an existing user

sudo usermod [options] username

Examples:

sudo usermod -aG sudo alice

This adds alice to the sudo group.

sudo usermod -d /new/path alice

This changes alice’s home directory.


3. passwd – Change a user’s password

sudo passwd username

This allows you to set or change a user’s password.

Example:

sudo passwd alice

This prompts you to enter a new password for alice.


4. userdel – Delete a user

sudo userdel username

Example:

sudo userdel alice

Also delete their directory:

sudo userdel -r alice

adduser vs useradd

Command Description
useradd Low-level command: simple, but requires more options.
adduser Interactive script: guides you step by step to create a user (password, info, home directory, shell, etc.).

Example:

sudo adduser bob

This starts a wizard to create a complete user.


​⚠️ EXTRA INFO:
It can happen that there’s a hiccup (usually when using useradd) and your user ends up without a home directory. You can fix this hiccup with the mkhomedir_helper command. (e.g. mkhomedir_helper myuserbob).



TLDR (Quick summary)

Command Role
useradd Create a user (simple)
adduser Create a user (assisted)
usermod Modify an existing user
passwd Change the password
userdel Delete a user
mkhomedir_helper Create a user’s home directory

Commands to switch users



Exercise ⚔️

Below is an example run:

# We download the challenge 1 script
curl -LO https://raw.githubusercontent.com/N0vachr0n0/NoFD/refs/heads/main/USER_EXO_1.sh

# We make it executable
chmod +x USER_EXO_1.sh

# We run it to start the challenge
./USER_EXO_1.sh




Group Management

Intro

To test 👨🏾‍💻👩🏾‍💻:


To test 👨🏾‍💻👩🏾‍💻:

The x you’ll see here again represents the group’s password. It’s located in /etc/gshadow, and just like for users in /etc/shadow:


The sudo or wheel group

Group management commands

⚠️ Don’t forget the points to note ⚠️

Managing the /etc/sudoers file

The /etc/sudoers file is a configuration file used on Linux/Unix systems to define user and group permissions regarding the use of the sudo command. It determines who can run commands as administrator (root) or another user, and which specific commands they can run. This file is crucial for security, since a misconfiguration could either block legitimate access or grant too many privileges.

The sudoers file should never be edited directly with a regular editor (like nano or vim), because a syntax error can lock you out of sudo. The recommended method is to use the visudo command, which checks the syntax before saving.

   sudo visudo

Below are a few example lines.

Line Meaning
jean ALL=(ALL) /usr/bin/apt, /usr/bin/systemctl The user jean is only allowed to run the /usr/bin/apt and /usr/bin/systemctl commands as root or any other user, from any host.
jean ALL=(ALL) NOPASSWD: ALL The user jean can run all commands as any user without having to enter their password.
jean ALL=(ALL) ALL The user jean can run all commands as any user, but will have to enter their password.
%admins ALL=(ALL) ALL All members of the admins group can run all commands as any user, with the obligation to enter their password. (Watch out for the %)

To finish in style, I invite you to do some research on:

sudo -l

Exercise ⚔️

Run the script to start the challenge like a boss 😉.



Feedback

Give us your feedback on this chapter.

👉🏾 Click here