Day 6 : File Permissions and Access Control Lists

Day 6 : File Permissions and Access Control Lists

ยท

4 min read

Understanding File Permissions in Linux ๐Ÿง๐Ÿ”

Hey there, DevOps enthusiasts! ๐Ÿ‘‹

Welcome to another exciting day in our 90-day DevOps journey. Today, we're diving into the world of file permissions in Linux. Understanding and managing file permissions is crucial for system security and efficient collaboration. Let's get started!

Introduction to File Permissions ๐Ÿ“๐Ÿ”

File permissions in Linux control who can read, write, or execute a file. These permissions are assigned to three categories of users:

  1. Owner: The user who owns the file.

  2. Group: A group of users that the file belongs to.

  3. Others: All other users on the system.

Each category can have three types of permissions:

  • Read (r): Permission to read the file.

  • Write (w): Permission to modify the file.

  • Execute (x): Permission to execute the file as a program.

Task 1: Creating a File and Viewing Permissions ๐Ÿ“๐Ÿ‘€

Let's create a simple file and view its permissions.

Commands:

touch practice.txt
ls -ltr practice.txt

The touch command creates an empty file named myfile.txt. The ls -ltr command lists the file details, showing the permissions in the leftmost column.

Task 2: Changing Ownership and Group Permissions ๐Ÿ”„๐Ÿ‘ฅ

You can change the ownership and group of a file using chown and chgrp.

Commands:

# Change ownership to user 'newowner'
sudo chown newowner practice.txt

# Change group to 'devgroup'
sudo chgrp newgrp practice.txt

After running these commands, use ls -ltr to verify the changes.

Task 3: Modifying Other Users' Permissions โœ๏ธ๐Ÿ‘ฅ

The chmod command is used to change file permissions for the owner, group, and others.

Commands:

bashCopy code# Add execute permission for others
chmod o+x myfile.txt
ls -ltr myfile.txt

This command adds execute permission for other users. Use ls -ltr to see the updated permissions.

Writing an Article: Understanding File Permissions ๐Ÿ“„๐Ÿ“

File permissions are fundamental to Linux security. They ensure that only authorized users can access or modify files, preventing unauthorized access and changes. By using commands like chown, chgrp, and chmod, you can control who can read, write, or execute your files, providing a secure and organized system.

Task 4: Access Control Lists (ACL) ๐Ÿ“œ๐Ÿ”’

ACLs provide a more flexible permission mechanism than the standard file permissions.

Commands:

Task 5: Script to Change Permissions of Multiple Files ๐Ÿ“‚๐Ÿ”„

Let's write a script that changes the permissions of multiple files in a directory based on user input.

Script: change_permissions.sh

#!/bin/bash

read -p "Enter directory path: " dir_path
read -p "Enter permission (e.g., 755): " permission

for file in "$dir_path"/*; do
    chmod "$permission" "$file"
    echo "Changed permissions of $file to $permission"
done

Task 6: Script to Set ACL Permissions ๐Ÿ“œ๐Ÿ”ง

Here's a script to set ACL permissions for a user on a given file.

Script: set_acl.sh

#!/bin/bash

read -p "Enter file path: " file_path
read -p "Enter username: " username
read -p "Enter permissions (e.g., rw): " permissions

setfacl -m u:$username:$permissions $file_path
echo "Set ACL permissions of $permissions for $username on $file_path"

Task 7: Understanding Sticky Bit, SUID, and SGID ๐Ÿ“›๐Ÿ”’

  • Sticky Bit: Prevents users from deleting files they don't own in a directory. Commonly used in /tmp.

  • SUID (Set User ID): Allows users to execute a file with the permissions of the file owner.

  • SGID (Set Group ID): Allows users to execute a file with the permissions of the file group.

Examples:

# Set sticky bit
chmod +t /path/to/directory

# Set SUID
chmod u+s /path/to/file

# Set SGID
chmod g+s /path/to/file

Task 8: Backup and Restore Permissions ๐Ÿ“‚๐Ÿ”„

Backup Script: backup_permissions.sh

#!/bin/bash

read -p "Enter directory path: " dir_path
backup_file="permissions_backup.txt"

getfacl -R $dir_path > $backup_file
echo "Permissions backed up to $backup_file"

Restore Script: restore_permissions.sh

#!/bin/bash

read -p "Enter backup file path: " backup_file

setfacl --restore=$backup_file
echo "Permissions restored from $backup_file"

Conclusion ๐ŸŽ‰

Understanding and managing file permissions in Linux is vital for system security and collaboration. Today, we explored how to view and modify file permissions, use ACLs for fine-grained control, and create scripts to automate these tasks. We also learned about sticky bit, SUID, and SGID, and created scripts for backing up and restoring permissions.

Keep experimenting and practicing these skills to become proficient in managing file permissions.

ย