DEV Community

Cover image for File Permissions and Access Control Lists
FARUKH KHAN
FARUKH KHAN

Posted on

File Permissions and Access Control Lists

File permissions in Linux are crucial for system security, determining who can access, modify, or execute files. As DevOps engineers, understanding how to manage permissions effectively is essential to prevent unauthorized access and ensure smooth workflows. This post covers everything from basic file permissions to ACL (Access Control Lists), SUID, SGID, and Sticky Bit, and even scripts to automate these tasks. Let’s dive in! πŸš€

πŸ“ Understanding File Permissions

In Linux, every file and directory is associated with three types of permissions: read (r), write (w), and execute (x). These permissions are granted to three categories of users:

Owner: The user who owns the file.

Group: The group that owns the file.

Others: Any other user on the system.

πŸ” Example: Listing File Permissions

Let’s create a file and check its permissions:

touch myfile.txt
ls -ltr myfile.txt
Enter fullscreen mode Exit fullscreen mode

The output will look something like this:

-rw-r--r-- 1 user group 0 Oct 18 10:30 myfile.txt

Enter fullscreen mode Exit fullscreen mode

Here’s what this means:

-rw-r--r-- β†’ File permissions (r = read, w = write, x = execute)

Owner (rw-): Read and write permission.

Group (r--): Only read permission.

Others (r--): Only read permission.

1 β†’ Number of links to the file.

user β†’ The file owner.

group β†’ The group that owns the file.

0 β†’ File size (in bytes).

Oct 18 10:30 β†’ Date and time of last modification.

πŸ”§ Modifying Permissions

We can modify file permissions using the following commands:

Change the owner of the file using chown:

sudo chown newuser myfile.txt

Enter fullscreen mode Exit fullscreen mode

Change the group of the file using chgrp:

sudo chgrp newgroup myfile.txt

Enter fullscreen mode Exit fullscreen mode

Modify permissions for others using chmod:

chmod u-w myfile.txt

Enter fullscreen mode Exit fullscreen mode

Run ls -ltr again to verify the changes.

πŸ”‘ Pro Tip: Always review your file permissions after making changes to avoid accidental exposure of sensitive data.

✍️ File Permissions

File permissions in Linux are the foundation of security and proper system administration. Every file and directory has associated permissions that dictate how users and groups can interact with them. Understanding these permissions helps to control access levels and ensure the right people have the proper permissions.

Understanding the Basics

Each file and directory in Linux is controlled by three permission types:

Read (r): Grants the ability to view the contents of a file or list the contents of a directory.

Write (w): Allows the user to modify or delete the contents of a file or add/remove files from a directory.

Execute (x): Grants permission to run a file (if it's an executable script or binary) or enter a directory.

These permissions are split across three categories of users:

Owner (u): The person who created the file and has the most control.

Group (g): A set of users who share access to the file based on their group membership.

Others (o): All other users who are not the owner or part of the group.

Modifying File Permissions: The chmod Command

You can use the chmod command to modify the permissions of a file. This command can be used in symbolic or numeric modes. Let’s explore both:

Symbolic Mode:

In symbolic mode, you can change permissions by specifying which user category (u, g, o) gets which permissions (r, w, x):

Grant write permission to the group:

chmod g+w myfile.txt

Enter fullscreen mode Exit fullscreen mode

Remove execute permission for others:

chmod o-x myfile.txt

Enter fullscreen mode Exit fullscreen mode

Numeric Mode:

In numeric mode, permissions are represented by numbers:

Read = 4

Write = 2

Execute = 1

So, when you sum them up:

7 (rwx) = Full permission.

6 (rw-) = Read and write.

5 (r-x) = Read and execute.

4 (r--) = Read only.

You can set the permissions using a three-digit number where each digit corresponds to the permissions for the owner, group, and others.

For example, to grant full access to the owner, read and execute permissions to the group, and only read permissions to others:

chmod 754 myfile.txt

Enter fullscreen mode Exit fullscreen mode

The Role of chown and chgrp

chown: This command changes the owner of a file:

sudo chown newowner myfile.txt

chgrp: This command changes the group ownership of a file:

sudo chgrp newgroup myfile.txt

The Importance of File Permissions

Properly managing file permissions is crucial to maintaining system security and operational efficiency. Whether you are managing a single system or a large infrastructure, the concepts of ownership, groups, and others remain fundamental to controlling file access. Using chmod, chown, and chgrp commands, you can fine-tune who can view, modify, or execute files, ensuring the integrity and confidentiality of your system.

πŸ” Access Control Lists (ACL)

Access Control Lists (ACLs) provide more granular control over file permissions, allowing you to specify permissions for individual users and groups in addition to the traditional owner-group-others model.

ACL Commands

View ACLs with getfacl:

getfacl myfile.txt

Enter fullscreen mode Exit fullscreen mode

Set ACLs with setfacl:

setfacl -m u:username:rwx myfile.txt

Enter fullscreen mode Exit fullscreen mode

🎯 Setting ACLs on a Directory

Let’s create a directory and assign specific ACL permissions:

mkdir mydirectory
setfacl -m g:developers:rwx mydirectory
getfacl mydirectory
Enter fullscreen mode Exit fullscreen mode

In this example, the developers group is granted full access to the mydirectory. ACLs provide flexibility in managing permissions across multiple users and teams, which is especially useful in collaborative environments.

πŸ› οΈ Additional Automation Tasks

πŸ”„ Task: Script to Modify File Permissions

When dealing with multiple files, automating permission changes can save significant time. Here’s a script that modifies permissions for all files in a directory based on user input:

#!/bin/bash
echo "Enter directory path:"
read dir
echo "Enter permissions (e.g., 755):"
read perm
chmod -R $perm $dir
echo "Permissions updated for all files in $dir"
Enter fullscreen mode Exit fullscreen mode

To Run the Script:

chmod +x change_permissions.sh
./change_permissions.sh

Enter directory path:
myfile
Enter permissions (e.g., 755):
000
Permissions updated for all files in myfile
Enter fullscreen mode Exit fullscreen mode

πŸ”’ Task: Script to Set ACL for a Specific User

This script allows you to set ACL permissions for a user on a specific file based on user input:

#!/bin/bash
echo "Enter file path:"
read file
echo "Enter username:"
read user
echo "Enter permissions (e.g., rwx):"
read perm
setfacl -m u:$user:$perm $file
echo "ACL set for $user on $file"
Enter fullscreen mode Exit fullscreen mode

To Run the Script:

chmod +x set_acl.sh
./set_acl.sh

Enter fullscreen mode Exit fullscreen mode

🚦 Understanding Sticky Bit, SUID, and SGID

🧷 Sticky Bit: Restricts file deletion in a directory to the owner. It's often used in shared directories like /tmp.

Example:

chmod +t /tmp

Enter fullscreen mode Exit fullscreen mode

πŸ” SUID (Set User ID): Allows a file to be executed with the permissions of the file owner rather than the user running the file.

Example:

chmod u+s myscript.sh

Enter fullscreen mode Exit fullscreen mode

πŸ‘₯ SGID (Set Group ID): Files created in a directory inherit the group of the directory, ensuring that group ownership is maintained.

Example:

chmod g+s /sharedfolder

Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Backup and Restore Permissions

Sometimes, you may need to back up file permissions before making changes. The following scripts will back up and restore permissions using ACL:

πŸ—„οΈ Backup Script:

#!/bin/bash
echo "Enter directory to backup permissions:"
read dir
getfacl -R $dir > permissions_backup.acl
echo "Permissions backed up to permissions_backup.acl"
Enter fullscreen mode Exit fullscreen mode

To Run the Script:

chmod +x backup_permissions.sh
./backup_permissions.sh
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Restore Script:

#!/bin/bash
echo "Enter directory to restore permissions:"
read dir
setfacl --restore=permissions_backup.acl
echo "Permissions restored from permissions_backup.acl"
Enter fullscreen mode Exit fullscreen mode

To Run the Script:

chmod +x restore_permissions.sh
./restore_permissions.sh
Enter fullscreen mode Exit fullscreen mode

πŸš€Wrapping It Up 🎁
And there you have it, folks! You're now armed with the knowledge to bend file permissions to your will. Remember, with great power comes great responsibility (and fewer "Permission denied" errors).
Keep experimenting, stay curious, and may your systems always be secure! πŸ”πŸ’ͺ

DevOps #Linux #FilePermissionNinja #SecuritySuperhero #BashScriptingWizard

P.S. Did this guide help you level up your DevOps game? Share it with your fellow tech adventurers and spread the file permission love! πŸ’–

Top comments (0)