How to Change File Permission on Linux?
In the Linux operating system, we make use of chmod command in order to change the file permission type. There are various ways to use this command so that you can change the permission of the particular file or directory. In this article, you will be knowing all the ways that you can make use of chmod command to change the file or the directory permission. It will contain the explanation, example, and syntax to use it. The general syntax to use chmod command is
- sudo chmod [option] mode file_name
- sudo chmod [option] mode directory_name
Let's try to understand the available mode in the file permission on Linux.
The mode in Linux File Permission
Basically, there are three modes in the file permission on the Linux Operating System. They are read, write and execute permission. Now all this can be done using the plain text/symbolic and octal number system. Mostly we will be using the plain text and octal number system. Before we get in-depth of each mode there is a different type of user in the Linux operating system.
Different Types of File Owner and its Denotation
Basically, there are three types of user denotations in the Linux operating system.
- Owner (u) - It is the user who is the owner of the specific file or directory. It is denoted using the symbol u(small letter u).
- Group (g) - It is the group where the individual user will be associated with. It is denoted using the symbol g(small letter g).
- Other (o) - It is the user who is not in the group. It is denoted using the symbol o(small letter o).
Now if you want to refer to all the owners at once then the denotation used is as given below.
- All (a) - It is the all denotes the all( owner, group, and other). It is denoted using the symbol a(small letter a).
Symbolic or Plain Text Mode
We make use of the text symbol for each permission. The symbols are as follows
- read - r
- write - w
- execute - x
Along with it, we make use of mathematical operators to assign file permission.
- Equal Sign(=): It will over right all the existing permission with the new one.
- Plus Sign(+): It will add specific permission to the file.
- Minus Sign(-): It will remove the specific permission from the file.
In order to see the specific file or directory permission, you can make use of the command given below.
- ls -l
It will display all the permission. For each type of file owner, we make use of three bits of space to represent the file permission.
File Permission |
Example: You have a file with name hello.c with default permission read and write to the owner, read to group and others. Now if you want to give all permission to all the users, then the command is
- sudo chmod a=rwx hello.c
Now you are having all the permission(read, write and execute) to the user, group and other. The alternative way to achive same is
- sudo chmod u=rwx,g=rwx,o=rwx hello.c
Since you are having all permission to file hello.c if you decide to remove write permission from the other, then the command used is
- sudo chmod o-w hello.c
Similarly, if you want to remove the write and execute file permission from the group the command is
- sudo chmod g-wx hello.c
Finally, if you want to add write permission back to hello.c file to the group then the command used is
- sudo chmod g+w hello.c
Octal Number System Mode
By the name only you might have got the hint how it will work. In this mode, we make use of the octal number system. The octal number system starts from 0 to 7. In the octal number system the read, write and execute permission is represented as given below.
- read - 4
- write - 2
- execute - 1
- no permission - 0
In order to give the required file permission, we do simple math addition. For example, if you want to give all read, write, and execute permission to the owner then the octal equivalent will be (4+2+1=7).
All the possible combination is given below in table for your reference.
Octal File Permission Value |
Example: Let's make use of same example as we have done with symbolic mode. Consider a file with name hello.c with default permission read and write to the owner, read to group and others. Now if you want to give all permission to all the users, then the command is
- sudo chmod 777 hello.c
Since you are having all permission to file hello.c if you decide to remove write permission from the other, then the command used is
- sudo chmod 775 hello.c
Similarly, if you want to remove the write and execute file permission from the group the command is
- sudo chmod 745 hello.c
Finally, if you want to add write permission back to hello.c file to the group then the command used is
- sudo chmod 765 hello.c
Tips: If you want to provide the same permission to its subdirectories and files inside a directory then make use of the options command in its command. For example, you are having a directory called Programming and it is having multiple subdirectories and files. Now if you want to give execute permission to others in all its subdirectories and files, the command is
- sudo chmod -R o+x Programming
Conclusion
Now you all know how to change the file permission in the Linux operating system. We make use of chmod command-line utility. It has two modes to give the file permission namely the symbolic and octal number system. It is up to you which one to use. If you have any doubts or clarification to be made feel free to drop a comment in the comment box.