Chapter 5 - Linux File Permissions and Ownership

Yusuf IsahYusuf Isah
3 min read

Introduction

In Linux, permissions and ownership play a crucial role in controlling access to files and directories. Understanding permissions and ownership is essential for maintaining security and organization in your Linux system. In this chapter, we'll delve into the world of permissions and ownership, exploring how to understand, change, and manage them effectively.

Understanding Permissions

Linux permissions determine what actions users can perform on files and directories. There are three primary permissions:

  • Read (r): This allows users to view file contents or list directory contents.

  • Write (w): This enables users to modify file contents or create/delete files in a directory.

  • Execute (x): This allows users to execute files (e.g., run programs) or access directories.

These permissions are assigned to three types of users:

  • Owner: This is the user who created the file or directory.

  • Group: This refers to a group of users with shared permissions.

  • Other This refers to all other users on the system.

Permissions can also be represented using octal values:

  • r = 4

  • w = 2

  • x = 1

To see the permissions for the files and folders in your current working directory, run:

ls -la

For example, a file permission of rwxr-xr-- translates to:

  • Owner: read, write, execute (rwx)

  • Group: read, execute (r-x)

  • Others: read (r--)

Changing Permissions

The chmod command is used to change the permissions of a file or directory.

chmod: Change File Modes

To change permissions using chmod, you can use either symbolic representation or octal values.

Using Symbolic Representation

  • For example, to add execute permission for the owner, run:

    chmod u+x filename
    
  • To remove write permission for the group, run:

    chmod g-w filename
    
  • To set read and execute permissions for others, run:

    chmod o=rx filename
    

Using Octal Values

  • Set permissions to rwxr-xr-x (755)

    chmod 755 filename
    

    The rwxr-xr-x above can be broken down into three parts:

  • rwx (owner permissions)

  • r-x (group permissions)

  • r-x (others permissions)

Each part can be converted to a numerical value using the following mapping:

  • r (read) = 4

  • w (write) = 2

  • x (execute) = 1

  • - (no permission) = 0

So, let's convert each part:

  • rwx (owner) = 4 + 2 + 1 = 7

  • r-x (group) = 4 + 0 + 1 = 5

  • r-x (others) = 4 + 0 + 1 = 5

Now, combine the three numerical values into a single octal number: 755

  • Set permissions to rw-r--r-- (644):

    chmod 644 filename
    

Changing Ownership

The chown command is used to change the ownership of a file or directory. Ownership can be assigned to a user and a group.

chown: Change File Owner and Group

To change the owner and group of a file or directory, use the following syntax:

  • Change the owner of a file:

      chown username filename
    
  • Change the owner and group of a file:

    chown username:groupname filename
    
  • Change the group of a file

    chown :groupname filename
    

    For example:

  • Change the owner to Mike:

    sudo chown Mike file.txt
    
  • Change the owner to alice and the group to developers:

    sudo chown alice:developers file.txt
    

Conclusion

Understanding and managing permissions and ownership in Linux is essential for controlling access to files and directories. By using commands like chmod and chown, you can ensure that your system's security and access controls are properly maintained.

Feel free to leave comments and share this article. Follow my blog for more insights on Linux!

0
Subscribe to my newsletter

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

Written by

Yusuf Isah
Yusuf Isah

Hello. I am a DevOps enthusiast from Nigeria. I am also passionate about Technical Writing. As a passionate DevOps enthusiast, I'm dedicated to bridging the gap between development and operations teams. With a strong foundation in Linux, Git, Docker, and Kubernetes, I excel in creating efficient, scalable, and reliable software delivery pipelines. With a keen eye for detail and a passion for continuous learning, I stay up-to-date with industry trends and best practices. My goal is to collaborate with like-minded professionals, share knowledge, and drive innovation in the DevOps space. I look forward to sharing with you, all I've learned so far in my DevOps journey.