A detailed Guide on File permissions and Ownership in Linux

Sawez FaisalSawez Faisal
5 min read

Ever wondered how does linux manage file permission and ownerships among multiple users in its operating system?

As Linux is a multi user operating system , which means that multiple users ( and not limited to users only) can access the same operating system environment . This creates a need to segregate files and directories, based on

  • Who Owns the files (File Ownership)

  • What permission does file owner have (File Permission)

Let us first dive into file ownership

The Linux environment has mainly three types of owners

  1. User

  2. Groups

  3. Others

Users

Any individual that creates a file (even directory is a type of file) becomes the owner of that file. This is the innermost level of ownership of any entity.

Groups

Groups consist of multiple users within it , and all the users within the group has access to the files , as per the groups persmission. However , it is to note that in linux the order of permission checks goes from user→group→other.

Others

Others can be considered as the largest group collection, basically anyone who has access to the system , in any group or as any user , will be a part of the others

So in summary , this is how the file owners would look like

Now that we are familiar with File Ownership, Lets dive into Permissions

Linux permissions are pretty simply implemented, which at first may seem confusing , but with just some basics down , one can understand and manage permissions

A file ( or a directory) contains three type of permissions , that one can grant:

  • Read (r) → Permission to read the contains of file (can enter , in case of a directory).

  • Write (w) → Permission to make changes to the file ( can create or delete files in a directory).

  • Execute (x) → Permission to execute the file (if it is executable)(in case of directory , can enter the directory)

Now lets run some commands to see these permissions

stat <filename/directory_name>

If you run this command , you would see something like this

Lets break each component down and understand , what each piece means:

User and Group Information

Tells us about the user and group names that this file belongs , to

To get the information about users and group , run the following

groups #shows you the list of groups you belong to
whoami #show currently logged in user
id <username+> #shows uid,gid for all the users and groups

Inode

Index node is a data structure , that is unique to every file and contains the meta data related to that file

Permissions

What does this 0664 means ?? and what is this random r,w and - that we see??

Remember we talked about read ,write and execute permissions , these r, w, and x are the same in order . The - that you see , implies that no permission has been set.

Now lets break it down

  • Read permission implies 4

  • Write permission implies 2

  • Execute permission implies 1

So , if you have something like rwx , it implies ( 4+2+1) =7

The order of defining permissions is User → Group → Others

Now that you understand the permission order , the permission number is pretty much easy

Here we have 664, lets break it down

6 → (4 +2+0) implies → r and w permission and - (no permission to execute), Hence we could say that , the user has permission to read and write to file test.c but can not execute the file

6 → pretty much the same , but now its for group , Hence , all the user of group has permission to read and write and not execute

4 → (4+0+0) implies → the Others only have the permission to read the contents of the file , but can’t write to it nor execute it

The (-) at the starting implies that it is a file , for directory its (d) .

Now that we understand how permission works , let us see how to change permission for any file/directory

The command chmod lets you change the file permissions.

Their are two broad ways to change file permissions

  • Using absolute mode

  • Using symbolic mode

Absolute Mode

In this mode , we can change permissions using numbers (octal numbers) , Here’s a quick lookup table for the same

Say you want the user to have all permissions , the group to have only read permission and others to have no permission , you could do something like:

chmod 740 <file/dir_name>

Breakdown:

7 → as user has (4(r) +2(w) +1(x)) permissions

4 → as group has (4 (R) ) permission

0 → as others has no permission

Symbolic Mode

The need for symbolic mode arises , due to the fact that in absolute mode you have to provide numbers(permission) for all the three , even if you wanted to change the permission for one .

Symbols:

  • u - user

  • g - groups

  • o - other

  • a - all ( u + g + o)

  • + → sets permission

  • - →removes permission

  • \= → assigns permission

Say you have something like this chmod a+r <file/dir> → this sets read permission for all (users+groups+others)

chmod g-x <file/dir> → this removes executable permission for group

Now that we know how permissions work , let us see how we can change owner

Linux provides the chown and chgrp commands to manage file ownership and group association.

Changing the File Owner (User)

To assign a file to a new user, use:

chown <new_user_name> <filename>

To update both the owner and the group in one step:

chown <new_user_name>:<new_group_name> <filename>

To update only the group

chown :<new_group_name> <filename>

Also it is to note that a file cannot be owned by multiple groups or users

Thank you so much for taking your time to read this!! , if you have any suggestions , please do share in the comments.

0
Subscribe to my newsletter

Read articles from Sawez Faisal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sawez Faisal
Sawez Faisal

New to the field and eager to learn how complex systems work smoothly. From building compilers to scalable systems, I’m solving problems as they come—whatever the domain—and sharing all the highs, lows, and lessons along the way!