Progress: 0/62 (0%)

🔒 File Permissions and Ownership

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.

Permission Types (r, w, x)

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
Permission Categories (Owner, Group, Others)

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

📌 Example:
-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
Viewing Permissions with ls -l

Use the ls -l command to view detailed file permissions:

ls -l notes.txt
📌 Example output:
-rw-r--r-- 1 user user 4096 Oct 25 10:15 notes.txt
  • First column → permissions
  • Next columns → owner, group, size, date, filename
Real-life analogy

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.

Symbolic Method (u, g, o, +, -, =)

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
📌 Give execute permission to owner:
chmod u+x script.sh
chmod u+x script.sh
📌 Remove write permission from others:
chmod o-w notes.txt
chmod o-w notes.txt
📌 Set exact permissions:
chmod u=rw,g=r,o=r file.txt
# Owner: read & write, Group & Others: read only
Numeric Method (Octal 0-7)

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
chmod 754 script.sh
📌 What 754 means:
7 → owner: rwx (read, write, execute)
5 → group: r-x (read, execute)
4 → others: r-- (read only)
Real-life analogy

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).

Basic usage
chown alice notes.txt

Changes the owner of notes.txt to user alice.

Change owner and group together
chown alice:teachers notes.txt
  • Owner → alice
  • Group → teachers
Recursive ownership change (-R)

To change owner for a directory and all its contents:

chown -R alice:teachers Projects/

Every file and folder inside Projects/ now belongs to alice and group teachers.

Real-life analogy

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.

Basic usage
chgrp teachers notes.txt

The file notes.txt now belongs to the teachers group.

Owner remains the same.

Recursive change (-R)

To change the group for a directory and all its contents:

chgrp -R teachers Projects/

Every file/folder inside Projects/ is now assigned to the teachers group.

Combine with chmod

You often combine chgrp with chmod to give the group specific access:

chgrp teachers file.txt chmod g+rw file.txt

The group teachers can now read and write the file.

Real-life analogy
  • 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.