Day 23 || File Ownership and Permissions in Linux | DevOps Bootcamp

Day 23 || File Ownership and Permissions in Linux | DevOps Bootcamp

Introduction

we will explore how to change file ownership, recursively modify ownership for directories and their contents, and set file permissions for secure file management in Linux.

Section 1: Changing File Ownership

Step 1: View Current Ownership

To check the current ownership of a file or directory, use the ls -l command. The output will display the owner and group of the file.

Step 2: Change Ownership

To change the ownership of a file, use the chown command, followed by the new owner's username and the file's name. For example:

sudo chown new_owner: new_file.txt

Section 2: Recursively Changing Ownership

Step 3: Recursively Change Ownership

To change ownership recursively for a directory and its subdirectories and files, use the -R option with the chown command. For example:

sudo chown -R new_owner: directory_name

Section 3: Setting File Permissions

Step 4: View Current Permissions

To view the current file permissions, use the ls -l command again. The output displays the permissions in the format -rwxrwxrwx.

Step 5: Modify File Permissions

Use the chmod command to modify file permissions. The command syntax is chmod [options] permissions filename. For example:

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

Here, 'u' stands for the owner, 'g' for the group, and 'o' for others. 'rw' represents read and write permissions.

Step 6: Numeric Permissions

You can also use numeric values to set permissions. For example, to give read and write permission to the owner, use chmod 600 file.txt. The '6' represents read (4) and write (2) permissions.

Step 6: Octal Number Representation

In addition to the symbolic representation of permissions (e.g., 'u=rw,g=r,o=r' for read and write permissions), you can use octal numbers to set permissions. Each permission corresponds to a number:

  • 4 represents read permission.

  • 2 represents written permission.

  • 1 represents execute permission.

For example:

  • chmod 644 file.txt is equivalent to chmod u=rw,g=r,o=r file.txt. In octal notation, this translates to read (4) for the owner and read (4) for the group and others.

  • chmod 755 script.sh grants read (4), write (2), and execute (1) permissions to the owner and read (4) and execute (1) permissions to the group and others. This allows the owner to execute the script.

Using octal numbers can be more concise and efficient, especially when setting permissions for multiple files simultaneously.

Home Activity

Scenario 1: Shared Document Folder

You and your roommate share a Linux computer, and you have a folder named "SharedDocs" in your home directory where you both keep essential documents. You want to ensure that only you and your roommate have access to these documents.

  • Task: Change the ownership of the "SharedDocs" folder to your username and your roommate's username. Set permissions so that both of you have read and write access, but others have no access at all.

Scenario 1: Shared Document Folder

Step 1: Open your terminal.

Step 2: Navigate to your home directory:

cd ~

Step 3: List the contents of your home directory to confirm the current owner and permissions of the "SharedDocs" folder:

ls -l

Step 4: Identify the "SharedDocs" folder:

cd SharedDocs

Step 5: Change the ownership of the "SharedDocs" folder to your username and your roommate's username (replace your_username and roommate_username with your actual usernames):

sudo chown your_username:roommate_username .

Note the dot . at the end, which means you are changing the ownership of the current directory and its contents.

Step 6: Set the permissions on the "SharedDocs" folder so that both you and your roommate have read and write access (6 in octal) and others have no access (0 in octal):

chmod 660 .

Step 7: Verify the changes by listing the contents of the "SharedDocs" folder:

ls -l

Now, your "SharedDocs" folder is securely shared between you and your roommate with read and write access, while others have no access.

Please make sure to replace your_username and roommate_username with your actual usernames while performing these commands.

Did you find this article valuable?

Support Aqib Hafeez(DevOps enthusiast) by becoming a sponsor. Any amount is appreciated!