1.

File Permission Commands

Answer»

There are 3 types of people who can use a file and each type has 3 types of access to the file. This is shown in the diagram given below:

The diagram shows that there are 3 types of people accessing a file and they are:


  1. User (u)

  2. Group (g)

  3. Others (o)

Also, the access that we want to give to each of them is of three types:


  1. Read (r)

  2. Write (w)

  3. Execute (x)

So, each of them can have 0 or more out of these 3 permissions. Now let us understand the Linux commands that help us give these permissions to the files.
One important thing to note here is that before these 9 slots of the user, group and others (read, write and execute permissions), there is also one another slot. This slot is for special files. For instance, if you something as drwxr--r--, here ‘d’ shows that it is a directory of which you are viewing the permissions. Further, rwx means that the user has all the three permissions where as r-- means that the group has only read permission and the write and execute permissions are not there with the group. The same is the case for others (another r--).

  • The chmod Command:

Before we jump into the Linux file permission commands and see some examples, it is very important to understand this chmod command in detail first as understanding this command completely will clear the entire concept of file permission commands. The chmod command stands for “change-mode” which means that using this command, we can change the mode in which some user is able to access the file. This command is used to change the file permissions. The syntax can be either using symbols (characters) or numbers. We will see that in detail.

  • Symbolic Method for granting permissions:

This is the first method of chmod command using which we can give permissions. The basic syntax is as follows:

chmod [ugoa…][-+=]perms…[,....] FILE….

Let us understand this syntax in detail.

The first set means the type of person to give access to. Here:


  1. u → Stands for User

  2. g → Stands for Group

  3. o → Stands for Others

  4. a → Stands for All the users i.e. instead of writing ugo, we can just write a.

If the user's flag is not included in the command i.e. we do not mention for which kind of people out of u, g and o, are we changing the permissions for, by default, it takes a i.e. all the users.

The second set is the set of operators. Let us see what they mean.


  1. - → removes the mentioned permission

  2. + → adds the mentioned permission

  3. = → Changes the current permission to the mentioned permission. IF no permission is mentioned after using the = operator, all the permissions from the mentioned class are removed.

The perms stand for permission and ‘,’ is used to separate different permissions. Let us now see the Linux commands using the symbolic notation of chmod.

COMMANDMEANINGEXAMPLE & SYNTAX
ls -l fileNameThis command is used to show the file permissions along with the owner and other details of the specified file. 

Example: The file permissions along with the owner and other details is shown for the file file1.txt on the right.


Syntax: $ ls -l file1.txt -rw-r--r-- 1 Guneet Malhotra 197121 0 Feb 25 10:51 file1.txt


rThis command represents the read permission.

Example: The command shown in the right adds the read permission to the o (other) class for the file file1.txt. 


Syntax: $ chmod o+r file1.txt


wThis command represents the write permission.

Example: This commands adds the write permission for a(all) i.e. user, group and others.


Syntax: $ chmod a+w file1.txt


xThis command represents the execute permission.

Example: This command adds the execution permission for the user.


Syntax: $ chmod u+x file1.txt


  • Numerical Method for granting file permissions

There are numeric codes for each permission. They are as follows:


  1. r (read) = 4

  2. w (write) = 2

  3. x (execute) = 1

  4. No permissions  = 0

The permissions number of a specific user class is represented by the sum of the values of all the permissions. For instance, if the user has read and executed permissions, but not the write permission, then the permissions number for the user will be read (4) + execute(1) = 5.

For instance, if we have to write a command to provide read and write permissions to the user, group and others, there can be many ways of doing so. Let us see one symbolic way:

  • Symbolic Way

$ chmod ugo+rw file1.txt

We can write this in a numeric way as shown below:

  • Numeric Way

$ chmod 666 file1.txt

Explanation: We have already studied that if we do not mention u/g/o then by default the permissions are applied to all. Also, read + write = 4 + 2  = 6. We have written 6 thrice because of applying the permissions to user, group and others. So, read and write permissions are applied to the user, group and others (666) for the file file1.txt.




Discussion

No Comment Found