File Permissions and Access Control Lists

File Permissions and Access Control Lists

#90 Days of DevOps Challenge - Day 6

Table of contents

In Linux, file permissions and Access Control Lists (ACLs) are two mechanisms used to control access to files and directories. Let's discuss each of them in more detail.

File Permissions: File permissions in Linux determine the level of access that different users or groups have to a file or directory. Three types of permissions can be assigned to a file:

    • Read (r): Allows reading and viewing the contents of a file or directory.

      • Write (w): Permits modifying or deleting a file, as well as creating or deleting files within a directory.

      • Execute (x): Grants permission to execute a file (if it is a program or script) or traverse into a directory.

For each file, there are three sets of permissions: one for the owner, one for the group, and one for others. These permissions can be represented using numeric values or symbolic notation.

Numeric representation:

  • Read (r): 4

  • Write (w): 2

  • Execute (x): 1

Symbolic notation:

  • Owner: u

  • Group: g

  • Others: o

  • All: a

For example, "rw-r--r--" represents read and write permissions for the owner, and read-only permissions for the group and others.

  • I have created the file let's check the file permissions by commandls -ltr

    This is the basic file permission structure :

      1. chown (Change Owner): The chown command is used to change the owner of a file or directory. Here's the syntax:

           chown [options] <new_owner> <file_or_directory>
        
        • [options]: Additional options that can be used with chown. Some common options include -R (recursively change ownership for directories and their contents) and --from=<old_owner> (change ownership only if the current owner matches <old_owner>).

        • <new_owner>: The new owner's username or UID.

        • <file_or_directory>: The file or directory for which you want to change the owner.

Example:

            chown Dee myfile.txt

This command changes the owner of the file myfile.txt to the user john.

  1. chgrp (Change Group): The chgrp command is used to change the group ownership of a file or directory. Here's the syntax:

     chgrp [options] <new_group> <file_or_directory>
    
    • [options]: Additional options that can be used with chgrp. Similar to chown, common options include -R (recursively change group ownership) and --reference=<reference_file> (set the group to match the specified <reference_file>).

    • <new_group>: The new group name or GID.

    • <file_or_directory>: The file or directory for which you want to change the group.

Example:

            chgrp developers myfile.txt

This command changes the group ownership of myfile.txt to the group developers.

  1. chmod (Change Mode): The chmod command is used to change the permissions of a file or directory. Here's the syntax:

     chmod [options] <mode> <file_or_directory>
    
    • [options]: Additional options that can be used with chmod. Common options include -R (recursively change permissions) and --reference=<reference_file> (set permissions to match the specified <reference_file>).

    • <mode>: The new permission mode. It can be represented in numeric form (e.g., 755) or symbolic notation (e.g., u+rwx,go=rx).

    • <file_or_directory>: The file or directory for which you want to change the permissions.

Example:

            chmod 755 script.sh

This command sets the permissions of the file script.sh to read, write, and execute for the owner, and read and execute for the group and others.

These commands are powerful tools for managing file ownership, group ownership, and permissions in Linux systems. Remember to use them with caution, as incorrect usage can lead to unintended consequences.

Access Control List (ACL)

The setfacl and getfacl commands in Linux are used to manage and display Access Control Lists (ACLs) for files and directories. They provide an extended level of access control beyond the traditional file permissions. Let's explore each command in more detail:

  1. setfacl (Set File ACL): The setfacl command allows you to set ACL entries for a file or directory. Here's the syntax:

     setfacl [options] <acl_entries> <file_or_directory>
    
    • [options]: Additional options that can be used with setfacl. Common options include -m (modify the ACL entries) and -x (remove the specified ACL entries).

    • <acl_entries>: The ACL entries you want to set, in the form of <user_or_group>:<permissions>. Multiple entries can be separated by commas.

    • <file_or_directory>: The file or directory for which you want to set the ACL.

Example:

    setfacl -m u:john:rwx,g:developers:r myfile.txt

This command adds ACL entries to myfile.txt, granting read, write, and execute permissions to the user John and read-only permissions to the group developers.

  1. getfacl (Get File ACL): The getfacl command is used to display the ACL entries for a file or directory. Here's the syntax:

     getfacl [options] <file_or_directory>
    
    • [options]: Additional options that can be used with getfacl. Common options include -R (recursively display ACLs for directories and their contents) and -p (display the default ACL entries).

    • <file_or_directory>: The file or directory for which you want to retrieve the ACL.

Example:

ACLs provide a more flexible way to define access control on files and directories by allowing specific permissions for individual users and groups. They are an optional feature in Linux and require a file system that supports ACLs, such as ext4 or XFS.

Please note that manipulating ACLs requires appropriate permissions and privileges. Incorrect usage of setfacl can lead to unintended access control changes, so it's essential to exercise caution when using these commands.

Thanks for reading! I hope you have a wonderful day. I'd love to hear your thoughts on this topic. Please leave a comment below. If you found this post helpful, please consider subscribing to my blog."

Thanks for reading! I'll see you next time. !!!!!