# Day 06: Mastering File Permissions, ACLs, and More in Linux!

Welcome to Day 06 of my #90DaysOfDevOps challenge! Today, I explored one of the most essential aspects of Linux: file permissions, Access Control Lists (ACLs), and special bits like Sticky Bit, SUID, and SGID. I also implemented scripts for automation and wrote about the significance of permissions in system administration. Here's a comprehensive look at what I achieved today.

---

## **Understanding File Permissions**

Linux assigns permissions to files and directories to ensure security and controlled access. These permissions are divided into three categories:

1. **Owner**
    
    * The user who owns the file.
        
    * Use the `chown` command to change ownership of files or directories.  
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732811457029/9467884a-d68c-4243-9f42-5c30b13a178a.png align="center")
        
2. **Group**
    
    * The group assigned to the file.
        
    * Use `chgrp` to modify the group ownership.  
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732811470829/ad02e646-7ece-4b04-9518-86c598328b44.png align="center")
        
3. **Others**
    
    * All users outside the owner and group.
        
    * Use `chmod` to update permissions for others.  
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732811488950/d7878d32-9127-4a2c-bed3-137cb9bd17ce.png align="center")
        

After updating permissions, the results can be verified using `ls -ltr`.  

\- Task: Change the user permissions of the file and note the changes after running `ls -ltr`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732811611053/47a8d3d0-9b34-49a5-830b-945bdc192c81.png align="center")

---

## **Access Control Lists (ACLs)**

ACLs provide fine-grained permissions for users and groups beyond the traditional file permissions.

### **Commands**:

* `getfacl`: View ACLs on a file or directory.
    
* `setfacl`: Modify ACLs for a file or directory.
    

I referred to [this guide](https://www.geeksforgeeks.org/access-control-listsacl-linux/) to deepen my understanding.

### **Practical Task**:

* Created a directory and assigned specific ACL permissions to users and groups.
    
* Verified the permissions using `getfacl`.  
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732811660237/59a4820b-a132-4201-8daf-6222d1f183d3.png align="center")

---

## **Automation with Scripts**

### 1\. **Changing Permissions for Multiple Files**

I wrote a script to change permissions for all `.txt` files in a given directory based on user input.

**Script:**

```bash
#!/bin/bash

<<Info
Author      : Amitabh Soni
Date        : 28/11/24
Description : This script changes the permissions of all .txt files in a directory based on user input.
Info

read -p "Enter the directory name: " dir_name
read -p "Enter permission value (numeric, e.g., 755): " num_value_per

if [[ ! -d "$dir_name" ]]; then
  echo "Error: Directory '$dir_name' does not exist."
  exit 1
fi

txt_files=("$dir_name"/*.txt)
if [[ ! -e "${txt_files[0]}" ]]; then
  echo "Error: No .txt files found in directory '$dir_name'."
  exit 1
fi

sudo chmod "$num_value_per" "$dir_name"/*.txt
if [[ $? -eq 0 ]]; then
  echo "Permissions changed successfully for all .txt files in '$dir_name'."
else
  echo "Failed to change permissions for .txt files in '$dir_name'."
fi
```

**Output:**  
Successfully updated permissions for `.txt` files.  

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732811713580/3edf0c26-85f1-4f3f-b3d2-89eea997b929.png align="center")

### 2\. **Setting ACL Permissions via Script**

This script sets ACL permissions for a user on a specified file.

**Script:**

```bash
#!/bin/bash

<<Info
Author      : Amitabh Soni
Date        : 28/11/24
Description : This script sets ACL permissions for a user on a given file, based on user input.
Info

read -p "Enter username: " user
read -p "Enter a file path: " file_path
read -p "Enter the permission for user (e.g., rwx): " per

if [[ ! -e "$file_path" ]]; then
   echo "Error: File '$file_path' does not exist."
   exit 1
fi

setfacl -m u:$user:$per "$file_path"
if [[ $? -eq 0 ]]; then
   echo "ACL permissions set successfully for user '$user' on file '$file_path'."
else
   echo "Failed to set ACL permissions for user '$user' on file '$file_path'."
fi
```

**Output:**  
ACL permissions set successfully.  

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732811748533/643a02d5-895f-467c-9135-ee4fcb171707.png align="center")

---

## **Special Permissions: Sticky Bit, SUID, SGID**

1. **Sticky Bit**  
    Prevents users from deleting files they do not own in shared directories.  
    
2. **SUID (Set User ID)**  
    Runs executables with the permissions of the file owner.  
      
    
3. **SGID (Set Group ID)**  
    Runs executables with the permissions of the group owner.  
    

---

## **Backing Up and Restoring Permissions**

### **Backup Permissions**

This script saves the current ACLs of a directory to a file.

**Script:**

```bash
#!/bin/bash

<<Info
Author      : Amitabh Soni
Date        : 28/11/24
Description : Creating a script that backs up the current permissions of files in a directory to a file.
Info

read -p "Enter the directory path to backup permissions: " dir_path

if [[ ! -d "$dir_path" ]]; then
   echo "Error: The directory '$dir_path' does not exist."
   exit 1
fi

getfacl -R "$dir_path" > permission_backup.txt
if [[ $? -eq 0 ]]; then
   echo "Permissions backed up successfully to permission_backup.txt"
else
   echo "Failed to backup permissions."
fi
```

**Output:**  
Permissions backed up successfully.  

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732811796992/bceb2ad6-3c25-4a8c-94c2-d40d1ac760d9.png align="center")

### **Restore Permissions**

This script restores permissions from a backup file.

**Script:**

```bash
#!/bin/bash

<<Info
Author      : Amitabh Soni
Date        : 28/11/24
Description : Script to restore file permissions from a backup file.
Info

read -p "Enter the backup file path: " backup_file

if [[ ! -f "$backup_file" ]]; then
   echo "Error: The backup file '$backup_file' does not exist."
   exit 1
fi

setfacl --restore="$backup_file"
if [[ $? -eq 0 ]]; then
   echo "Permissions restored successfully from $backup_file"
else
   echo "Failed to restore permissions."
fi
```

**Output:**  
Permissions restored successfully.  

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732811811313/e56b7bd9-0d10-4f13-b243-150b584596ad.png align="center")

---

### **Conclusion**

Today’s tasks were an insightful journey into Linux file permissions and ACLs. The practical knowledge gained will be invaluable for managing secure environments in real-world DevOps scenarios.

Let me know your thoughts, suggestions, or queries in the comments! 🚀
