Linux File Permissions

Linux File Permissions

ยท

3 min read

Hey there, fellow tech enthusiasts! Today, we're going to dive into the intriguing world of Linux File Permissions and Ownership. ๐Ÿง

So, what's all this fuss about permissions and ownership? In Linux, every file and directory comes with a set of access rights that determine who can do what with it. These access rights are categorized into three groups:

  1. Owner ๐Ÿ‘‘: The owner of the file or application. This is the individual who created the file or the user who currently holds it.

  2. Group ๐Ÿค: The group that owns the file or application. Groups allow multiple users to share common permissions on files, making it easier to manage access.

  3. Others ๐ŸŒ: Everyone else on the system who is not the owner or part of the group. These users have the least privileged access.

    Now, let's get hands-on and play with some commands! ๐Ÿ˜Ž

    Changing Ownership and Group ๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘๐Ÿง‘โ€๐Ÿ‘งโ€๐Ÿ‘ง

    Suppose we have a directory named "project_files" with its current owner as "Shubham" and group as "Trainwithshubham." Now, we want to change the ownership to "aryan" and the group to "Explorewitharyan."

      $ ls -l project_files
      drwxr-xr-x 3 Shubham Trainwithshubham 4096 Jul 20 12:00 project_files
    
      $ sudo chown aryan:Explorewitharyan project_files
      $ ls -l project_files
      drwxr-xr-x 3 aryan Explorewitharyann 4096 Jul 20 12:00 project_files
    

    Now, "aryan" is the owner, and "Explorewitharyan" is the new group for the "project_files" directory.

    ๐Ÿ˜Ž Group Permission: The group refers to a collection of users who share common access rights to the file. When a file is created, it inherits the group of the user who created it. The chgrp command is used to change the group ownership of a file:

      chgrp new_group file.txt
    

    To change permissions for the owner, group, and others simultaneously, we can use a shorthand notation with chmod. For instance:

      chmod u=rw,g=r,o=r file.txt
    

    This command grants read and write permissions to the owner, read permissions to the group, and read permissions to others.

    Changing Permissions Numerically

    Suppose we have a script called "my_script.sh" with the following permissions:

      $ ls -l my_script.sh
      -rwxr-xr-- 1 sana group3 1024 Jul 20 14:30 my_script.sh
    

    We want to give the owner full access, the group read and execute permissions, and others no permissions.

      $ chmod 750 my_script.sh
      $ ls -l my_script.sh
      -rwxr-x--- 1 sana group3 1024 Jul 20 14:30 my_script.sh
    

    Now, the owner has read, write, and execute permissions (7), the group has read and execute permissions (5), and others have no permissions (0).

    Here's the breakdown of the numeric codes:

    • 7 (Owner): Read (4) + Write (2) + Execute (1)

    • 5 (Group): Read (4) + Execute (1)

    • 0 (Others): (0)

Adding and Removing Permissions

You have a script named "my_script.sh," and you want to allow the owner to execute it and grant read and execute permissions to the group while removing all permissions from others.

    bashCopy code$ chmod u+x my_script.sh    # Add execute permission for the owner
    $ chmod g+rx my_script.sh   # Add read and execute permissions for the group
    $ chmod o-rwx my_script.sh  # Remove all permissions from others

๐Ÿ˜Ž Remember, understanding these concepts is vital for maintaining data security and access control on your system. So, keep exploring, and don't forget to have fun with those Linux commands! ๐Ÿš€๐Ÿง๐Ÿ’ป

Hope you like my blog...!

If you like the content follow me on LinkedIn: linkedin.com/in/aryankale

Happy Learning ๐Ÿ˜Š

ย