πŸ”’ Linux File Permissions, ACLs & Special Permissions! πŸš€

πŸ”’ Linux File Permissions, ACLs & Special Permissions! πŸš€

Understand and manage Linux file permissions, ACLs, and special permissions with easy examples.

Β·

4 min read


πŸ“Œ 1. Understanding File Permissions

Every file and directory in Linux has three sets of permissions for three types of users:

πŸ‘€ Owner – The user who created the file.
πŸ‘₯ Group – A set of users who share access.
🌍 Others – Anyone else with system access.

Each file permission is made up of three types of actions:

  • r (read) πŸ‘€ – Can view the contents of a file.

  • w (write) ✍️ – Can modify the file or add/remove content.

  • x (execute) πŸš€ – Can run the file if it’s a script or program.

πŸ“Œ Example of File Permissions

Run this command to see file permissions:

ls -ltr

Example output:

-rwxr--r-- 1 user group 1234 Feb 9 12:00 myfile.txt

Let's break this down:

  • -rwxr--r-- β†’ The first section shows permissions.

    • rwx β†’ Owner can read, write, and execute.

    • r-- β†’ Group can only read.

    • r-- β†’ Others can only read.

  • user β†’ The owner of the file.

  • group β†’ The group that owns the file.

  • 1234 β†’ File size in bytes.

  • Feb 9 12:00 β†’ Last modified date and time.

  • myfile.txt β†’ File name.


🎯 2. Changing File Permissions

πŸ›  Change File Permissions

You can modify who can read, write, or execute a file using chmod.

πŸ”Ή Add or Remove Specific Permissions

tchmod u+x file.txt  # Give execute permission to owner
chmod g-w file.txt  # Remove write permission from group
chmod o-r file.txt  # Remove read permission from others
  • u β†’ Owner

  • g β†’ Group

  • o β†’ Others

  • + β†’ Add permission

  • - β†’ Remove permission

πŸ”Ή Change Permissions Using Numbers

Each permission type has a number:

  • r = 4 (Read)

  • w = 2 (Write)

  • x = 1 (Execute)

To set permissions for owner, group, and others, sum up the values:

chmod 755 file.txt

Breakdown of 755:

  • 7 β†’ Owner = rwx (4+2+1 = 7)

  • 5 β†’ Group = r-x (4+0+1 = 5)

  • 5 β†’ Others = r-x (4+0+1 = 5)


πŸ›  Change Ownership of a File

When a file is created, it automatically belongs to the user who created it. You can change this using:

πŸ”Ή Change File Owner

chown new_owner file.txt
  • Example: If you want to change the owner of report.txt to alice:

      chown alice report.txt
    

πŸ”Ή Change Group Ownership

chgrp new_group file.txt
  • Example: If you want to change the group of data.csv to developers:

      chgrp developers data.csv
    

πŸ† 3. Access Control Lists (ACLs) – Advanced Permissions

Linux Access Control Lists (ACLs) allow more flexible permission management. Instead of just Owner, Group, Others, you can assign specific permissions to individual users.

πŸ›  View ACL Permissions

getfacl file.txt
  • This shows detailed permissions including special ACL settings.

πŸ›  Set ACL for a Specific User

setfacl -m u:username:rwx file.txt
  • Example: Give full access to bob for notes.txt:

      setfacl -m u:bob:rwx notes.txt
    

πŸ›  Remove ACL for a User

setfacl -x u:username file.txt
  • Example: Remove bob from ACL of notes.txt:

      setfacl -x u:bob notes.txt
    

⚑ 4. Special Permissions: Sticky Bit, SUID, SGID

🎩 Sticky Bit (πŸ‘’ Protecting shared folders!)

  • Used in shared directories like /tmp.

  • Prevents users from deleting files they don’t own.

πŸ”Ή Set Sticky Bit

chmod +t mydir

πŸ”Ή Check if Sticky Bit is set

ls -ld mydir

πŸ”Ή Example output: drwxrwxrwt (notice the t at the end)


🎭 SUID (Superpower Mode! 🦸)

  • Allows a file to run as the owner, not the user executing it.

  • Used for commands like passwd (which needs root privileges).

πŸ”Ή Set SUID on a script

chmod u+s script.sh

πŸ”Ή Check if SUID is set

ls -l script.sh

πŸ”Ή Example output: -rwsr-xr-x (notice the s in place of x)


🀝 SGID (Group Magic! πŸ§™)

  • Ensures new files inside a directory inherit the group.

  • Useful for collaborative project folders.

πŸ”Ή Set SGID on a directory

chmod g+s mydir

πŸ”Ή Check if SGID is set

ls -ld mydir

πŸ”Ή Example output: drwxrwsr-x (notice the s in the group section)


πŸ›  5. Automating Permissions with Scripts

πŸ“Œ Change Permissions for Multiple Files

#!/bin/bash
echo "Enter permission (e.g., 755): "
read perm
chmod $perm *
  • Prompts user for a permission code and applies it to all files in the directory.

πŸ“Œ Set ACL for a User on a File

#!/bin/bash
echo "Enter username: "
read user
setfacl -m u:$user:rwx myfile.txt
  • Prompts for a username and grants them full access to myfile.txt.

πŸ›‘οΈ 6. Backup & Restore Permissions

πŸ“Œ Backup Permissions

getfacl -R mydir > permissions_backup.txt
  • Saves all file permissions in mydir to permissions_backup.txt.

πŸ“Œ Restore Permissions

setfacl --restore=permissions_backup.txt
  • Restores previously saved permissions.
Β