2013-08-07

file permissions in Linux


Pranaam to all bhai ji _/\_
I am back, with tutorial on linux file permissions .
many of us have heard and faced problem of permission in linux that we dont have permission to access this file or file is "read only".
ok lets discuss about file permission in linux ;)
when we run command "ls  -al" in a directory , we get something like this


before proceeding i want to discuss about  2 things which are essential for understanding file permission :)
we have three types of user for a file
1. owner
2.group
3.other

owner:-
this is the user by whom file was created . for example file has been created by user root , file owner will be root, if file is created by user  indishell , owner of that file is indishell
owner is the user who have read,write and execute permission on the file when file created by user.

group:-
this is the category of user which is used by system to provide some permission to those user who belongs to a group and that group must be assigned to the file and after that user falls in that group will be able to perform specified actions(read/write or execute operation)
when user creates a file , bydefault its owner as well as group is the user who created it .
for example we have user indishell, and he creates a file r.txt , then file owner as well as group user will be indishell.
if we add a user to group indishell, that user will also be able to perform same action as group user can :).

other:-
those user who are neither owner of the file nor in the list of group user , comes under "other user" category
for example file is created by user root , so the owner of the file is root as well as group too
but we have added user indishell to group "root" , now group users are root and indishell , rest of the user on system comes under "other user" category .

ok it was a short introduction about user category "owner" "group" "other".
lets come to point and talks about file permissions
we have 3 type of permission for a user on a file (user may be owner,group or other)
1.read (r) (octal representation of read permission is 4 )
2.write (w)  (octal representation of write permission is 2 )
3.execute (x)  (octal representation of execute permission is 1 )


in above snap, have a look at  this  -rw-r--r--
start from right (-rw-r--"r--")
every user type has 3 filed of permission , r w x  that is read,write and execute
- shows that user dont have permission of that filed(field may be read write or execute)
if we have these fields like this , rw- it means user has only read(r) and write(w) permission and dont have permission of execution
if we have these fields like this  r-x , it means user has permission of read(r) and execute(x) and dont have write permission(w field is filled with - )
if fields are like this  r--, it represent that user has only read permission ,
if we have fields like this one  --- , it shows that user dont have any permission ,neither read,write nor execute
from right , first three field represent other user permissions and in our above example we have permissions r-- (rw-r--"r--") which represent user "other" has only "read" permission
from right, next three fileds(from 4th to 6th) represents permissions for user " group " which are r-- (rw-"r--"r--)
from right, next three fileds(from 7th to 9th) represents permissions for user "owner"  which are following
rw- ("rw-"r--r--) means owner of the file has read and write permissions on file

lets have a look on these pics


lets differentiate these permissions  via another  pic ;)


changing permission for file:-
we can change file permission by running command chmod :)
syntax of chmod command :-  chmod  user+/-permission  file_name
here
 user  may be owner(u) group(g) other(o) all(a)
 permission may be  read(r) write(w) or execute(x)
for example i have a file r.txt with permissions  rw-r--r-- and now i want to add(+) permission execute(x) for user other , then command will be
chmod  o+x  r.txt




if i want to remove(-)  read(r)  permission for user group(r) on r.txt file , command will be
chmod g-r r.txt



changing file permission using octal number:-
we can also use octal number representation to change permission of file using single command for user owner,group and other
how, lets start :)
read(r) permission octal number is 4
write(w) permission octal number is 2
execute(x) permission octal number is 1
if we want to set read and execute permission for a user then we will add value of read and execute permission octal number  that is 4+1=5
for read and write permission 4+2=6
for write and execute permission 2+1=3
for read permission 4
for write permission 2
for execute permission 1

syntax for octal number permission changing command is 
chmod  0owner_permission|group_permission|other_permission  file_name

Note:- here 0 before owner_permission represent special permission(we will discuss later)
in octal number permission changing method , we define permission for user owner,group and other
for example , i want to set
 read and write permission for owner ( 4+2=6 )
 read and execute permission for group ( 4+1 =5)
 execute permission for other  ( 1 )
command will be
chmod   0651 file_name


changing owner or group of file:-
we can change owner or group of file by using command chown and chgrp.
chown command changes owner(own) of the file
chgrp command changes group(grp) of the the file

Syntax of chown command :-  chown  user_name  file_name
lets change owner of the file r.txt from root to indishell by using command chown



ok now changes group of file r.txt from root to indishell using command chgrp
syntax of command chgrp is :- chgrp  user_name  file_name



:D

Thank you
Greetz to :- Guru ji zero , whole Team IndiShell and Hardeep bhai <3




Share this post

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

© 2009 Start With Linux | Mannu Linux
Designed by cyb3r.gladiat0r
Posts RSSComments RSS
Back to top