# Day 7: Linux  -  File System Permissions

File permissions are essential for maintaining the security and access control of files and directories on Unix/Linux systems. Each file and directory has 10 bits that define its permissions. These permissions are divided into three roles: Owner, Group, and Others. Let's break down the key concepts and how to set file permissions.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689957362118/ab6f0600-1f14-41df-bcd3-74f0f3b9d9a6.png align="center")

1. **File Types and Permission Bits:** When you run the `ls -l` command in the terminal, you'll see a file's permissions displayed as a series of ten characters, with the first character indicating the file type and the next nine characters showing the permissions. The nine characters are divided into three groups of three, representing the permissions for Owner, Group, and Others, respectively.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689956754835/102485f7-c867-4563-b83f-6b369c02376b.png align="center")
    
2. **Roles:**
    
    * Owner (User): The user who owns the file or directory.
        
    * Group: All the users who belong to the group associated with the file or directory.
        
    * Others: All the other users on the system who are not the Owner or part of the Group.
        
3. **Permissions:** Each role (Owner, Group, Others) has three types of permissions: Read, Write, and Execute.
    
    * Read (r): Allows reading/viewing the content of a file or listing files in a directory.
        
    * Write (w): Allows modifying the content of a file or creating, renaming, and deleting files in a directory.
        
    * Execute (x): Allows executing a file (like a shell script) or entering a directory and accessing files inside it.
        
4. **Ways to Change File Permissions in Linux:** You can use the `chmod` command to change file permissions in two ways:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689959174367/8db7412a-3095-48e4-935d-12528b77476d.png align="center")
    
    **a) Symbolic Notation:**
    
    * User (u): Use `u` to represent the owner's permissions.
        
    * Group (g): Use `g` to represent the group's permissions.
        
    * Others (o): Use `o` to represent the permissions for others.
        
    * `+` adds permission, `-` removes permission, and `=` sets the exact permissions.
        
        **Example 1: Using Symbolic Notation** Let's say we have a file named `example.txt`, and we want to set the permissions to allow the owner to read, write, and execute the file, the group to read and execute it, and others only to read it.
        
        1. Check the current permissions of the file using `ls -l`:
            
            Output:
            
            ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689957686574/f5f2b229-db55-42d8-8ba9-dafe53d01d52.png align="center")
            
        2. Now, let's change the permissions using symbolic notation:
            
            ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689957824822/6355b800-7760-4a90-87fc-bb114e757012.png align="center")
            
            This command sets the following permissions:
            
            * Owner (user): read (r), write (w), execute (x)
                
            * Group: read (r), execute (x)
                
            * Others: read (r)
                
        3. Verify the changed permissions using `ls -l` again:
            
            Output
            
            ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689957873458/e73dac9c-5696-45f3-90d0-eca8b5c2fbfb.png align="center")
            
    
    **b) Octal Notation:**
    
    * Read (r): Represented by the number 4.
        
    * Write (w): Represented by the number 2.
        
    * Execute (x): Represented by the number 1.
        
    
    For example:
    
    * 7 (rwx): Read, Write, and Execute permissions for Owner.
        
    * 6 (rw-): Read and Write permissions for Group.
        
    * 5 (r-x): Read and Execute permissions for Others.
        
    * 4 (r--): Only Read permission for everyone.
        
        **Example 2: Using Octal Notation** In this example, we'll change the permissions of the same `example.txt` file to allow the owner to read and write, the group to have no permissions, and others to have only read access.
        
        1. Check the current permissions of the file using `ls -l`
            
            Output :
            
            ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689958090420/badb8a3c-d7be-4411-b399-c853e174439b.png align="center")
            
        2. Now, let's change the permissions using the octal notation:
            
            ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689958182609/33633ca7-d806-449c-b7e9-5e257e1b7181.png align="center")
            
            Here, the octal number `644` represents the following permissions:
            
            * Owner (user): read (4), write (2)
                
            * Group: read (4)
                
            * Others: read (4)
                
        3. Verify the changed permissions using `ls -l` again
            
            Output :
            
            ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689958252095/8104c776-e99f-4c37-a82c-28ab2f0bf4a8.png align="center")
            

Using these methods, you can easily manage file permissions to control who can read, write, and execute files and directories on your Linux system, thereby enhancing security and access control.

Managing File Permissions and Ownership in Linux: chmod, chown, and chgrp

**1.** `chmod` - Change File Permissions `chmod` is a command used in Unix/Linux systems to change the permissions of files and directories. It allows you to grant or revoke read, write, and execute permissions for the owner, group, and others.

**chmod - Change File Permissions:**

1. Grant read, write, and execute permissions to the owner of the file:
    
    ```plaintext
    chmod u+rwx file.txt
    ```
    
2. Revoke write permission from the group:
    
    ```plaintext
    chmod g-w file.txt
    ```
    
3. Grant read and execute permissions to others, while keeping owner's permissions unchanged:
    
    ```plaintext
    chmod o+rx file.txt
    ```
    
4. Remove execute permission for everyone (owner, group, others):
    
    ```plaintext
    chmod a-x file.txt
    ```
    

**2.** `chown` **\-** Change File Ownership `chown` is a command used to change the owner of a file or directory. Only the superuser (root) or the current owner of the file can change its ownership.  
**chown - Change File Ownership:**

1. Change the owner of a file to a specific user:
    
    ```plaintext
    chown newuser file.txt
    ```
    
2. Change the owner of a directory and its contents recursively:
    
    ```plaintext
    chown -R newuser directory/
    ```
    
3. Change the owner and group of a file:
    
    ```plaintext
    chown newuser:newgroup file.txt
    ```
    

**3.** `chgrp` - Change Group Ownership `chgrp` is a command used to change the group ownership of a file or directory. Similar to `chown`, only the superuser or the current owner of the file can change its group ownership.

**chgrp - Change Group Ownership:**

1. Change the group ownership of a file:
    
    ```plaintext
    chgrp newgroup file.txt
    ```
    
2. Change the group ownership of a directory and its contents recursively:
    
    ```plaintext
    chgrp -R newgroup directory/
    ```
    
3. Change both the owner and group of a file simultaneously:
    
    ```plaintext
    chown newuser:newgroup file.txt
    ```
    

Remember that changing file permissions, ownership, or group ownership should be done with caution, especially with system files and directories, as it can affect system stability and security. Always ensure that you have the necessary privileges and understand the implications of your changes before using these commands.
