Linux Permissions

Linux permissions are an important part of managing files and directories in a Linux system. They control who can access, modify, and execute files and directories, and they are essential for maintaining the security and integrity of your system. In this tutorial, we will cover the basics of Linux permissions and how to manage them.

Understanding Linux Permissions

Linux permissions are divided into three types of access: read, write, and execute. These permissions are assigned to three categories of users: the file or directory owner, the group to which the file or directory belongs, and all other users.

Each file or directory in Linux has three sets of permissions: one for the owner, one for the group, and one for everyone else. These permissions can be represented by a series of numbers, where each digit represents a different type of access. The first digit represents the permissions for the owner, the second digit represents the permissions for the group, and the third digit represents the permissions for everyone else.

The numbers represent the following permissions:

  • 0: no permissions
  • 1: execute only
  • 2: write only
  • 3: write and execute
  • 4: read only
  • 5: read and execute
  • 6: read and write
  • 7: read, write, and execute

For example, a file with permissions of 644 would mean that the owner has read and write permissions, while the group and everyone else has only read permissions.

Managing Linux Permissions

There are several commands in Linux that can be used to manage permissions. Here are some of the most common ones:

chmod

The chmod command is used to change the permissions of a file or directory. The basic syntax for the command is as follows:

chmod [permissions] [file/directory]

For example, to give the owner read, write, and execute permissions and everyone else no permissions, you would use the following command:

chmod 700 file.txt

Another example, if you want to give the owner read, write, and execute permissions, and give the group and all other users only read and execute permissions, you would use the following command:

chmod 755 file.txt

Here is a breakdown of what each number means:

  • 7: Read, write, and execute permissions (4 + 2 + 1)
  • 5: Read and execute permissions (4 + 1)

You can also use the chmod command to modify the ownership and group of a file using the chown and chgrp commands, respectively.

chown

The chown command is used to change the owner of a file or directory. The basic syntax for the command is as follows:

chown [new owner] [file/directory]

For example, to change the owner of a file named “file.txt” to a user named “bob”, you would use the following command:

chown bob file.txt
chgrp

The chgrp command is used to change the group of a file or directory. The basic syntax for the command is as follows:

chgrp [new group] [file/directory]

For example, to change the group of a file named “file.txt” to a group named “users”, you would use the following command:

chgrp users file.txt

ls

The ls command is used to display the permissions of a file or directory. The basic syntax for the command is as follows:

ls -l [file/directory]

For example, to display the permissions of a file named “file.txt”, you would use the following command:

ls -l file.txt

This will show the permissions in the following format:

-rw-r--r-- 1 user group 0 Jan 1 00:00 file.txt

The first set of characters (“-rw-r–r–“) represents the permissions for the owner, group, and everyone else, respectively.

Linux File Permissions Chart

To help you understand Linux file permissions better, here is a chart that shows the different combinations of read, write, and execute permissions:

PermissionsOwnerGroupOther
rwxX
rw-XX
r-xXX
r–XXX
-wx
-w-X
–xX

In this table, X represents a permission that is set to “on”, while a hyphen (-) represents a permission that is set to “off”. The permissions are broken down into three groups: the owner of the file, the group that the file belongs to, and all other users. For example, if a file has the permissions “rw-r–r–“, this means that the owner of the file has read and write permissions, while the group and all other users have only read permissions.

Also, see the example code shell-scripting-examples in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media
Follow Author