Changing permission of a file in LINUX System


How to change permission of a file (or directory) in a Unix/Linux System?
This is the very basic problem which a first time unix/linux user faces very oftenly , how to get special privilege or permission to a file or directory. The intended audience to this post is who want to understand "Ownership and permissions" in a unix like systems from the very basic step-by-step.

Basic Concepts:-
For every file or directory there are three categories who may have access.
1:- Owner
2:- Group
3:- Others
Brief description of each given below.
1:- Owner
Owner of a file is a person who created the file. Traditionally ownership of a file is not transferable but we can change it by using command
chown
Only SUPER-USER (User who have admin power) can use this command.
For a super-user start every command with "SU" then shell prompt asks for a password, enter password , now you become super user. 
2:- Group
There might be a group of users who have group ownership and permissions to a file or directory. Use following command to see group ownership.
ls -lg
3:- Others
All users other than group and owner are known as others.
Every file or directory has three type of permission describing which type of operation would be possible with directory or file.
1:- Read(r)
User who has this permission for a file or directory allow to see the content of that file and to explore that directory.
2:- Write(w)
User who has this permission for a file or directory allow to change the content of that file or directory. With this permission user can create new files and can remove existing ones.
3:- Execute(e)
User who has this permission for a file or directory allow to use filename as a command and to change the directory ( through "cd" command ) and can copy files from from that directory .
Changing Permissions!
There are three categories of users , and three types of permissions read(r), write(w), execute(e) apply to each of them. hence there are 9 cases. These 9 permissions collectively known as "mode"  of file or directory. Use following command to change the mode of file or directory.
chmod
each file has some kind of permissions. To know which file has what kind of permissions use the given command .
ls -l
The first column looks like this.
drwxrwx--- or
drwxr-xr-x or
-rwxrwxr-- 
The ten lettered word denote permission.
first letter tell you about nature of the file. if it is "d" the file is actually a directory. If it is "-" file is an ordinary file. remaining 9 letters split into three words of three letter each, the first word tells about owner, the second about group and the third about the others.
There are total of 8 possibilities which are denoted by numbers between 0 and 7. These numbers are generated by Let us consider one of them assigning the values 0 to 1, 2 to w and 4 to r,  and adding them as shown in figure
This helps us to replace a word of 9 letters by a three letter digit number. for example
rw-r-xrwx
can be splited into three groups as
rw- r-x rwx
and hence would be replaced by 657.
In this, the first digit denotes permission for the owner whether he/she can read, write and execute the file. The second digits denotes what members of the group can do. The third one denotes what any other user can do.
the command to change permission (mode) is very simple as shown below.
chmod 744 filename
so that filename gets "744" permission.

Share this post and spread the knowledge --->

No comments:

Post a Comment