To modify a file’s permissions, the chmod command is used. Only the owner of the file and root can use this command. The syntax is simple: chmod PERMISSIONS FILE.
You can set file permissions in two ways: using numbers and letters.
Change permissions using numbers
With this method, each permission is assigned a number: r=4, w=2 and x=1. You use each set’s total number to assing the permission. For example, if you want to assign a read and write permissions, you would add the numbers representing these permissions (4+2=6).
Here is a table with all possible values:
Here are a couple of examples that will help you grasp the concept.
We have a file called bobs_file.txt. Since bob is the owner of the file, he can change the file’s permissions. We have removed all permissions from the file. Now we want to assign the following permissions:
owner – read, write, execute (4+2+1=7)
group – read, write (4+2=6)
other – read (4)
We can do that by using the following command:
Remember, the first number represents the owner, the second number represents the group, and the third number represents everyone else on the system.
Here is another example with the same file. Let’s say that we want to assign the following permissions:
owner – read, write (4+2=6)
group – read (4)
other – read, execute (4+1=5)
We can do that using the following command:
Change permissions using letters
You can also change permissions of a file using letters instead of numbers. The letter r represents the Read permission, the letter w the Write permission, and the letter x the eXecute permission. You use the operators +, –, and = to indicate whether you would like to add or remove a permission.
For example, lets say that we have the following permissions on bobs_file.txt:
owner – read, write
group – read, write
other – read
We want to add the write permission only to the others group. Well, we could do that simply by typing chmod 666 bobs_file.txt. We can also use operators to change only the permission of an individual class. The owner class is represented by the letter u, the group class by the letter g, and the other class by the letter o. So in this case, we can just use the following command: chmod o+w bobs_file.txt
You can assign permissions to more than one class at once:
In the example above we have added the execute permission for the owner and other classes.