🔒 File Permissions and Ownership
Every file and directory in Linux has permissions that control who can do what. Think of it like access levels in a school: some students can read a book, some can write in it, and only teachers can modify it fully. In this section, you'll learn how to view, change, and manage these permissions.
Understanding Linux File Permissions
Linux uses a permission system to control access to files and directories. Understanding this system is crucial for security and collaboration.
There are three basic types of permissions:
| Permission | Symbol | Meaning | Analogy |
|---|---|---|---|
| Read | r | Can open and read the file | You can look at a book but not write in it |
| Write | w | Can modify or delete the file | You can write in the notebook |
| Execute | x | Can run the file (if it's a program/script) | You can run a machine or execute a task |
Permissions apply to three types of users:
- Owner (u) → The file's creator
- Group (g) → Users who belong to the same group
- Others (o) → Everyone else
So each file has 9 permission slots: rwxrwxrwx
-rw-r--r-- - → file type (dash = regular file, d = directory) rw- → owner can read & write r-- → group can read only r-- → others can read only
Use the ls -l command to view detailed file permissions:
-rw-r--r-- 1 user user 4096 Oct 25 10:15 notes.txt
- First column → permissions
- Next columns → owner, group, size, date, filename
Think of a school library:
- Owner → The teacher who owns the book
- Group → Students in the class who can borrow it
- Others → Any other student in the school
- Permissions → Read (look at the book), Write (take notes in it), Execute (use it in a project)
chmod — Changing Permissions
The chmod command lets you change who can read, write, or execute a file. Think of it as adjusting the access rules for your files, like locking, unlocking, or giving special permission to someone.
Use letters to add, remove, or set permissions:
- u → user/owner
- g → group
- o → others
- a → all (user + group + others)
Operators:
- + → add a permission
- - → remove a permission
- = → set exact permissions
chmod u+x script.sh
chmod o-w notes.txt
chmod u=rw,g=r,o=r file.txt # Owner: read & write, Group & Others: read only
Each permission has a number value. Combine them for owner-group-others:
| Permission | Value |
|---|---|
| --- | 0 |
| --x | 1 |
| -w- | 2 |
| -wx | 3 |
| r-- | 4 |
| r-x | 5 |
| rw- | 6 |
| rwx | 7 |
7 → owner: rwx (read, write, execute) 5 → group: r-x (read, execute) 4 → others: r-- (read only)
Imagine keys for rooms:
- Owner key → full access (all rooms)
- Group key → partial access (some rooms)
- Others key → minimal access (lobby only)
chmod is you deciding who gets which key.
chown — Changing Ownership
The chown command lets you change the owner of a file or directory. Think of it like giving someone else the ownership of a notebook — they now control it (can read/write/execute depending on permissions).
Changes the owner of notes.txt to user alice.
- Owner → alice
- Group → teachers
To change owner for a directory and all its contents:
Every file and folder inside Projects/ now belongs to alice and group teachers.
Imagine a library book:
- Currently, you are the owner (teacher)
- You can assign the book to another teacher → chown
- You can also change the class group that can access it → owner:group combo
chgrp — Changing Group Ownership
The chgrp command lets you change the group ownership of a file or directory without affecting the owner. Think of it like reassigning which class of students can access a book, while the teacher (owner) stays the same.
The file notes.txt now belongs to the teachers group.
Owner remains the same.
To change the group for a directory and all its contents:
Every file/folder inside Projects/ is now assigned to the teachers group.
You often combine chgrp with chmod to give the group specific access:
The group teachers can now read and write the file.
- Owner → teacher who owns the book
- Group → students in a class
- chgrp → assign the book to a different class without changing the teacher
Topic Summary: File Permissions and Ownership
Understanding permissions → Learn who can read/write/execute files (rwx for owner, group, others)
chmod → Change permissions using symbolic (u+x) or numeric (754) methods
chown → Change file owner (and optionally group)
chgrp → Change only the group ownership
These commands are essential for security and collaboration on Linux systems.