π Linux File Permissions, ACLs & Special Permissions! π
Understand and manage Linux file permissions, ACLs, and special permissions with easy examples.
π 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
β Ownerg
β Groupo
β 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
toalice
:chown alice report.txt
πΉ Change Group Ownership
chgrp new_group file.txt
Example: If you want to change the group of
data.csv
todevelopers
: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
fornotes.txt
:setfacl -m u:bob:rwx notes.txt
π Remove ACL for a User
setfacl -x u:username file.txt
Example: Remove
bob
from ACL ofnotes.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
topermissions_backup.txt
.
π Restore Permissions
setfacl --restore=permissions_backup.txt
- Restores previously saved permissions.