Saturday, September 25, 2010

File Permissions in Linux

Some time you may have seen that you are not able to access some of the file or directory of your own system. Do you know why ..?

No?

Your answer is here....
As you know, Linux is a multiuser OS. So every user have the permissions for a particular dir or file. A user not given authentication will not be able to access that...

Before going to the permission manipulation a would like to tell you some basics of it.

There are three types of ownership in Linux as..

User:
It is the creator of the file. It is assigned by default to the creator.

Group:
It is the bunch of the user belongs to the group to which file belong. All the user of that group have the access permission to that file

Other:
The user which neither owner of the file nor belongs to the group of the file are under the category of other or in other words we can say that we are making the folder global.

Other than all these, each file have three types of permissions for all types of user separately as follow....

Read:
A user provided read permission will be able to read the file only he is not able to read it.

Write:
This permission to the user provide the access the file for making the changes to the file.

Exicute:

This is the permission given to particular user for executing the program file or shell script.


Now after knowing all this basics next question generally comes to everybody's mind (As it came to mine even) how to know that which user is being provided with what permissions, who is owner of file and which group it belongs whether it is a file or a directory.......

Use the following 'ls' command in listing mode (by using -l with command) as

sachin@ubuntu:~$ ls -l

and it will result as..

total 37576
-rw-r--r-- 1 sachin sachin 4949 2005-08-22 22:10 class.mysql.php
-rw-r--r-- 1 sachin sachin 8174 2005-10-08 03:31 class.table.php
drwxr-xr-x 5 sachin sachin 4096 2010-09-25 22:55 Desktop
drwxr-xr-x 3 sachin sachin 4096 2010-08-20 17:05 Documents
drwxr-xr-x 2 sachin sachin 4096 2010-09-23 12:45 Downloads
-rw-r--r-- 1 sachin sachin 179 2010-05-09 01:21 examples.desktop



where each column have the significant meaning....




In the file permission column you see something like dxw-rw---x-

In which each letter have significant meaning..

The first character can be any of these

'-'-- regular file
'd'-- directory
'l'-- symbolic link
's'-- unix domain socket
'p'-- Named pipe
'c'-- Character device file
'b'-- Block device file

the next nine character represent the owner (next three) group (next three) other (next three)
these may contain only following four character as(In all group of three in all user three character represent read, write and execute respectively )

'-'-- No permission
'r'-- Read permission
'w'-- write permission
'x'-- Execute permission

Now I explain the file in the above image...
1. It is a directory
2. Owner of the file is 'sachin' and group is also sachin
3. Owner have the all three permissions read, write and execute
4. All the user of 'sachin' have the read and execute permission.
5. Where as other user also have the read and execute permission


Now the question is.... How to set or change the permission of the user and groups

There are two ways to do so one is symbolic mode and other is numeric mode.
First we will discuss the symbolic mode

Example:

$chmod u-x Desktop

the above command will remove the execute permission of owner

It has three parts as
Which user ? 'u' for owner, 'g' for group and 'o' for others

what ? '+' add the permission,'-' remove the permisson, '=' exactly gives the assigned permission

$chmod g=rx Desktop

the above command will assign the read and execute permission to Desktop no any other permission


Secondly we will do the same using numeric mathod...

$chmod 777 Desktop

the above command give all the permission to all user

In above command first number represent the owner second group and last represent the other

and the number have the following meaning

0 No permission
1 execute permission
2 write permission
4 read permission
1+2=3 execute and write permission
1+4=5 execute and read permission
2+4=6 write and read permission
1+2+4=7
All the three execute, write and read permission


Now, In the last one thing is left....
how to change the owner and group of file or dir

Use the following command

$chown ownername fileordirname


the above command will change the owner of the fileordirname to ownername

and

$chgrp groupname fileordirname

the above command will change the group of fileordirname to groupname....


Note: If you use -v(vebose ) will show you what it has done with the file or dir and -R(to be used with dir only) will apply the changes recursively to all the files and dir contained in the given dir.