In the previous two installments we had begun to see how it handles file and directory permissions on Linux, In both the ‘rwx’ form and the numerical nomenclature, where we assign the value of 4.2 and 1 to the bits from left to right to obtain the desired shape. Now, as we had advanced the last time we talked about this, let’s see how to change user permissions and the owner and group of a file or directory.
The command to modify file and directory permissions on Linux is chmod, Which supports modifiers such as ‘+’, ‘-‘ and ‘=’ to add, modify or set the specified permissions, respectively. This is used in conjunction with the letters o, gyo which indicate owner, group and others respectively, to indicate that we are going to add or remove both the owner of a file and his group and for all users. I we do not need to execute it separately for each but we can combine it into a single order, Separating by commas, and so to add write permission for the owner, and read by the group (for a file called test.html) we do:
# Chmod u + w, g + r test.html
Now, for example, let’s add the read permission to ‘others’ and remove it from the group:
# Chmod gr, o + r test.html
Another way to modify permissions is to use the octal shape, Which we left well explained in the previous installment but which is not bad to remember. Basically, say that these are three numbers that represent the permissions for the owner, group and for all users, and whose values add up as follows: 4 for the read bit, 2 for the d writing and 1 for execution. So they can range from a 111 (if only the latter is activated) to a 777 if they are all activated, through multiple intermediate values such as 415, 551 or 775.
In this case, assuming we want to leave the test.html file with all the active permissions for the owner, the read and execute permissions for the group and the execution permissions for all users we do:
# Chmod 771 test.html
On the other hand, if we wanted to leave all the permissions to the owner but only the execution permissions to both the group and other users we do:
# Chmod 711 test.html
Now, what if once we have the permissions as we want, we realize that we need the files and directories to belong to another user? In this case we must change the owner of a file or directory, which in Linux is done through the chown command, The operation is of the type:
# Chown user files
The value of ‘user’ can be both your username within the system and your user ID, And as a detail to say that the only one who can modify the permissions of any element of the system to taste is the superuser, The root. all the rest of the users are only able to modify the permissions and owner of those files that belong to them.
So, if we want to modify the owner of the test.html file so that instead of belonging to the user guille becomes the property of the user adry, what we need to do is:
$ Chown adry test.html
If at any time we need the file to belong to user guille again we will need to ‘kindly’ the user adry run the following:
$ Chown guille test.html