Day 6 of #90daysofdevops File Permissions and Access Control Lists

Mudit Mathur
5 min readJul 25, 2023

--

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:

  1. Owner (user) ๐Ÿ‘ค: The user who creates the file or owns it.
  2. Group ๐Ÿ‘ฅ: A set of users who share the same access permissions for the file.
  3. Others ๐Ÿ‘ค๐Ÿ‘ฅ: All users not included in the owner or group category.

๐Ÿ”‘ Each category can be assigned three types of permissions:

  1. Read (r) ๐Ÿ”: Users with read permission can view the content of a file or the list of a directory.
  2. Write (w) โœ๏ธ: Users with write permission can modify or delete files and directories.
  3. 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. ๐Ÿš€๐Ÿ”’

--

--

Mudit Mathur
Mudit Mathur

No responses yet