Table of contents
- π Mastering File Permissions & ACLs in Linux
π Mastering File Permissions & ACLs in Linux
Linux provides a powerful permission model to control who can access, modify, and execute files on a system. Proper permission management is crucial for security, stability, and efficient user management. Today, we will dive deep into file permissions, ownership, and Access Control Lists (ACLs) with real-world examples. π
π Understanding Linux File Permissions
Every file in Linux has three types of user classifications:
1οΈβ£ Owner (User) β The creator of the file (e.g., a developer working on a script).
2οΈβ£ Group β A set of users who share permissions (e.g., DevOps engineers in a team).
3οΈβ£ Others β Any other user on the system (e.g., external users accessing a shared server).
Each classification has three permission types:
π Read (r) β Can view the file's contents.
βοΈ Write (w) β Can edit or delete the file.
π Execute (x) β Can run the file if it's a script or program.
π» Checking File Permissions
To check a fileβs permissions, run:
ls -l myfile.txt
Example Output:
-rwxr--r-- 1 devops team 1024 Feb 22 10:00 deploy.sh
Breaking Down the Permissions:
rwxr--r--
β Represents who can do what (user, group, others).devops
β The owner of the file.team
β The group assigned to the file.1024
β File size in bytes.Feb 22 10:00
β Last modified date.deploy.sh
β File name.
π Decoding the Permission Code
Position | Symbol | Meaning |
- | File type (- for file, d for directory) | |
rwx | Owner can read, write, and execute | |
r-- | Group can only read | |
r-- | Others can only read |
π§ Modifying File Permissions Using chmod
πΉ Symbolic Mode (User-Based)
Change permissions for User (u), Group (g), and Others (o):
Example 1: Grant execute permission to the owner:
chmod u+x deploy.sh
β Now, the owner can execute the script.
Example 2: Remove write access from the group:
chmod g-w deploy.sh
β The group can no longer modify the file.
π’ Octal Mode (Numeric Representation)
Permissions are represented as:
- r (Read) = 4, w (Write) = 2, x (Execute) = 1
Permission | Octal Value |
rwx | 7 |
rw- | 6 |
r-- | 4 |
Example 3: Set permissions to rw-r--r--
(Owner: Read & Write, Group & Others: Read-Only)
chmod 644 deploy.sh
β Ensures only the owner can edit the file.
π Changing File Ownership with chown
Every file has an owner and an associated group. The chown
command is used to change the owner or group of a file.
Example 1: Assign john
as the new owner of deploy.sh
:
sudo chown john deploy.sh
β Now, John owns the file.
Example 2: Change both owner and group to john:developers
:
sudo chown john:developers deploy.sh
β Now, John and the developers group manage the file.
π‘οΈ Advanced Access Control Lists (ACLs)
Traditional permissions (chmod
and chown
) are sometimes too restrictive. What if you want to give a specific user extra permissions without changing the group? Thatβs where Access Control Lists (ACLs) come in!
π Checking ACLs for a File
getfacl deploy.sh
β This displays custom permissions beyond standard Linux permissions.
βοΈ Adding ACLs
Example: Grant alice
read and execute access to deploy.sh
without modifying group settings:
setfacl -m u:alice:rx deploy.sh
β Now, Alice can read & execute the file.
β Removing ACLs
To remove all ACLs from a file:
setfacl -b deploy.sh
β Now, the file follows only traditional permissions.
π― Real-Life Use Cases for File Permissions & ACLs
πΉ Example 1: Securing Deployment Scripts in DevOps
Scenario: A DevOps team maintains a deploy.sh
script used for server updates.
The script owner should have read, write, and execute permissions.
The DevOps team (group) should have read & execute access.
Other users should not have any access.
Solution:
chmod 750 deploy.sh
β This ensures only authorized team members can execute the script.
πΉ Example 2: Shared Project Folder for Developers
Scenario: A development team is working on a project.
The Project Lead should have full access.
Developers should have read and write access.
Interns should have only read access.
Solution Using ACLs:
setfacl -m u:lead:rwx project_folder
setfacl -m g:developers:rw project_folder
setfacl -m g:interns:r project_folder
β This customizes access levels for different roles in the team.
πΉ Example 3: Preventing Accidental File Deletion
Scenario: A critical system log file should never be modified by regular users.
Solution: Apply immutable flag to prevent accidental edits:
chattr +i /var/log/syslog
β Now, even root cannot delete it without removing the flag.
π Key Takeaways for IT Professionals
βοΈ File Permissions (chmod
) control who can read, write, and execute files.
βοΈ Ownership (chown
) defines who manages a file.
βοΈ ACLs (setfacl
) allow fine-grained access control beyond traditional permissions.
βοΈ Immutable files (chattr
) can protect critical files from accidental deletion.
π― Next Steps
π‘ Practice Task:
1οΈβ£ Create a test file and experiment with chmod
, chown
, and ACLs
.
2οΈβ£ Try setting different access levels for different users in a group.
3οΈβ£ Learn about special permissions (SUID, SGID, Sticky Bit) for advanced security.
π Coming Next: Day 7 β Linux Process Management! βοΈ Stay tuned!