Galle, LK--:--5/6 sites live

Linux Permissions, Ownership, Chmod & Chown Security

පිටුව 04: Linux Permissions, Ownership, Chmod සහ Chown ආරක්ෂක ක්‍රම

Linux පරිශීලක හිමිකම් (User, Group, Others), කියවීම්/ලිවීම් අවසර (r=4, w=2, x=1), chmod 755/644/400 ගණනය කිරීම සහ chown භාවිතය.

5 Sept 20264 min readSelf Guide · Stage 10#Permissions#Security#SRE Labs

Lab 04 of 20 · Security & Permissions · 5 hands-on activities · bilingual (English + සිංහල)

පිටුව 04: Linux Permissions, Ownership, Chmod සහ Chown ආරක්ෂක ක්‍රම

Linux පරිශීලක හිමිකම් (User, Group, Others), කියවීම්/ලිවීම් අවසර (r=4, w=2, x=1), chmod 755/644/400 ගණනය කිරීම සහ chown භාවිතය.

💡 සරල සිංහලෙන්: chmod 755 සහ 644 ගණන් හදන්නේ කොහොමද?

r=4, w=2, x=1 සරල එකතු කිරීමේ ක්‍රමය:

  • Read (කියවීම): අගය 4
  • Write (ලිවීම/වෙනස් කිරීම): අගය 2
  • Execute (ධාවනය කිරීම): අගය 1

උදාහරණ:

  • 7 = 4 + 2 + 1 (Read + Write + Execute ඔක්කොම තියෙනවා).
  • 5 = 4 + 0 + 1 (Read සහ Execute විතරක් තියෙනවා, වෙනස් කරන්න බෑ).
  • 4 = 4 + 0 + 0 (Read විතරයි, වෙන කිසිම දෙයක් බෑ).
  • chmod 755: Owner ට 7 (rwx), Group එකට 5 (r-x), අනිත් අයට 5 (r-x). Deploy scripts වලට දාන්නේ මේකයි.
  • chmod 400: Owner ට විතරක් කියවන්න පුළුවන් (r--), අනිත් කාටවත් බෑ. AWS SSH Key වලට 400 දාන්නම ඕන, නැත්තම් Linux එකෙන් login වෙන්න දෙන්නේ නෑ!

Architectural Foundation & Systems Diagram

Linux is a multi-user operating system built on strict least-privilege access control. Every file and directory is owned by a User and a Group, with permissions split into three scopes:

  • 👤 Entity Scopes (u / g / o) — • User (u): The individual user account that owns the file.
    • Group (g): The group of users sharing access.
    • Others (o): Everyone else with an account on the machine.

  • 🔢 Octal Math: r=4, w=2, x=1 — • Read (r = 4): View file content / list directory.
    • Write (w = 2): Modify or delete file / add directory files.
    • Execute (x = 1): Run script or binary / enter directory.

  • 🛡️ Key Production Chmod Values — • 755 (rwxr-xr-x): Standard for executables & scripts.
    • 644 (rw-r--r--): Standard for config files & web assets.
    • 400 (r--------): Mandatory for SSH private keys!

Step-by-step practical lab

Step 01: Inspect File Inode Numbers

සිංහලෙන් — File එකක Inode අංකය (හැඳුනුම්පත) පරීක්ෂා කරමු.

Display the unique filesystem index node (Inode) number assigned to each file by the Virtual File System (VFS).

ExampleShell
ls -i Dockerfile
  • -i : Index node: ගොනුවේ Inode අංකය පෙන්වයි.

Expected output

Output
1441829 Dockerfile

SRE Tip: Two filenames with the exact same Inode number on the same disk are Hard Links pointing to the same physical data blocks.

Step 02: Check Filesystem Disk & Inode Capacity

සිංහලෙන් — Hard Disk එකේ ඉඩ සහ Inodes ප්‍රමාණය පරීක්ෂා කරමු.

Inspect available disk storage and inode capacity across all mounted storage partitions in human-readable format.

ExampleShell
df -h
  • -h : Human-readable: 10G, 500M වැනි මිනිසාට පහසුවෙන් කියවිය හැකි ඒකක වලින් පෙන්වයි.

Expected output

Output
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme0n1p1   50G   14G   34G  30% /
/dev/nvme1n1    100G   24G   72G  25% /var/lib/docker

SRE Tip: Even if df -h shows 50% free disk space, your server can still fail with 'No space left on device' if df -i shows 100% Inode exhaustion!

Step 03: Inspect Inode Consumption Ratios

සිංහලෙන් — Inodes පිරී ඇති ප්‍රතිශතය (Inode Exhaustion) පරීක්ෂා කරමු.

Check inode allocation limits across all mounted filesystems to prevent inode exhaustion outages.

ExampleShell
df -i
  • -i : Inodes: Storage bytes වෙනුවට ගොනු හැඳුනුම්පත් (Inodes) ප්‍රමාණය පෙන්වයි.

Expected output

Output
Filesystem       Inodes  IUsed   IFree IUse% Mounted on
/dev/nvme0n1p1  3276800 412800 2864000   13% /
/dev/nvme1n1    6553600 894200 5659400   14% /var/lib/docker

SRE Tip: Millions of tiny temporary cache files or email queues can exhaust inodes while leaving gigabytes of raw disk space free!

Hands-on practice activities

Work through these yourself — tick them off as you go.

  • Activity 16: List File Attributes and Permissions (Drill) — Print long-format listing with permissions, owners, file size, and timestamps.
  • Activity 17: Query Current Effective User Identity (Drill) — Print effective user name to confirm whether you are operating as unprivileged sre-engineer or superuser root.
  • Activity 18: Query User UID and Group GIDs (Drill) — Inspect numeric User ID (UID), primary Group ID (GID), and all supplementary group memberships.
  • Activity 19: Security Rule: Never Run 'chmod 777' (Checkpoint) — Never execute 'chmod 777' in production! It allows any local process or compromised service to overwrite code or inject backdoors.
  • Activity 20: Octal Permission Calculation Quiz (Quiz) — What is the numeric octal value for Read + Execute permissions? Answer: 5 (4 + 1).

Keep going: ← Lab 03 · Lab 05 → · Track your progress in the Self Guide

ExerciseKnowledge check

What numeric permission code grants Read, Write, and Execute to Owner, and Read & Execute to everyone else? / Owner ට rwx ද, අනෙක් අයට r-x ද ලබාදෙන අංකය කුමක්ද?

Finished this one?

Part of Self Guide · Stage 10: SRE Labs I — the machine underneath. Progress is saved in this browser.

Guide