Modifying file permissions with chmod command in GNU/Linux

Image

In any multi-user operating system the files have a series of features that allow us to see them, modify them or execute them for users that we define. Although there are several alternatives for achieving this, GNU/Linux uses the traditional users and groups system, allowing us to configure the file permission in various ways. By executing ls -l we can list the permissions over the various files inside the directory we are in. Look at the folowing example line displayed on executing ls -l

-rwxr-xr-x 1 user1 group1 128931 Feb 19 2000 test.txt

From the above line, first 10 characters corresponds to file permissions and we concentrate on them for the same reason.
Character 1        : It tells us whether it is a file or a directory. ‘-‘ for file and ‘d’ for directory.
Characters 2,3,4: These are the so called rwx permissions i.e, read,write and execute permissions. And 2,3 and 4 characters include the permissions for the owner of that file.
Characters 5,6,7: These characters have exactly the same meaning as the preceding ones, but refer to the permissions granted to the users of the
group to which the file belongs.
Characters 8,9,10: Same as the precedng ones, but for the other users in the system.

Following these 10 characters we have a number showing the number of hardlinks that the file has. If it is a directory the number shows how many folders there are in it as well as the number of hardlinks it has. Next we see the file’s owner and the group in which the owner is included followed by size in bytes and the date it was last modified. At last we find the name of the file.

In order to change the permissions of a particular file we can use the chmod command. We need to remember that only the file owner(or root) can change
these permissions, otherwise it is of no use. The chmod command is usually used in 2 ways.

Method 1: Of the form chmod xxx file. The xs will be a number between 0 and 7. The first x indicates the permission we would like to apply for the user,the second one, for the group, and the third one, for all others. For giving the exact permission we need to get the meaning of these numbers in binary format. You can make use of the following table.
Decimal Binary Meaning
0             000         —
1             001         –x
2             010         -w-
3             011         -wx
4             100         r–
5             101         r-x
6             110         rw-
7             111         rwx
Method 2: Another way of using this command is to specify explicitly the permissions we wish to apply. First we indicate whether we refer to the                   permissions of the user, or the group, or all others, with the letters ‘u’, ‘g’ or ‘o’ respectively. Next, we will add a ‘+’ or a ‘-‘  depending on whether we wish to add or remove the permission, which we will indicate by ‘r’,’w’ or ‘x’. For example, chmod ugo+r test.txt would give a read permission to the group and to other users for the file test.txt.

In order to change the file owner we have the chown command which can only be used by the root. In order to change the group of a particular file we can use the chgrp command, again with root privilege only. Have a look at the power of root. But as I always suggest unless or otherwise you need to, never login as root. By now I think you know the reason behind my suggestion, right?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s