Lab 08: chmod — Changing Permissions

Objective

Use chmod in both symbolic and octal modes to set permissions precisely. Understand umask, set permissions on scripts, and apply security hardening patterns.

Time: 25 minutes | Level: Foundations | Docker: docker run -it --rm ubuntu:22.04 bash


Step 1: Default Permissions

touch /tmp/testfile
ls -la /tmp/testfile

📸 Verified Output:

-rw-r--r-- 1 root root 0 Mar  5 00:57 /tmp/testfile

New files default to 644 (-rw-r--r--). This comes from umask.


Step 2: chmod with Octal Notation

chmod 755 /tmp/testfile
ls -la /tmp/testfile

📸 Verified Output:

-rwxr-xr-x 1 root root 0 Mar  5 00:57 /tmp/testfile

📸 Verified Output:

💡 Common octal values to memorise:

  • 600 — private file (SSH keys, credentials)

  • 644 — config files, web pages

  • 700 — private directory

  • 755 — scripts and executables

  • 777 — NEVER use in production (everyone can modify)


Step 3: chmod with Symbolic Notation

Symbolic format: [who][operator][permission]

  • who: u=user/owner, g=group, o=other, a=all

  • operator: +=add, -=remove, ==set exactly

  • permission: r, w, x

📸 Verified Output:

📸 Verified Output:

💡 Symbolic mode is safer for modification (+x only adds execute, won't change other bits). Octal mode is safer for setting exact permissions (chmod 644 sets exactly rw-r--r-- regardless of current state).


Step 4: Recursive chmod

📸 Verified Output:

💡 Never use chmod -R 777 . — it's a common "quick fix" that creates serious security vulnerabilities. Use find to apply different permissions to files vs directories.


Step 5: Making Scripts Executable

📸 Verified Output:

📸 Verified Output:


Step 6: umask — Default Permission Mask

📸 Verified Output:

📸 Verified Output:

umask 022 means: subtract 022 from max permissions 666 (files) → 644.

📸 Verified Output:

💡 Set umask 027 in /etc/profile or /etc/bash.bashrc on security-sensitive servers. New files will never be world-readable by default.


Step 7: Private Directory

📸 Verified Output:


Step 8: Capstone — Harden a Web Application Directory

📸 Verified Output:


Summary

Command
Effect

chmod 644 file

Owner rw, group/other r

chmod 755 file

Owner rwx, group/other rx

chmod 600 file

Owner rw only (private)

chmod +x file

Add execute for all

chmod u+x file

Add execute for owner only

chmod -R 755 dir

Recursive (use carefully)

umask 022

New files default to 644

umask 027

New files default to 640

Last updated