Everything about chmod command in Linux

What does it do?

chmod command is used to change permissions of a given file according to a certain mode which might be a set of octal characters or a set of alphabetical characters.

Permissions explained

Each file on your system has a certain set of permissions associated with it. There are three types of permissions that can be associated with a file:

  • Read permissions denoted by 'r'
  • Write permission denoted by 'w'
  • Execute permissions denoted by 'x'

You can check the permissions of a given file by using the following command:

ls -l <filename>

The output of this command will look something like this:

enter image description here

The string rwxr-xr-x represents the permissions of this file. It can further be divided in to three parts containing three characters each and we get the three components of file permissions in linux:

  • Owner permissions(rwx) - Permissions for the user who created the file. rwx means that this user can read, write and execute this file.

  • Group permissions(r-x) - Permissions for other users in the file's group. r-x means that the user can read and execute the file but cannot write to it.

  • Other user permissions(r-x) - Permissions for users who do not belong to the above categories. In this case these are the same as the group permissions.

Permission modes

There are two types of modes that can be given as input to the chmod command:

Set of octal characters

Consider the owner permissions in the above example. They are represented by rwx. Now consider r=4, w=2, x=1. Therefore rwx = 4 + 2 + 1 = 7. Therefore the octal character 7 represents that the user has the permissions to read write and execute the file.

Let us take another example. The group permissions in the above case is r-x. As we already know r=4 and x=1. We denote the empty slot by 0. Octal representation for r-x = 4 + 0 + 1 = 5. Therefore the octal character 5 represents that the user has permissions to read and execute the file but it cannot write to the file.

Similarly the other users' permissions evaluate to 5 in our case. Therefore the complete permissions of this file can be represented by the octal number 755.

To change the permissions of a file using the octal number mode we run:

chmod <octal number> <filename>

Now we want to change the permissions for this file to say, rw--r-xr--.

Owner permissions(rw-) = 4 + 2 + 0 = 6
Group permissions(r-x) = 4 + 0 + 1 = 5
Other user's permissions(r--) = 4 + 0 + 0 = 4

Now, for changing the file permissions we run:

chmod 654 chmod.txt

Values of r,w and x.

In the above sections we assumed values r=4, w=2 and x=1. Now let us discuss how these are derived. The three character permission string(rwx, r-x or r--) is nothing but a three bit binary string. Now by convention the first bit specifies read permission, second bit specifies the write permission and the third bit specifies the execute permission. If that bit is one, the user has that permission. Now we denote read by 'r' and if read is allowed there is a 1 on the first bit, which evaluates to 4 if the other bits are 0. Similarly the values for 'w' and 'x' can be calculated.

Set of alphabetical characters

The below image(copied from man page of chmod command) specifies the allowed characters and their meanings

enter image description here

We would add three more characters to this list. First one is 'a' which represents a union of all the users that is owner, group users and others. The second character is '+' which is specified to add a permission to a user, and the third character is '-' which is specified to revoke a permission from the user.

Now for changing file permission we run:

chmod <user type(u/g/o/a)><add/revoke(+/-)><permission type(r/w/x)>

To change the owner permissions of a file to read and write, we run:

chmod u+rw chmod.txt

To give a write permission to everyone, we run:

chmod a+w chmod.txt

To remove the write permission for others, we run:

chmod o-w chmod.txt

To change the permissions of a directory, we run:

chmod <permission> <directory name>

To change the permissions of a directory with its files and sub-directories recursively, we run:

chmod -R <permission> <directory name>

This is just a basic explanation of the command. To read more about it, you can refer to the man pages for chmod given at http://ss64.com/bash/chmod.html