Skip to main content

Permissions and access rights


Table of contents

  1. Understanding rwx
  2. Symbolic vs octal notation
  3. chmod: modify permissions
  4. chown and chgrp: change owner
  5. Special permissions
  6. Hands-on exercises


1 - Understanding rwx

The three basic permissions

PermissionLetterOn a fileOn a directory
ReadrRead the contentList the content
WritewModify the contentCreate/delete files
ExecutexExecute (script/program)Enter the directory

The three categories

Reading ls -l

-rwxr-xr-- 1 john developers 4096 Jan 15 10:30 script.sh
│├─┤├─┤├─┤
││ │ │
││ │ └── Others : r-- (lecture seule)
││ └── Group : r-x (lecture + exécution)
│└── User : rwx (lecture + écriture + exécution)
└── Type (- = fichier, d = dossier)

Permission examples

PermissionMeaning
rwxrwxrwxEveryone can do everything
rwxr-xr-xOwner everything, others read+execute
rw-r--r--Owner read+write, others read
rwx------Only the owner
r--------Owner read only
warning

A file with the permissions rwxrwxrwx (777) is a security flaw! Anyone can modify it.

🔝 Back to table of contents



2 - Symbolic vs octal notation

Symbolic notation

Uses letters to represent the permissions.

rwxr-xr--
│││││││││
│││││││└┴── Others : r-- = 4
│││││└┴──── Group : r-x = 5
│││└┴────── User : rwx = 7
└┴┴──────── Propriétaire, Groupe, Autres

Octal (numeric) notation

Each permission has a value:

PermissionValue
Read (r)4
Write (w)2
Execute (x)1
None (-)0

Calculation: You add the values for each category.

PermissionsCalculationOctal
rwx4+2+17
rw-4+2+06
r-x4+0+15
r--4+0+04
---0+0+00

Conversion examples

SymbolicOctalUsage
rwxr-xr-x755Scripts, directories
rw-r--r--644Normal files
rw-------600Sensitive files
rwx------700Private scripts
rwxrwxrwx777⚠️ Dangerous!

Quick reference table

Octal  Symbolique  Description
────────────────────────────────
0 --- Aucun droit
1 --x Exécution
2 -w- Écriture
3 -wx Écriture + Exécution
4 r-- Lecture
5 r-x Lecture + Exécution
6 rw- Lecture + Écriture
7 rwx Tous les droits

🔝 Back to table of contents



3 - chmod: modify permissions

chmod = change mode

Syntax

chmod [options] MODE fichier

Symbolic mode

# Ajouter une permission (+)
chmod u+x script.sh # Ajoute exécution au propriétaire
chmod g+w fichier.txt # Ajoute écriture au groupe
chmod o+r fichier.txt # Ajoute lecture aux autres
chmod a+x script.sh # Ajoute exécution à tous (a = all)

# Retirer une permission (-)
chmod o-w fichier.txt # Retire écriture aux autres
chmod g-x script.sh # Retire exécution au groupe

# Définir exactement (=)
chmod u=rwx,g=rx,o=r fichier.txt
chmod a=r fichier.txt # Lecture seule pour tous

Octal mode

chmod 755 script.sh      # rwxr-xr-x
chmod 644 fichier.txt # rw-r--r--
chmod 600 secret.txt # rw-------
chmod 700 dossier/ # rwx------

Useful options

OptionEffect
-RRecursive (folders)
-vVerbose
--referenceCopy the permissions of another file
# Appliquer récursivement
chmod -R 755 dossier/

# Mode verbeux
chmod -v 644 fichier.txt
# mode of 'fichier.txt' changed from 0755 (rwxr-xr-x) to 0644 (rw-r--r--)

# Copier les permissions
chmod --reference=modele.txt cible.txt

Common use cases

# Rendre un script exécutable
chmod +x script.sh

# Protéger une clé SSH
chmod 600 ~/.ssh/id_rsa

# Répertoire web
chmod 755 /var/www/html/
chmod 644 /var/www/html/*.html

# Fichier de configuration sensible
chmod 600 config.ini

🔝 Back to table of contents



4 - chown and chgrp: change owner

chown - Change the owner

# Changer le propriétaire
sudo chown alice fichier.txt

# Changer propriétaire et groupe
sudo chown alice:developers fichier.txt

# Changer seulement le groupe (avec :)
sudo chown :developers fichier.txt

# Récursif
sudo chown -R alice:developers dossier/

chgrp - Change the group

# Changer le groupe
sudo chgrp developers fichier.txt

# Récursif
sudo chgrp -R www-data /var/www/

DevOps use cases

# Fichiers web appartenant à www-data
sudo chown -R www-data:www-data /var/www/html/

# Répertoire de déploiement
sudo chown -R deploy:deploy /app/

# Fichiers de log
sudo chown syslog:adm /var/log/app.log
tip

The syntax chown user:group is more convenient than using chown then chgrp separately.

🔝 Back to table of contents



5 - Special permissions

Beyond rwx, there are three special permissions.

SUID (Set User ID)

When a file with SUID is executed, it runs with the rights of the owner (not of the user who launches it).

# Exemple : /usr/bin/passwd
ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root ... /usr/bin/passwd
# ^
# s = SUID (exécution avec droits root)

# Définir SUID
chmod u+s fichier
chmod 4755 fichier

SGID (Set Group ID)

  • On a file: runs with the rights of the group
  • On a directory: files created inherit the group of the directory
# Sur un répertoire partagé
chmod g+s /shared/
chmod 2775 /shared/

ls -ld /shared/
drwxrwsr-x 2 root developers ... /shared/
# ^
# s = SGID

Sticky bit

On a directory, only the owner of a file can delete it (even if others have write access).

# Exemple : /tmp
ls -ld /tmp
drwxrwxrwt 10 root root ... /tmp
# ^
# t = Sticky bit

# Définir le sticky bit
chmod +t dossier/
chmod 1777 dossier/

Summary table

PermissionOctalSymbolEffect
SUID4000s (user)Runs as the owner
SGID2000s (group)Runs as the group / Inheritance
Sticky1000tDeletion restricted to the owner

Octal calculation with special permissions

chmod 4755 = SUID + rwxr-xr-x
chmod 2755 = SGID + rwxr-xr-x
chmod 1777 = Sticky + rwxrwxrwx
chmod 6755 = SUID + SGID + rwxr-xr-x

🔝 Back to table of contents



6 - Hands-on exercises

Exercise 1: Reading permissions

Decode these permissions:

ls -lUserGroupOthersOctal
-rw-r--r--????
-rwxr-x---????
drwxrwxr-x????
See the answers
ls -lUserGroupOthersOctal
-rw-r--r--rw- (6)r-- (4)r-- (4)644
-rwxr-x---rwx (7)r-x (5)--- (0)750
drwxrwxr-xrwx (7)rwx (7)r-x (5)775

Exercise 2: Set permissions

# Créez ces fichiers et définissez les permissions :

# 1. Script exécutable par tous
touch script.sh
chmod 755 script.sh

# 2. Fichier de config lisible par propriétaire seulement
touch config.ini
chmod 600 config.ini

# 3. Répertoire partagé (groupe peut écrire)
mkdir shared
chmod 775 shared

# Vérifiez
ls -la script.sh config.ini shared

Exercise 3: Change owners

# En tant que root (sudo)

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

# 2. Créez un fichier
touch testfile.txt

# 3. Donnez-le à testuser
sudo chown testuser:testuser testfile.txt

# 4. Vérifiez
ls -l testfile.txt

# 5. Nettoyez
sudo userdel -r testuser
rm testfile.txt

🔝 Back to table of contents



Key takeaways

  • r=4, w=2, x=1 → add them up to get the octal
  • chmod 755 = rwxr-xr-x (scripts, directories)
  • chmod 644 = rw-r--r-- (normal files)
  • chmod 600 = rw------- (sensitive files)
  • chown user:group changes owner AND group
  • SUID/SGID/Sticky = advanced special permissions
  • Always verify with ls -l after a change

🔝 Back to table of contents


← Previous chapter | Next chapter: sudo and privileges →