Unit 4: File Permissions and Ownership
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 -lprints 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:
- 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 withoutx.
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. Withoutx, even a readable directory yields "Permission denied" on its contents. - Worked distinction: A directory with
r--lets you runls(see names) but notcat file(no traversal); with--xyou cancata known filename butlsfails.
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, ora(all). - operator:
+(add),-(remove),=(set exactly). - permissions: any of
r,w,x.
- who:
- 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-----: owner4+2 = 6, group4 = 4, others0→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.txtcopies ownership from another file. - Related tool:
chgrp developers filechanges only the group; the file's own group members may run it ifchgrpis 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 receivexby 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
umaskprints the current mask (e.g.0022);umask -Sprints 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.
- File:
077(strict/private): Files become600, directories700— no access for group or others.- Setting it:
umask 027applies for the current shell session; placing it in~/.bashrcor/etc/profilemakes 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/passwdmaps UID to username. - Creator default: The user who creates a file becomes its owner automatically.
- Owner privileges: The owner may run
chmodon 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/groupmaps 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 (
idlists them). - Practical use: A
developersgroup owning/srv/projectwith mode770gives all members full access while excluding everyone else. - setgid on directories:
chmod g+s dirmakes 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/750over666/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_rsamust be600; SSH refuses keys readable by group or others.
B. Special Permission Bits
- SUID (
4xxx, shown assin owner-execute): A program runs with the owner's privileges, not the caller's./usr/bin/passwduses 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 ast): 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 -4000locates all SUID binaries for review;find / -perm -0002finds world-writable files. - Root discipline: Because root bypasses all bits, run privileged commands via
sudofor a specific task rather than as a persistent root login, preserving an audit trail. - Immutable flag:
chattr +i fileblocks even the owner from modifying or deleting a file — a layer beyond the permission model for critical configs.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →