Day 6 of #90daysofdevops File Permissions and Access Control Lists
TABLE OF CONTENTS
๐ Introduction
๐ File Permissions Overview
๐ Task 1: Change the Permission of file/directories
๐ Task 2: Change the ownership of a file/directory
๐ฅ Task 3: Change the group permission of a file/directory
๐ Access Control Lists (ACL) commands getfacl and setfacl
๐ฏ Conclusion
๐ Introduction
Welcome to Day 6 of the #90DaysOfDevOps challenge. In this blog, weโll explore File Permissions and Ownership in Linux, making it simple to understand! Weโll learn how to modify permissions and ownership, and even dive into Access Control Lists (ACL) using commands like โgetfaclโ and โsetfacl.โ Letโs unlock the secrets of secure file management! ๐๏ธ๐
๐ File Permissions Overview
In Linux, file permissions dictate who can access, modify, and execute files and directories. They are crucial for ensuring security and control over sensitive data and system resources.
Three categories of users can have distinct permissions for a file:
- Owner (user) ๐ค: The user who creates the file or owns it.
- Group ๐ฅ: A set of users who share the same access permissions for the file.
- Others ๐ค๐ฅ: All users not included in the owner or group category.
๐ Each category can be assigned three types of permissions:
- Read (r) ๐: Users with read permission can view the content of a file or the list of a directory.
- Write (w) โ๏ธ: Users with write permission can modify or delete files and directories.
- Execute (x) ๐โโ๏ธ: Users with execute permission can run executable files or access directories to list their contents.
๐ข File permissions are represented using a three-character string for each category. For example, โrw-r โ r โ โ means the owner has read and write permissions, while the group and others have only read permissions.
๐ To view and modify file permissions, you can use the ls -l
command to display the permissions for files and directories in the current directory. To change permissions, you can use the chmod
command, followed by the desired permission code and the filename.
It is crucial to set appropriate file permissions to prevent unauthorized access to sensitive files and maintain the integrity of the system. Always exercise caution when modifying file permissions, as improper settings can lead to security risks and system vulnerabilities.
๐ Task 1: Change the Permission of file/directories
In Linux, when we want to modify file or directory permissions, we use the chmod
command.
There are two ways to change permissions: the Symbolic method and the Absolute method. ๐
Symbolic method (ugo):
- โuโ stands for User
- โgโ stands for Group
- โoโ stands for Other
For example, if a manager asks us to add execute permission for the user, add write permission for the group, and remove read permission for others, and to verify whether permission is changed or not use the following command:
chmod u+x, g+w, o-r file.txt
ls -l file.txt
Absolute method:
Here we use numbers to set permissions for a file or directory. ๐งฎ
Hereโs the numeric mapping:
- 4 stands for Read ๐
- 2 stands for Write โ๏ธ
- 1 stands for Execute ๐โโ๏ธ
For example, if we want to set the permissions to read, write, and execute for the owner, read and write for the group, and only read for others, we can use the following command:
chmod 632 test.txt
ls -l file.txt
Using numbers in the Absolute method provides a quick and precise way to manage permissions in Linux!
๐ Task 2: Change the ownership of a file/directory
In Linux, you can change the ownership of a file using the chown
command, which stands for "change owner." Only the root user can perform this action.
For example, to change the owner of file.txt to ubuntu, you can use the following command:
sudo chown ubuntu file.txt
ls -l file.txt
After executing the command, the ubuntu user becomes the owner of the file.txt file.
๐ฅ Task 3: Change the group permission of a file/directory
In Linux, you can alter the group ownership of a file or directory using the chgrp
command. This task is exclusively restricted to the root user, meaning only the superuser can execute this command.
Example: To illustrate, consider the following command:
chgrp ubuntu devtxt.txt
ls -l file.txt
With this command, the group ownership of the file named file.txt is changed to โubuntu.โ However, keep in mind that only the root user or a user with equivalent administrative privileges can successfully perform this action. ๐ก๏ธ๐ป
๐ Access Control Lists (ACL) commands getfacl and setfacl
Access Control Lists (ACLs) give precise control over file permissions. Unlike regular permissions (owner, group, and others), ACL lets you set specific access for users or groups.
๐ก Two helpful commands for ACL are getfacl (๐) to view ACL settings and setfacl (๐ ๏ธ) to modify entries.
To see ACL settings of a file, use:
getfacl file.txt
Note: To use ACL, install it using sudo apt install acl
.
To change the ACL entries and give particular permissions to users or groups, utilize the setfacl command. For example, to grant read, write, and execute permissions to a user:โจ
sudo setfacl -m g::r--,o::r-- file.txt
getfacl file.txt
ACLs offer a cool way to handle file permissions, especially in tricky situations where you want to give certain people or groups special access.
๐ฏ Conclusion
Congratulations on completing Day 5 of the #90DaysOfDevOps challenge. ๐ Today, we explored file permissions are essential for secure and controlled access to files and directories in Linux. Throughout this overview, we explored tasks related to changing permissions, ownership, and group permissions of files and directories. Additionally, we learned about Access Control Lists (ACL) and the useful commands getfacl
and setfacl
for finer control over permissions. With this knowledge, you can confidently manage file access and ensure data security in your Linux environment. ๐๐