Skip to main content

sudo and privileges


Table of contents

  1. Why sudo?
  2. The /etc/sudoers configuration
  3. su vs sudo
  4. Security best practices
  5. Hands-on exercises


1 - Why sudo?

sudo = Super User DO

sudo lets you run a command with the privileges of another user (root by default).

The problem without sudo

The solution with sudo

Advantages of sudo

AdvantageExplanation
AuditEvery sudo command is logged
GranularitySpecific rights per user
SecurityNo permanent root session
PasswordUses the user's password
TimeoutAsks for the password again after 15 min

Basic usage

# Exécuter une commande en tant que root
sudo apt update

# Exécuter en tant qu'un autre utilisateur
sudo -u postgres psql

# Ouvrir un shell root (déconseillé pour usage prolongé)
sudo -i
# ou
sudo su -

🔝 Back to table of contents



2 - The /etc/sudoers configuration

File structure

danger

NEVER edit /etc/sudoers directly! Always use visudo, which checks the syntax.

sudo visudo

Rule format

utilisateur  machine=(utilisateur:groupe)  commandes

Configuration examples

# Permettre à john d'exécuter toutes les commandes
john ALL=(ALL:ALL) ALL

# Permettre sans mot de passe
john ALL=(ALL:ALL) NOPASSWD: ALL

# Permettre seulement certaines commandes
alice ALL=(ALL) /usr/bin/apt, /usr/bin/systemctl

# Permettre à un groupe (préfixe %)
%developers ALL=(ALL) /usr/bin/docker, /usr/bin/docker-compose

# Interdire certaines commandes
bob ALL=(ALL) ALL, !/bin/rm, !/bin/su

The sudoers.d files

Rather than modifying the main file, create files in /etc/sudoers.d/:

# Créer une règle personnalisée
sudo visudo -f /etc/sudoers.d/developers

# Contenu
%developers ALL=(ALL) NOPASSWD: /usr/bin/docker, /usr/bin/systemctl restart nginx

Verify the configuration

# Vérifier les droits sudo d'un utilisateur
sudo -l
# ou pour un autre utilisateur
sudo -l -U alice

Aliases in sudoers

# Définir des alias
User_Alias ADMINS = john, alice, bob
Host_Alias SERVERS = server1, server2
Cmnd_Alias NETWORKING = /sbin/ifconfig, /sbin/route

# Utiliser les alias
ADMINS ALL = NETWORKING

🔝 Back to table of contents



3 - su vs sudo

su - Switch user

su = Substitute User

# Devenir root (demande le mot de passe ROOT)
su

# Devenir root avec environnement complet
su -

# Devenir un autre utilisateur
su alice
su - alice # Avec son environnement

Comparison

Aspectsusudo
PasswordOf the target userOf the current user
AuditLimitedComplete (logs)
GranularityAll or nothingSpecific commands
SessionOpens a shellSingle command
SecurityRequires sharing the root passwordNo sharing

When to use what?

SituationRecommendation
Quick command as rootsudo command
Extended maintenancesudo -i then exit
Become an application usersudo -u postgres bash
Test as another usersu - username

sudo with environment preservation

# Préserver l'environnement (variables)
sudo -E commande

# Préserver le PATH
sudo env "PATH=$PATH" commande

# Éditer un fichier avec nano
sudo -e /etc/hosts
# ou
sudoedit /etc/hosts

🔝 Back to table of contents



4 - Security best practices

The principle of least privilege

Do

PracticeExample
Limit the commandsalice ALL=(ALL) /usr/bin/systemctl restart nginx
Use groups%webadmins ALL=(ALL) /usr/bin/nginx
Log the actionsCheck /var/log/auth.log
Short timeoutDefaults timestamp_timeout=5

Avoid

Bad practiceRisk
NOPASSWD: ALLEscalation without authentication
Logging in as rootPossible critical errors
Sharing the root passwordNo traceability
Permanent sudo su -Risky root environment

Secure configuration

# Dans /etc/sudoers (via visudo)

# Timeout de 5 minutes
Defaults timestamp_timeout=5

# Toujours demander le mot de passe
Defaults !authenticate

# Protéger le PATH
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Logger les entrées/sorties (avancé)
Defaults log_input, log_output
Defaults!/usr/bin/sudoreplay !log_input, !log_output

Auditing sudo actions

# Voir les logs sudo
sudo grep sudo /var/log/auth.log

# Ou avec journalctl
sudo journalctl -u sudo

# Format typique d'un log
# Jan 15 10:30:00 server sudo: john : TTY=pts/0 ; PWD=/home/john ; USER=root ; COMMAND=/usr/bin/apt update
warning

Every sudo command is traced! In a company, administrators can see who did what.

🔝 Back to table of contents



5 - Hands-on exercises

Exercise 1: Basic usage

# 1. Vérifiez vos droits sudo
sudo -l

# 2. Exécutez une commande simple
sudo whoami
# Résultat attendu : root

# 3. Mettez à jour le système
sudo apt update

Exercise 2: sudoers configuration

# 1. Créez un utilisateur de test
sudo useradd -m testadmin

# 2. Définissez son mot de passe
sudo passwd testadmin

# 3. Créez une règle sudo personnalisée
sudo visudo -f /etc/sudoers.d/testadmin

# Ajoutez cette ligne :
# testadmin ALL=(ALL) /usr/bin/apt update, /usr/bin/apt upgrade

# 4. Testez (dans un autre terminal ou avec su)
su - testadmin
sudo apt update # Doit fonctionner
sudo apt install vim # Doit échouer

# 5. Nettoyez
exit
sudo userdel -r testadmin
sudo rm /etc/sudoers.d/testadmin

Exercise 3: Audit

# 1. Exécutez quelques commandes sudo
sudo ls /root
sudo cat /etc/shadow

# 2. Vérifiez les logs
sudo grep "$(whoami)" /var/log/auth.log | tail -5

Quiz

Q1. Which command edits sudoers securely?

Answer

visudo or sudo visudo

Q2. Where are the sudo logs stored?

Answer

/var/log/auth.log or via journalctl

Q3. How do you allow a group to use sudo without a password?

Answer

%groupe ALL=(ALL) NOPASSWD: ALL

🔝 Back to table of contents



Key takeaways

  • sudo > su for security and audit
  • Always use visudo to edit sudoers
  • Apply the principle of least privilege
  • sudo actions are logged in /var/log/auth.log
  • Use /etc/sudoers.d/ for custom configurations
  • Avoid NOPASSWD: ALL except in special cases

🔝 Back to table of contents


← Previous chapter | Next chapter: Package management →