File Permissions in Linux

File Permissions in Linux

by Admin

Knowing about file permission is very important. Suppose we have a file name abc.txt and we want to list out its permissions in linux

You can create file using touch command

touch <filename>

eg. touch abc.txt

Now the file has been created lets check its permission and to check that use ls command

ls -ltr <filename>   

eg.  ls -ltr abc.txt

It will show -rw-r–r– so we observe there are 10 positions lets understand them one by one

We can divide the whole thing into 4 parts

-       r    w  -            r   -    -               r   -   -
0       1   2   3           4   5   6            7   8   9

 r for read           w for write     x for execute

0th Position represents if it is a file or directory, in case of directory it will be d instead of

First triplet 1 2 3 represents permissions for user similarly 4 5 6 and 7 8 9 are for groups and others respectively.

For the file abc.txt user have read and write permissions however groups and others have only read permissions so they can only read the file but can’t modify or execute.

We can change the permissions using chmod command.

We can think of each group as 3 bit binary number

If we wan to give read, write and execute then all 3 bits should be 1 1 1 and that is equal to 7 in binary

For read and write permissions it should be 1 1 0 i.e 6 in binary similarly for read and execute we can give 1 0 1 i.e 5, so we can use give any permission we want by selecting appropriate number and this can be done for all user, group and others.

Let’s consider a situation where we want to give below permissions

User:      read, write and execute            (1 1 1 ) i.e 7

Groups: read and execute                        (1 0 1) i.e 5

Others:  execute                                         (0 0 1) i.e 1

Then the command will be chmod 751 abc.txt

Note: always refrain to use 777

Related Posts

Leave a Comment