Day 6 Task: File Permissions and Access Control ListsπŸ“ƒ

Manav RautManav Raut
5 min read

Task 1: Understanding File Permissions πŸ“‚πŸ”

Create a simple file and run ls -ltr to see the details of the files.

  • Each of the three permissions is assigned to three specific categories of users. The categories are:

    • Owner: The person who owns the file or application. πŸ‘€

      • Use chown to change the ownership of a file or directory.
    • Group: The group that owns the file or application. πŸ‘₯

      • Use chgrp to change the group ownership of a file or directory.
    • Others: All users who have access to the system, excluding those in the group. 🌐

      • Use chmod to change the permissions for other users on a file or directory.
  • Task: Change the user permissions of the file and note the changes after running ls -ltr. βœοΈπŸ“„

a. Create a file and view details: πŸ“„πŸ”


   touch myfile.txt  # Create a simple file
   ls -ltr myfile.txt  # View detailed permissions and metadata

You'll see an output like this:

You'll see an output like this:

-rw-r--r-- 1 user group 0 Oct 14 10:00 myfile.txt

  • First character (-): Indicates the file type (- for regular file, d for directory).

  • Next nine characters (rw-r--r--): Indicate permissions for owner, group, and others (in that order).

    • r (read), w (write), x (execute).

b. Change ownership (chown): πŸ”„πŸ‘€

sudo chown newuser myfile.txt  # Change the owner of the file to 'newuser'
ls -ltr myfile.txt  # Verify the changes

c. Change group ownership (chgrp): πŸ”„πŸ‘₯

sudo chgrp newgroup myfile.txt  # Change the group owner to 'newgroup'
ls -ltr myfile.txt  # Verify the changes

d. Change permissions (chmod): πŸ”§βœ¨

codechmod 764 myfile.txt  # Owner: rwx, Group: rw, Others: r
ls -ltr myfile.txt  # Verify the changes

Task 2. Writing an Article:

Understanding Linux File Permissions 🐧

In Linux, file permissions control who can access or modify files and directories. Permissions are divided into three categories: owner, group, and others. Each category can have read (r), write (w), and execute (x) permissions. πŸ”

Viewing Permissions πŸ‘€

You can view file permissions using the ls -l commandπŸ“œ:

codels -l myfile.txt

Output:

code-rw-r--r-- 1 user group 0 Oct 14 10:00 myfile.txt
  • -rw-r--r--: The first set of characters shows the permissions for owner, group, and others.

    • rw-: Owner can read and write.

    • r--: Group can read.

    • r--: Others can read.

Changing Permissions πŸ”„πŸ”

Permissions can be modified using the chmod command:

  • Symbolic mode: ✨

  •     codechmod u+x myfile.txt  # Add execute permission for the owner
    
  • Numeric mode:πŸ”’

      codechmod 764 myfile.txt  # Owner: rwx, Group: rw, Others: r
    

You can also change the ownership and group of a file:

  • chown: Change the owner. πŸ‘€πŸ”„

      codesudo chown newuser myfile.txt
    
  • chgrp: Change the group. πŸ‘₯πŸ”„

      sudo chgrp newgroup myfile.txt
    

Special Permissions: Sticky Bit, SUID, and SGID

  • Sticky Bit: 🧷 Applied to directories to prevent users from deleting files they don’t own.

  •     codechmod +t /tmp/shared_directory
    
  • SUID: Allows a file to run with the owner's privileges. πŸš€

      codechmod u+s /usr/bin/passwd
    
    • SGID: Ensures new files in a directory inherit the directory's group. πŸ“‚πŸ‘₯

        codechmod g+s /mydir
      

      Task 3. Access Control Lists (ACL): πŸ”

    • a. Reading about ACL: Access Control Lists (ACL) provide more detailed control than standard permissions by allowing specific users or groups to have customized permissions for a file or directory. πŸ“š

    • b. Commands for ACL: πŸ› οΈ

      • View ACL (getfacl): πŸ‘€
        codegetfacl myfile.txt  # View ACL entries for the file
  • Set ACL (setfacl): ✍️
        codesudo setfacl -m u:username:rwx myfile.txt  # Grant 'username' full permissions
        sudo setfacl -m g:groupname:rx myfile.txt  # Grant 'groupname' read and execute permissions
        getfacl myfile.txt  # Verify ACL entries

c. Task:

  • Create a directory and apply ACL: πŸ“‚πŸ”
        codemkdir mydir  # Create directory
        sudo setfacl -m u:anotheruser:rwx mydir  # Give 'anotheruser' full access
        getfacl mydir  # Verify ACL permissions

Task 4. Additional Tasks:

  • a. Script to change permissions of multiple files: πŸ“œπŸ”§

      code#!/bin/bash
      echo "Enter the directory path:"
      read dir
      echo "Enter permission (e.g., 755):"
      read permission
      for file in $dir/*; do
        chmod $permission $file
        echo "Changed permissions of $file to $permission"
      done
    
        codechmod +x change_permissions.sh
  • b. Script to set ACL permissions based on user input: πŸŽ›οΈπŸ“

      code#!/bin/bash
      echo "Enter filename:"
      read filename
      echo "Enter username to set ACL:"
      read username
      echo "Enter permission for ACL (e.g., rwx):"
      read permission
      sudo setfacl -m u:$username:$permission $filename
      echo "Set ACL for $username with $permission on $filename"
    
    • Save this as set_acl.sh, and run it similarly to the other script.

Task 5. Sticky Bit, SUID, and SGID:

  • Sticky bit: 🧷 Ensures only the owner of a file (or root) can delete or rename it within a shared directory.

    • Example:

      bash codemkdir /tmp/shared_directory chmod 1777 /tmp/shared_directory # Apply sticky bit

  • SUID (Set User ID): πŸš€ When an executable with SUID is run, it executes with the permissions of the file's owner.

    • Example:

        codesudo chmod u+s /usr/bin/passwd  # Example of an executable with SUID
      
  • SGID (Set Group ID): 🏷️ When SGID is set on a directory, new files inherit the directory's group.

    • Example:

        codemkdir /tmp/sgid_test
        chmod g+s /tmp/sgid_test  # Apply SGID
      

Task: Demonstrate each with the above commands and explain the use cases.


Task 6. Backup and Restore Permissions: πŸ”„πŸ’Ύ

a. Backup permissions: πŸ—„οΈβœ¨

        code#!/bin/bash
        echo "Enter directory path:"
        read dir
        getfacl -R $dir > permissions_backup.txt
        echo "Permissions backed up to permissions_backup.txt"

b. Restore permissions: πŸ”„πŸ› οΈ

        #!/bin/bash
        echo "Enter directory path:"
        read dir
        setfacl --restore=permissions_backup.txt
        echo "Permissions restored from permissions_backup.txt"

Save them as backup_permissions.sh and restore_permissions.sh, and make them executable as before.


0
Subscribe to my newsletter

Read articles from Manav Raut directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Manav Raut
Manav Raut