DEV Community

Yusuf Isah
Yusuf Isah

Posted on • Originally published at yuscode.hashnode.dev

Chapter 5 - Linux File Permissions and Ownership

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.

Table of Contents

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • To remove write permission for the group, run:
chmod g-w filename
Enter fullscreen mode Exit fullscreen mode
  • To set read and execute permissions for others, run:
chmod o=rx filename
Enter fullscreen mode Exit fullscreen mode

Using Octal Values

  • Set permissions to rwxr-xr-x (755)
chmod 755 filename
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Change the owner and group of a file:
  chown username:groupname filename
Enter fullscreen mode Exit fullscreen mode
  • Change the group of a file
  chown :groupname filename
Enter fullscreen mode Exit fullscreen mode

For example:

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

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!

Top comments (0)