What are file permissions in Linux
File permission is the type of access associated with a file. Each file in Linux has its owner, a group, and permission access for three main types of users: the file owner, the group members, and others. Each of these user classes has three types of file permissions: read, write, and execute permissions. Knowing about the file permission helps you specify which users can execute, read, or write the file.
How to check file permission in Linux
The “ls” is used to check the permission of the files present on your system. To view permission of a single file, add its name to the “ls” command. For instance, we will execute the below-given command to check the file permissions of the “testfile”:
Here, the “-l” option is added to get the content of the “testfile” including its file permissions:
The first character in the output specifies the entry type where “–” represents a “file”, and “d” indicates a “directory”. Then we have, three sets of nine characters, where the first three characters set represent file owner permissions, the next characters set represent group permissions, and the last set represents permissions for other users that are not considered in the first two categories:
Each set of permissions contains three characters. If the character is a dash “–”, it indicates that access permissions are denied. Permission has been given to a user if the character is “r”, “w”, or “x”. The “r” in a permission set indicates that the user only has read permission which means that the file can be only opened and viewed, with “w,” you will have write permission for the specific file, and you will be able to edit, modify and delete the file. Lastly, the “x” letter represents the execute permissions; for instance, if your file is a C++ program or script, it will have the execute permission with the letter “x“.
If the “ls” command shows you “—” for any set, this means that no permission is granted. In another case, “rwx” indicates that all types of permission are granted, including reading, writing, and executing.
Now you have some background related to file permissions, and it will help you understand the chmod command working more efficiently.
What is chmod command in Linux
The “chmod” is an acronym for “change mode”. It modifies the access of your system directories, files, and scripts. The “chmod” command has specific modes that determine the permission for modification. These modes are represented by numerical form (letters) or symbolic form (octal numbers). When you use the chmod command with numerical form, it follows the below-given syntax:
In numerical representation, we have these options:
- “0” represents “no permission”.
- “1” represents “execute permission”.
- “2” represents “write permission”.
- “4” represents “read permission”.
If you want to use the symbolic representation, then chmod command syntax will be written as follows:
We have the following options in the symbolic form:
- “u” indicates file owner.
- “g” indicates groups.
- “o” indicates others.
- “a” indicates all users as owner, group, and others (ugo).
Whereas the chmod command accepts the following operators:
- “+”: This operator is utilized to add specified permissions.
- “–”: This operator is utilized to remove specified permissions.
- “=”: This operator is utilized to define the exact file permission for any user.
Now, let’s check out some examples of using the chmod command with symbolic form in Linux:
Example 1: Setting “read by owner only” file permission using chmod command
In this example, we will change the file permissions of “testfile” so that only the owner can read it. Other than this permission, no other group or user can read, write or execute this file. Even the owner will not have the access to execute and write something in the file. To do so, use “4” as a numerical representation of “read-only” and place it at the start of three character set, and adding “0” for the “groups” and “others” mode will not grant any permissions to those users:
Now, list the file permission system by using the “-l” option in the “ls” command:
The first “–” indicates that “testfile” is a file, and “r” shows that only the file owner has permission to read the file. You can also check that groups and other users do not have any associated permissions:
Example 2: Setting “read by group only” file permission using chmod command
Place the “4” as “group” mode between the zeroes of “owner” and “others” mode. This sequence will associate “ready by group only” permission to the file:
Again check out the changes we made into the “testfile” by using the below-given “ls” command:
Here “r” represents the “read” permission:
Similarly, you can allow the “read by others only” file permission by defining the “004” mode in the chmod command.
Example 3: Setting “write by owner only” file permission using chmod command
In numerical representation of the modes, “2” indicates the “write” permissions. Place the “2” at the start of the permission set, and add two zeros after that:
Execution of this command will only allow the file owner to write into the test file:
Confirm the changed file permission by using the “ls” command:
Here, “w” represents the “write” permission:
Similarly, you can define the “write by group only” permission with the “020” mode and “write by other only” using the “002” mode.
Example 4: Setting “execute by owner only” file permission using chmod command
In the chmod command, the “1” digit represents the execute mode. If you want to set the “execute by owner only” permission, then execute the below-given command in your terminal:
Again, list out the permission of the “testfile” by using the “ls” command:
Here, “x” represents the “execute permission”:
In the same manner, if you want to change the file permission to “execute by group only,” then define the mode as “010,” and for allowing other users to execute the file, add “001” as permission mode in the chmod command:
Example 5: Setting “read by everyone” file permission using chmod command
Using symbolic links, if you change the file permission to “read by everyone,” then execute the below-given command in your terminal:
Here “a” represents “all users”, “r” indicates “read” permissions, and the “+” operator is used to add the read permission to the specified users:
Check the changed file permission by utilizing the “ls” command:
Example 6: Setting “execute by owner” file permission using chmod command
The “u+x” permission mode indicates that “execute” permission will be granted to the file “owner”:
Now, execute the following to verify the file permission changes:
“x” in the set of owner permission shows that now the file owner has the permission to execute the file:
Setting file permission is a method to restrict the user’s access to a particular file for maintaining its security. In a Linux-based operating system, the chmod command is utilized to change the file permissions. You can easily set these file permissions by using the numeric and symbolic modes. In this article, we have demonstrated various examples of using the chmod command in Linux. We have shown you how to change file permission by utilizing the numerical and symbolic mode of the chmod command.