Unit 4: File Permissions and Ownership

CSE105 — Creative Engineering Workshop 7 min read

Linux inherits the multi-user, permission-based access control model of UNIX (developed at Bell Labs, early 1970s). Every file and directory carries metadata that decides who may access it and how. Because Linux treats almost everything as a file, this single model governs regular files, directories, devices in /dev, and pseudo-files in /proc.

The model rests on a fixed set of conventions that every later section depends on:

  • Three permission classes: Access is granted to exactly three categories — the file's owner (user, u), the file's group (g), and others (o), meaning everyone else on the system.
  • Three permission types: Each class independently holds read (r), write (w), and execute (x) rights.
  • Discretionary control: The owner (and root) decides who gets access, hence "discretionary access control" (DAC).
  • Root override: The superuser (uid 0) bypasses all permission checks; permissions constrain ordinary users only.
  • Inode storage: Permission bits and ownership IDs live in the file's inode, not the filename, so hard links share identical permissions.
  • Displayed as a 10-character string: ls -l prints permissions like -rwxr-xr--, decoded in the next section.

II. Read, Write and Execute Permissions — What Each Bit Grants

Each of the nine permission bits means something different for files than for directories; misreading this distinction is the commonest source of access bugs.

The ls -l string decodes positionally:

TEXT
-  rwx  r-x  r--
│   │    │    │
│   │    │    └─ others
│   │    └────── group
│   └─────────── owner (user)
└─────────────── file type (- file, d directory, l symlink)

A. Read (r)

  • On a file: Permits viewing contents with tools like cat, less, or opening in an editor.
  • On a directory: Permits listing the names inside it (e.g. ls), but not accessing the files themselves without x.

B. Write (w)

  • On a file: Permits modifying or truncating contents.
  • On a directory: Permits creating, renaming, and deleting entries within it. Deleting a file therefore depends on write permission on its directory, not on the file itself.

C. Execute (x)

  • On a file: Permits running it as a program or script.
  • On a directory: Permits entering/traversing it (cd) and accessing files by name. Without x, even a readable directory yields "Permission denied" on its contents.
  • Worked distinction: A directory with r-- lets you run ls (see names) but not cat file (no traversal); with --x you can cat a known filename but ls fails.

III. chmod — Changing Permission Bits

chmod ("change mode") edits the nine permission bits. It accepts two interchangeable syntaxes producing identical results.

A. Symbolic Mode

  • Purpose: Adjust specific bits relatively, leaving others untouched.
  • Grammar: chmod [who][operator][permissions] file
    • who: u, g, o, or a (all).
    • operator: + (add), - (remove), = (set exactly).
    • permissions: any of r, w, x.
  • Examples:
    • chmod u+x script.sh — grant execute to owner only.
    • chmod go-w file.txt — remove write from group and others.
    • chmod a=r notes.txt — set everyone to read-only, clearing all else.

B. Numeric (Octal) Mode

  • Purpose: Set all three classes at once via an absolute value.
  • Principle: Each permission has a weight — read = 4, write = 2, execute = 1 — summed per class to give one octal digit (0–7).
  • Digit table: 7 = rwx, 6 = rw-, 5 = r-x, 4 = r--, 0 = ---.
  • Examples:
    • chmod 755 script.sh → rwxr-xr-x (owner full; group/others read-execute).
    • chmod 644 notes.txt → rw-r--r-- (owner read-write; others read-only).
    • chmod 600 secret.key → rw------- (owner only).
  • Worked example: For rw-r-----: owner 4+2 = 6, group 4 = 4, others 0 → chmod 640 file.
  • Recursion: chmod -R 755 dir/ applies to a directory and everything beneath it.

IV. chown Command — Reassigning Ownership

chown ("change owner") changes which user and/or group owns a file. Because it can transfer ownership away from oneself, only root may change the owning user.

  • Basic syntax: chown [owner][:group] file.
  • Change user only: chown alice report.txt — alice becomes owner.
  • Change user and group: chown alice:developers report.txt — sets both in one call.
  • Change group only: chown :developers report.txt (leading colon) — user unchanged.
  • Recursive: chown -R www-data:www-data /var/www — retargets an entire tree, common when handing a directory to a service account.
  • Reference form: chown --reference=template.txt new.txt copies ownership from another file.
  • Related tool: chgrp developers file changes only the group; the file's own group members may run it if chgrp is permitted to them (a user can set any group they belong to).

V. umask Command — Default Permission Masking

umask ("user mask") determines the permissions removed from files at creation, controlling defaults rather than existing files.

A. Principle of Subtraction

  • Base permissions: The system starts new files at 666 (rw-rw-rw-) and new directories at 777 (rwxrwxrwx). Files never receive x by default, for safety.
  • Masking operation: The final permission is base AND (NOT umask) — the umask bits are cleared, not arithmetically subtracted, though the common values behave like subtraction.
  • Display: Running umask prints the current mask (e.g. 0022); umask -S prints it symbolically (u=rwx,g=rx,o=rx).

B. Common Values

  • 022 (typical default):
    • File: 666 − 022 = 644 → rw-r--r--.
    • Directory: 777 − 022 = 755 → rwxr-xr-x.
  • 077 (strict/private): Files become 600, directories 700 — no access for group or others.
  • Setting it: umask 027 applies for the current shell session; placing it in ~/.bashrc or /etc/profile makes it persistent per user or system-wide.

VI. File Ownership and Group Ownership — The Identity Layer

Permissions are meaningless without the identities they apply to; every file records two numeric IDs that map to names.

A. File Ownership (User)

  • Definition: Each file stores a UID identifying its owning user; /etc/passwd maps UID to username.
  • Creator default: The user who creates a file becomes its owner automatically.
  • Owner privileges: The owner may run chmod on the file even without write permission, since ownership is checked before the permission bits.
  • Display: In ls -l, the third column shows the owner (e.g. -rw-r--r-- 1 alice staff ...).

B. Group Ownership

  • Definition: Each file also stores a GID; /etc/group maps it to a group name. Group ownership lets several users share access without opening files to "others".
  • Primary vs supplementary groups: A user has one primary group (assigned at file creation) and may belong to additional supplementary groups (id lists them).
  • Practical use: A developers group owning /srv/project with mode 770 gives all members full access while excluding everyone else.
  • setgid on directories: chmod g+s dir makes new files inside inherit the directory's group rather than the creator's primary group — essential for shared project folders.

VII. Permission Security Concepts — Safe Configuration

Correct permissions are a frontline defence; over-permissive files are a standard attack vector.

A. Principle of Least Privilege

  • Rule: Grant the minimum rights needed and no more; default to 640/750 over 666/777.
  • World-writable danger: A file with o+w (e.g. 777) lets any user overwrite it — never assign it to scripts or config files.
  • SSH keys: ~/.ssh/id_rsa must be 600; SSH refuses keys readable by group or others.

B. Special Permission Bits

  • SUID (4xxx, shown as s in owner-execute): A program runs with the owner's privileges, not the caller's. /usr/bin/passwd uses SUID root so users can update /etc/shadow. SUID on unvetted binaries is a privilege-escalation risk.
  • SGID (2xxx): On executables, runs with the group's rights; on directories, forces group inheritance (see VI-B).
  • Sticky bit (1xxx, shown as t): On a shared directory like /tmp (1777), only a file's owner may delete it, despite the directory being world-writable.

C. Auditing and Practical Safeguards

  • Find risky files: find / -perm -4000 locates all SUID binaries for review; find / -perm -0002 finds world-writable files.
  • Root discipline: Because root bypasses all bits, run privileged commands via sudo for a specific task rather than as a persistent root login, preserving an audit trail.
  • Immutable flag: chattr +i file blocks even the owner from modifying or deleting a file — a layer beyond the permission model for critical configs.