Skip to main content

Users and groups


Table of contents

  1. The concept of Linux users
  2. Root vs standard user
  3. Information commands
  4. Creating and managing users
  5. Managing groups
  6. The /etc/passwd and /etc/group files


1 - The concept of Linux users

Linux is a multi-user system. Each user has:

  • A unique identifier (UID)
  • A personal directory
  • Specific permissions

Types of users

TypeUIDUsage
root0Super-administrator
System1-999Services and daemons
Humans1000+Real users

Why several users?

ReasonExample
SecurityIsolate access
AuditTrace actions
PermissionsControl who does what
ServicesOne user per service
info

Each service (nginx, mysql, etc.) generally has its own user to limit the damage in case of compromise.

🔝 Back to table of contents



2 - Root vs standard user

The root user

CharacteristicRootStandard user
UID01000+
Home/root/home/username
Prompt#$
PowerUnlimitedLimited

The dangers of root

danger

Working as root is dangerous!

# Une erreur de frappe peut être catastrophique
rm -rf / home/user # Espace en trop = système détruit !

Best practices

DoAvoid
Use sudo occasionallyLogging in as root
Create dedicated usersDoing everything as root
Limit privilegesSharing the root password

🔝 Back to table of contents



3 - Information commands

whoami - Who am I?

$ whoami
john

id - Detailed information

$ id
uid=1000(john) gid=1000(john) groups=1000(john),27(sudo),docker(999)

$ id john
uid=1000(john) gid=1000(john) groups=1000(john),27(sudo)

$ id -u # Juste l'UID
1000

$ id -g # Juste le GID principal
1000

$ id -G # Tous les GIDs
1000 27 999

who - Connected users

$ who
john pts/0 2024-01-15 10:30 (192.168.1.100)
alice pts/1 2024-01-15 11:00 (192.168.1.101)

$ who -a # Informations détaillées

w - User activity

$ w
10:45:00 up 5 days, 2:30, 2 users, load average: 0.15, 0.10, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
john pts/0 192.168.1.100 10:30 0.00s 0.05s 0.00s w
alice pts/1 192.168.1.101 11:00 5:00 0.02s 0.02s vim file.txt

last - Login history

$ last
john pts/0 192.168.1.100 Mon Jan 15 10:30 still logged in
alice pts/1 192.168.1.101 Mon Jan 15 11:00 still logged in
john pts/0 192.168.1.100 Sun Jan 14 09:00 - 18:00 (09:00)

$ last -n 5 # 5 dernières connexions

🔝 Back to table of contents



4 - Creating and managing users

useradd - Create a user

# Création basique
sudo useradd alice

# Création complète (recommandé)
sudo useradd -m -s /bin/bash -c "Alice Martin" alice
OptionEffect
-mCreate the home directory
-sLogin shell
-cComment (full name)
-gPrimary group
-GAdditional groups
-dCustom home path
-eExpiration date

adduser - Interactive alternative (Debian/Ubuntu)

sudo adduser bob
# Assistant interactif qui demande :
# - Mot de passe
# - Nom complet
# - Autres informations
tip

adduser is friendlier than useradd on Debian/Ubuntu. It automatically creates the home and asks for the password.

passwd - Set the password

# Définir le mot de passe d'un utilisateur
sudo passwd alice

# Changer son propre mot de passe
passwd

# Verrouiller un compte
sudo passwd -l alice

# Déverrouiller un compte
sudo passwd -u alice

# Forcer le changement à la prochaine connexion
sudo passwd -e alice

usermod - Modify a user

# Changer le shell
sudo usermod -s /bin/zsh alice

# Ajouter à un groupe (sans retirer des autres)
sudo usermod -aG docker alice

# Changer le home
sudo usermod -d /home/new_alice -m alice

# Renommer
sudo usermod -l alice_new alice
warning

The -aG (append Groups) option is crucial! Without the a, the user is removed from all their existing groups.

userdel - Delete a user

# Supprimer l'utilisateur (garde le home)
sudo userdel alice

# Supprimer l'utilisateur ET son home
sudo userdel -r alice

🔝 Back to table of contents



5 - Managing groups

The concept of groups

groupadd - Create a group

# Créer un groupe
sudo groupadd developers

# Créer avec un GID spécifique
sudo groupadd -g 2000 devops

groupmod - Modify a group

# Renommer un groupe
sudo groupmod -n dev developers

# Changer le GID
sudo groupmod -g 2001 devops

groupdel - Delete a group

sudo groupdel developers

gpasswd - Manage members

# Ajouter un utilisateur au groupe
sudo gpasswd -a alice developers

# Retirer un utilisateur du groupe
sudo gpasswd -d alice developers

# Définir les administrateurs du groupe
sudo gpasswd -A alice developers

groups - View a user's groups

$ groups
john sudo docker

$ groups alice
alice : alice developers docker

🔝 Back to table of contents



6 - The /etc/passwd and /etc/group files

/etc/passwd - The user database

$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
john:x:1000:1000:John Doe:/home/john:/bin/bash

Structure:

john:x:1000:1000:John Doe:/home/john:/bin/bash
│ │ │ │ │ │ │
│ │ │ │ │ │ └── Shell
│ │ │ │ │ └── Home
│ │ │ │ └── Commentaire (GECOS)
│ │ │ └── GID principal
│ │ └── UID
│ └── Mot de passe (x = dans /etc/shadow)
└── Nom d'utilisateur

/etc/shadow - Passwords (encrypted)

$ sudo cat /etc/shadow
john:$6$xyz...:19000:0:99999:7:::
FieldMeaning
1Username
2Encrypted password
3Last modification
4Minimum age
5Maximum age
6Warning period
7Inactivity period
8Expiration date

/etc/group - The group database

$ cat /etc/group
sudo:x:27:john,alice
docker:x:999:john,bob
developers:x:1001:alice,bob,carol

Structure:

developers:x:1001:alice,bob,carol
│ │ │ │
│ │ │ └── Membres
│ │ └── GID
│ └── Mot de passe (rarement utilisé)
└── Nom du groupe

Useful commands

# Voir un utilisateur spécifique
grep john /etc/passwd

# Compter les utilisateurs humains (UID >= 1000)
awk -F: '$3 >= 1000 {print $1}' /etc/passwd

# Voir les groupes d'un utilisateur
grep john /etc/group

🔝 Back to table of contents



Key takeaways

  • Linux distinguishes root (UID 0), system (1-999), and humans (1000+)
  • Never work as root directly → use sudo
  • useradd -m -s /bin/bash or adduser to create
  • usermod -aG group user to add to groups (watch out for the -a!)
  • /etc/passwd = user info, /etc/shadow = passwords
  • A user has a primary group and can have secondary groups

🔝 Back to table of contents


Next chapter: Permissions and access rights →