RousselTMLEARNING

Systems & Linux

Users, groups, sudo and permissions on Linux

Manage Linux accounts (useradd, usermod, passwd, chage), groups, sudo and visudo, rwx and octal permissions, umask, SUID, SGID, sticky bit, ACLs and extended attributes.

1. Accounts and groups #

Who can log in and with which identity.

Account files #

FileContent
/etc/passwdname:x:UID:GID:comment:directory:shell (readable by everyone)
/etc/shadowpassword hashes and expiry (root only)
/etc/groupgroups and secondary members
/etc/sudoers, /etc/sudoers.d/sudo rules

Each account has a UID: 0 for root, below 1000 for system accounts (services), 1000 and above for humans. Services run under system accounts without a shell (/usr/sbin/nologin) to limit damage in case of compromise.

Managing accounts #

sudo useradd -m -s /bin/bash -c "Alice Martin" alice   # créer avec répertoire personnel
sudo passwd alice                     # définir le mot de passe
sudo usermod -aG devs,docker alice    # ajouter à des groupes (-a indispensable !)
sudo chage -M 90 -W 7 alice           # expiration du mot de passe à 90 jours
sudo chage -l alice                   # politique d'expiration
sudo usermod -L alice                 # verrouiller ; -U pour déverrouiller
sudo userdel -r bob                   # supprimer avec son répertoire
sudo useradd -r -s /usr/sbin/nologin -d /srv/app app   # compte de service
id alice ; groups alice ; getent passwd alice
who ; w ; last -n 10 ; lastlog         # qui est connecté, historique

Groups #

sudo groupadd devs
sudo gpasswd -d alice docker   # retirer d'un groupe
newgrp devs                    # activer un nouveau groupe sans se reconnecter
Common mistake: usermod -G devs alice without -a replaces all of alice's secondary groups: she loses, for example, the sudo group.

2. sudo #

Delegating root rights without sharing the password.

Principle #

How sudo works
Each command is attributed to a person: traceability is preserved.

sudo runs a command under another identity (root by default) after authenticating the user, and logs every command. You never log in directly as root: everyone uses their named account, which keeps traceability.

sudo -l                          # ce que j'ai le droit de faire
sudo -i                          # shell root (journalisé)
sudo -u postgres psql            # exécuter en tant qu'un autre utilisateur
sudo visudo -f /etc/sudoers.d/devs   # éditer avec vérification de syntaxe
journalctl _COMM=sudo --since today  # audit des commandes sudo

Rules #

# /etc/sudoers.d/devs
# utilisateur/groupe  hôtes=(identité)  commandes
%sudo   ALL=(ALL:ALL) ALL
%devs   ALL=(root) /usr/bin/systemctl restart app, /usr/bin/journalctl -u app*
deploy  ALL=(root) NOPASSWD: /usr/local/bin/deploy.sh
Good practice: Grant specific commands rather than ALL, and never allow an editor, less or a user-modifiable script through sudo: they make it possible to get a root shell.
Common mistake: Always edit with visudo: a syntax error in sudoers can block all root access.

3. Permissions #

Read, write, execute.

Reading permissions #

Breakdown of permissions
r = 4, w = 2, x = 1: add them up for each category.
PermissionOn a fileOn a directory
r (4)read the contentlist the names
w (2)modifycreate, rename, delete files
x (1)executetraverse (cd, access files)
Order in which permissions are evaluated
An owner with r-- cannot write, even if the group has rw-.

Changing them #

chmod 640 secret.conf            # rw- r-- ---
chmod u+x deploy.sh              # ajouter l'exécution au propriétaire
chmod -R g+rwX /srv/projet       # X : exécution seulement sur les répertoires
sudo chown alice:devs fichier    # changer propriétaire et groupe
sudo chown -R www-data: /srv/www # groupe principal de l'utilisateur
find /srv -type d -exec chmod 2775 {} +   # appliquer aux seuls répertoires

umask #

The umask removes permissions from created files. With umask 022, a file is created as 644 and a directory as 755; with 027, as 640 and 750 (nothing for others).

umask ; umask 027   # valeur courante, puis plus restrictive pour la session

4. Special permissions, ACLs and attributes #

When rwx is not enough.

SUID, SGID and sticky bit #

BitNotationEffect
SUID (4000)rws------the program runs with its owner's rights (e.g. passwd)
SGID (2000) on a directoryrwxrws---created files inherit the directory's group: ideal for a team folder
Sticky (1000)rwxrwxrwtonly the owner can delete their files (e.g. /tmp)
sudo chmod 2770 /srv/equipe            # dossier partagé avec héritage du groupe
find / -xdev -perm -4000 -type f 2>/dev/null   # inventaire des binaires SUID (audit)
Group inheritance with SGID
With a 002 umask, all members of devs can modify the team's files.

ACLs #

ACLs grant permissions to additional users or groups, without changing the owner. A + at the end of the permissions in ls -l signals an ACL.

setfacl -m u:bob:rx /srv/rapports          # droit pour bob
setfacl -d -m g:audit:r /srv/rapports      # ACL par défaut, héritée par les nouveaux fichiers
getfacl /srv/rapports
setfacl -b /srv/rapports                   # supprimer toutes les ACL

Attributes #

sudo chattr +i /etc/resolv.conf   # immuable, même pour root
sudo chattr +a /var/log/audit.log  # ajout uniquement
lsattr /etc/resolv.conf

Related articles

Linux system architecture: kernel, boot and directory tree

Understand how a Linux server works: kernel and user space, the UEFI, GRUB, initramfs and systemd boot sequen…

The Bash shell and files: navigation, redirections, pipes and text processing

Master the Linux command line: paths, file handling, wildcards, redirections, pipes, searching with find and …

Packages, systemd services, logs and scheduled tasks

Install and update software with apt and dnf, manage services with systemctl, write a systemd unit, read logs…

Keep learning

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!