Linux File Permission

What is File Permission in Linux

Linux is an operating system that has the facility to create files and folders. These files and folders are protected with permissions for owner, groups and other users.

3 types of permission are there in the file system

  • Read ( r )

  • Write ( w )

  • Execute ( x )

In the Linux file system, we can give a combination of permission for users, groups and others.

Check File Permission

Use ls -ltr to check the file permission of files and folders.

In the above screenshot, you can see some pattern drwxr-xr-x, This is file/folder permissions.

Let's decode this.

Understand Permissions

  1. -/d: This indicates the type of the file whether it is a file or a folder.

    1. dash(-): Dash defines it as a file.

    2. d: d defines it as a directory(folder).

  2. rwx: First 3 characters after -/d is for user/owner permission.

    1. Read ( r )

    2. Write ( w )

    3. Execute ( x )

  3. rwx: Second 3 characters are for group permission.

    1. Read ( r )

    2. Write ( w )

    3. Execute ( x )

  4. rwx: Last 3 characters are for other users' permission.

    1. Read ( r )

    2. Write ( w )

    3. Execute ( x )

In the place of rwx if there is a dash(-) then the specific permission is not given to the respective permission holder.

Let's understand this with diagrams:

Change File Permission

to change the file permission chmod command is used.

chmod <permission> <file name>

Permissions can be given in either a numerical way or symbolic way.

For numerical way refer the below chart

For symbolic, Refer below chart:

chmod <user>+<symbols> <filename>

chmod u+rwx devops - I have given user/owner (u) permission to read, write and execute.

Example

For example, there is a file hello-world.sh and I want to execute the file.

vim hello-world.sh

So let's check the permission first with ls -ltr command.

-rw-r--r-- 1 neel neel 39 Apr 2 23:21 hello-world.sh

As you see I, owner, does not have permission of execution of the file, so I will change the permission first with chmod

-rwxr----- 1 neel neel 39 Apr 2 23:21 hello-world.sh

Here I have given 740 that means 7 for owner (all), 4 for groups (read only), 0 for other users (nothing)

Now I can execute the file.

Have further queries? comment on this blog.