Table of contents
- Understanding File Permissions in Linux ๐ง๐
- Introduction to File Permissions ๐๐
- Task 1: Creating a File and Viewing Permissions ๐๐
- Task 2: Changing Ownership and Group Permissions ๐๐ฅ
- Task 3: Modifying Other Users' Permissions โ๏ธ๐ฅ
- Writing an Article: Understanding File Permissions ๐๐
- Task 4: Access Control Lists (ACL) ๐๐
- Task 5: Script to Change Permissions of Multiple Files ๐๐
- Task 6: Script to Set ACL Permissions ๐๐ง
- Task 7: Understanding Sticky Bit, SUID, and SGID ๐๐
- Task 8: Backup and Restore Permissions ๐๐
- Conclusion ๐
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:
Owner: The user who owns the file.
Group: A group of users that the file belongs to.
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.