In the Linux environment not all the users have permission to modify files and have access to directories. There are a set of user access permissions based on which category the user belong (user, group or other users). A superuser is the one who has access to the entire system whereas a normal users only have a particular set of permission and access to files and directories. If a normal user wishes to do something with a file to which he does not have appropriate permissions, that is where the “chmod" command comes in.
The users can be categorized into user, group and others.
- user - A particular user. You can use the whoami command to find who the current user is.
- groups - A user belonging to a set of groups. Use the groups command to find out more about the groups and the groups in which the user is in.
- others - Any other user.
Users generally need permissions to either a file or a directory.
Types of Permissions
Base on weather it is a file or directory, the permissions are shown below. The 3 most common permissions are,
- Read - r
- Write - w
- Execute - x
If it is a directory,
- r - Access the directory
- w - Add or Delete directories
- x - Reference or Move directories
If it is a file,
- r - Read and copy the file
- w - Insert or delete contents of file
- x - Execute files if it is one.
Changing permission using chmod
To provide a user with a permission or access the chmod command can be used.
chmod -[Options] [Mode] [file | directory]
R - This option tells the chmod command to change the permissions to all the subdirectories recursively if the specified target is a directory.
Modes in chmod
There are two ways in which we can specify the permissions,
In the symbolic way, there are set of mnemonics identifier with which we can set to whom the permissions are to be set.
- u - user
- g - groups
- o - others
- a - All of user, groups and others
Then there are operators that we can use to assign the various permissions that we discussed already(r, w, x).
= - Sets the permission provided and the other permissions are denied.
+ - Sets the provided permissions and the other permissions are unchanged.
- - Removes the provided permissions and the others are left unchanged.
To specify multiple users at the same time use , to separate them and no space must be included. See the example for the = shown above.
Octal representation is used to set the permissions for all the users at the same time using the octal equivalent code. The following table shows the various way by which you can specify the permissions.
|4 (r - read)||2 (w - write)||1 (r - execute)||Permissions set|
|0||0||1||Will set the execute permission for the file/directory.|
|0||2||0||Will set the write permission for the file/directory.|
|4||0||1||1 + 4 = 5, so 5 Will set the read and execute permission for the file/directory.|
|4||2||1||1 + 2 + 4 = 7, so 7 Will set the read, write and execute permission for the file/directory.|
By using the above way to set permissions, we must set the permissions for all users, groups and others at the same time.
To use the recursive method use the R option.