How file and directory permissions work in Linux (III)

linux logo

In the two previous installments we had begun to see what the handling of the file and directory permissions in Linux, both in the 'rwx' form and in the numerical nomenclature, where we assign the value of 4,2 and 1 to the bits from left to right to obtain the desired form. Now, as we anticipated the last time we talked about this, let's see how to change user permissions and owner and group of a file or directory.

The command to modify file and directory permissions in Linux is chmod, which supports modifiers such as '+', '-' and '=' to add, modify or set the indicated permissions, respectively. This is used together with the letters u, g and o that indicate owner, group and others respectively, in order to indicate that we are going to add or remove both for the owner of a file and for its group and for all users. Y It is not necessary that we execute it separately for each one but we can combine it in a single order, separating by commas, and thus to add write permission for the owner, and read permission for the group (for a file called test.html) we do:

# chmod u + w, g + r test.html

Now, for example, we are going to add the read permission to 'others' and we are going to remove it from the group:

# chmod gr, o + r test.html

Another way to modify the permissions is by using the octal form, which we leave well explained in the previous installment but it doesn't hurt to remember. Basically, to say that it is three numbers that represent the permissions for the owner, group and for all users, and whose values ​​are added as follows: 4 for the read bit, 2 for the write bit and 1 for the one of execution. With which they can vary from 111 (if only the latter is activated) to 777 if they are all activated, passing through multiple intermediate values ​​such as 415, 551 or 775.

In this case, assuming that 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 want to leave all the permissions to the owner but only the execution permissions to both the group and the other users, we do:

# chmod 711 test.html

Now, what happens if once we have the permissions as we want, we realize that we need the files and directories to belong to another user? In that case we have to change the owner of a file or directory, which in Linux is done through the chown command, whose 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 say that the only one who can freely modify the permissions of any element of the system is the superuser, or root. Everyone the other users are only allowed to modify the permissions and owner of those files that belong to them.

Thus, if we wanted to modify the owner of the test.html file so that instead of belonging to the user guille it becomes the property of the user adry, what we have to do is the following:

$ chown adry test.html

If at any time we need the file to belong to the user guille again, we will need to 'gently' the user adry execute the following:

$ chown guille test.html


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Juan Jose Cúntari said

    Mobile care + link in that article with opera browser and fine print they deducted 15, 01 pesos without eating or drinking it

  2.   Jahaziel Ortiz Barrios said

    Excellent your articles, thank you

  3.   Brandon said

    Why use permissions? I do not understand 🙁 udos greetings.