Sunday, July 24, 2022

Basics of Permissions on Unix and Linux Systems and chmod Command | Part -1

Permissions play an important role in unix / linux ecosystem, they are integral part of data security, every file and directory must have clearly defined ownership, As a beginner you should undertand that each file and directory have three types of permissions

A) Read
B) Write
C) Execute

There are some advanced concepts as well such as sticky bit,suid,sgid and umask which are meant for advanced users, we will discuss these in our future posts.

These permissions are represented in following two types of notations :

1) Symbolic notation
a) read : r
b) write : w
c) execute : x

2) Numeric notation
a) read : 4
b) write : 2
c) execute : 1

lets understand these concepts with examples:
i have created two directories Permissionsdemo1 and Permissionsdemo2 inside home path of user "appuser1" with same user, this user is part of group "app", defaults permissions of these directories are "rwxr-xr-x" ( rwx -> owner | r-x -> group | r-x others ), which means owner of these directories have all three read(4), write(2) and execute(1) permissions, the group ( or group members) to which owner belongs to have read(4) and execute (1) permissions, other too have read(4) and execute (1) permissions













Next i have created a file inside Permissionsdemo1 directory with "appuser1" user, with name "Script1.sh", its a executable file ( Script ), default permission of this file is "-rw-r--r--" ( -rw -> owner | r-- -> group | r-- others ), which means owner of this file have read(4) and write(2) permissions,the group ( or group members) to which owner belongs have read(4) permissions and other too have read(4) permissions















Here comes the interesting part, Unix ( or Linux ) Provides you way to change the default permissions, You can do this with the help of "chmod" command. Again this commands as two forms -

A) Symbolic
B) Numerical

In this post i will be focusing on Numerical form , will explain Symbolic form in next part, i personally find Numerical form easier to learn and practice , Please pay close attention to following example where i have provided permsions "640" to file "Script1.sh" . This means owner of this file have this file have read(4) and write(2) permissions,the group ( or group members) to which owner belongs to have read(4) permissions and other Dont have any permissions (0). Notice that only owner of the file or superuser ( example : root ) can run this command . After this change , one other user "neeraj" ( which is not part of group "app" ) is not able to read the file. howver appuser2 which is part of "app" group is still able to read the file, since group has read permissions,















However we notice that neither "appuser1" ( owner ) or "appuser2" ( owners group member ) are are able to execute this file













To Solve this problem, we will again use "chmod" command.
i provided permsions "750" to file "Script1.sh" . This means owner of this file have this file have read(4) and write(2) and execute(1) permissions,the group ( or group members) to which owner belongs have read(4) permissions and execute(1) and others Dont have any permissions (0).










In next expample i have tried to write one line to "Script1.sh" using "appuser2" which is not owner of file but is part of owners group, since the group does not have write permissions, my attempt failed and i got error "Permission denied". group currently has 5 ( Read : 4 + execute : 1 ) Permissions.









To Provide write permissions to group , i have used "chmod 770" , this will provide full permissions ( Read : 4 ,Write : 2 and Execute : 1 ) to owner as well as group but no (0) permission to others, after this i am able to write to file "Script1.sh", i am also able to execute the modified file with "appuser2"











Since now "appuser2" has "full" ( really ? ) permissions to file "Script1.sh" ( As it is part of "app" group and group has read,write and execute permissions). Should it be able to delete ( remove ) the file "Script1.sh" ? The answer is "not yet" and reason is little complex
you can see in following example "appuser2" is not able to delete ( remove ) the file "Script1.sh"







The reason is while deleting a file (Script1.sh) "appuser2" ( owners group user ), also trying to modifiy the directory "PermissionDemo1" , however the group does not have permission to modify the directory. with the help of chmod command we have now modified persmissions of "PermissionDemo1" directory, We have used chmod "771" , this gives read ( 4 ) , write ( 2 ) and execute ( 1 ) permissions to both owner and group and only execute (1) permisions to others, note that this is for directory. after that "appuser2" is able to delete the file.











By now you are aware of basic concepts related to linux persmissions and also about "chmod" command, in next post i will be covering some advacned copncepts




if you want learn to Basic Unix Commands in 1 Hour, here is the link

Basic Unix Commands in 1 Hour

if you want learn Unix/Linux Commands in detail, here is the link

Learn Unix/Linux Commands in detail

Also keep visiting my blog to learn more

unixtechworld.blogspot.com



Get latest Tech world updates

No comments:

Post a Comment