File and Dir Permission

πŸ”Ή Linux File & Folder Permissions (Real-World Scenarios)

This guide covers: βœ… Understanding permissions (rwx, 777, 644, etc.) βœ… Setting & modifying permissions with chmod, chown, chgrp βœ… Real-world enterprise scenarios βœ… Advanced Access Control (ACLs, sticky bit, setuid, setgid) βœ… Common issues & solutions


πŸ”Ή Step 1: Understanding File Permissions

πŸ”Ή View file permissions using ls -l

ls -l /opt/project/file.txt

πŸ‘€ Example Output:

-rw-r--r-- 1 alice developers 2048 Feb 24 10:30 file.txt

Section

Meaning

-rw-r--r--

Permissions (Owner, Group, Others)

1

Number of hard links

alice

File owner

developers

File group

2048

File size (bytes)

Feb 24 10:30

Last modified date


πŸ”Ή Step 2: Understanding Permission Codes (777, 644, etc.)

Permission

Octal Code

Meaning

rwxrwxrwx

777

Everyone has full access (BAD PRACTICE)

rw-r--r--

644

Owner can read/write, others can only read (DEFAULT)

rwxr-xr-x

755

Owner can edit, others can only execute & read

rwx------

700

Only owner has full control (private files)

r--r--r--

444

Read-only for everyone


πŸ”Ή Step 3: Changing Permissions with chmod

3.1 Using Numeric Codes (Octal Method)

πŸ”Ή Give full access to everyone (777)

🚨 Real-World Warning: Never use 777 on important files – it gives full access to anyone!

πŸ”Ή Set read/write for owner, read for others (644)

πŸ”Ή Make a script executable (755)


3.2 Using Symbolic Method (r, w, x)

πŸ”Ή Give only the owner write access

πŸ”Ή Remove execute permission for the group

πŸ”Ή Give everyone execute permission

βœ… Common Issue: Permission Denied πŸ”Ή Check if the file has execute permission

πŸ”Ή Fix it:


πŸ”Ή Step 4: Changing Ownership with chown

πŸ”Ή Change file owner to alice

πŸ”Ή Change both owner (alice) and group (developers)

βœ… Common Issue: "Operation not permitted" error πŸ”Ή Fix: Use sudo


πŸ”Ή Step 5: Managing Group Access with chgrp

πŸ”Ή Change group ownership to developers


πŸ”Ή Step 6: Advanced File Permissions

6.1 Setting Default Permissions with umask

πŸ”Ή Check current default permissions

πŸ‘€ Output Example:

This means new files will have 644 permissions by default.

πŸ”Ή Change default permissions for new files

This will make new files 640 (owner read/write, group read, others no access).


6.2 Special Permissions (SetUID, SetGID, Sticky Bit)

πŸ”Ή SetUID (Run as file owner, not user running it)

πŸ“Œ Example: /bin/passwd uses SetUID to allow normal users to change their passwords.

πŸ”Ή SetGID (Inherit group ownership in a folder)

πŸ“Œ Use case: Ensures new files in /opt/project belong to developers group.

πŸ”Ή Sticky Bit (Prevent deletion by non-owners)

πŸ“Œ Use case: /tmp directory has a sticky bit so users can’t delete each other’s files.

βœ… Common Issue: Users can delete each other’s files in shared folders πŸ”Ή Fix: Apply the sticky bit


πŸ”Ή Step 7: Real-World Enterprise Scenarios

7.1 Scenario: Dev Team Needs Access to /opt/project

πŸ‘©β€πŸ’» Requirement:

  • Developers can read/write files

  • Admins have full access

  • Others have no access

βœ… Solution:

πŸ‘€ Breakdown:

  • chown -R root:developers /opt/project β†’ Sets group ownership

  • chmod 2770 /opt/project β†’ Enables SetGID so new files inherit developers group


7.2 Scenario: Secure Log Files from Unauthorized Access

πŸ‘¨β€πŸ’» Requirement:

  • Only admins can read logs

  • Developers should not access them

βœ… Solution:

πŸ‘€ Now only admins can view logs!


πŸš€ Summary: What We've Covered

βœ” Understanding rwx and permission codes (777, 644, etc.) βœ” Modifying permissions with chmod (numeric & symbolic) βœ” Changing ownership with chown & chgrp βœ” Advanced file access (SetUID, SetGID, Sticky Bit) βœ” Real-world scenarios (Dev team access, securing logs, etc.)


Next: Do you want to cover SELinux & AppArmor for enhanced security? πŸ”₯

Last updated