Beginner 22 min readModule: Module 3: Permissions, Ownership & Access Control (POSIX & ACLs)
File Permissions, Ownership & Special Bits (SUID/SGID)
Understand read/write/execute permissions for User, Group, and Others, umask calculations, SUID executables, and the Sticky Bit on shared directories.
What You Will Learn in This Lesson
- The 3x3 POSIX permission matrix (User, Group, Others with r, w, x)
- Calculating octal numeric permission modes (e.g. 755, 644, 600, 700)
- Changing ownership and group affiliation with chown and chgrp
- Special permission bits: SetUID (4000), SetGID (2000), and Sticky Bit (1000)
Introduction & Core Concept
Linux is a multi-user operating system with robust POSIX access control. Every file and directory is owned by a User (UID) and a Group (GID), with permissions governing Read (r=4), Write (w=2), and Execute (x=1) access for the Owner, the owning Group, and all Other users on the system.
WHY DOES THIS MATTER IN THE REAL WORLD?
Improper permissions are a primary cause of security vulnerabilities (e.g., world-writable private SSH keys or database credentials) and deployment failures. Mastering permission masks and special bits guarantees system integrity.
Syntax & Structure
bash
chmod 755 script.shchmod 600 id_rsachown deploy:www-data /var/www/htmlchmod +t /shared_directoryConfiguring Secure File and Directory Permissions
bashbash
1234567891011121314151617181920#!/usr/bin/env bash# POSIX Permission Hardening Demonstrationmkdir -p /tmp/secure_app && cd /tmp/secure_app# Create sensitive credential file and executable scripttouch db_credentials.env deploy.sh# 1. Restrict sensitive secret to Owner ONLY (Read/Write = 600)chmod 600 db_credentials.env# 2. Grant Owner Read/Write/Exec (7), Group & Others Read/Exec (5) = 755chmod 755 deploy.sh# 3. Create a shared temporary directory with Sticky Bit (+t / 1777)mkdir -p shared_uploadschmod 1777 shared_uploads# Inspect permissions formatls -ld db_credentials.env deploy.sh shared_uploads
Line-by-Line Technical Breakdown
1Octal Math: Read = 4, Write = 2, Execute = 1. Add them together for each triad: rwx = 4+2+1 = 7, rw- = 4+2+0 = 6, r-x = 4+0+1 = 5, r-- = 4+0+0 = 4.
2Special Bits: SUID (4xxx) executes a binary with the permissions of the file owner (e.g., /usr/bin/passwd). SGID (2xxx) on directories forces newly created files to inherit the parent directory's group. Sticky Bit (1xxx) restricts file deletion in shared directories.
Try It Yourself (Interactive Editor)
Modify the code in real-time and click Run to test live browser output and console logs.
Intelligent Code Runner & Live Sandbox[BASH]
BASH SOURCE EDITOR
Interactive Live CodeCommon Mistakes & How to Avoid Them
#1: Using chmod 777 as a quick fix to resolve application permission errors.
chmod 777 makes every file readable, writable, and executable by every user and process on the system, creating severe security holes.
Incorrect / Antipattern
chmod -R 777 /var/www/appCorrect / Professional Solution
chown -R www-data:www-data /var/www/app
find /var/www/app -type d -exec chmod 755 {} +
find /var/www/app -type f -exec chmod 644 {} +Industry Best Practices & Professional Standards
- Private keys (~/.ssh/id_rsa) must always be set to chmod 600 (or chmod 400).
- Set default directory permissions to 755 and file permissions to 644.
- Configure umask 027 in production environments to prevent newly created files from being readable by unauthenticated users.
Lesson Summary & Core Takeaways
- Permissions are evaluated in strict order: Owner → Group → Others.
- Numeric modes use octal sums: Read (4) + Write (2) + Execute (1).
- The Sticky bit (1777) protects shared multi-user folders from unauthorized deletion.