Linux file permissions : A simple guide

Aniket MoreAniket More
3 min read

Hey there, folks!

Did you know every Linux command that you use is basically a file that you execute ? In Linux system, each command is stored as a file and when you execute a command, that specific file is accessed from a specific location and run. Also there are some files that you cannot even modify or even access them. Isn’t it amazing!!!

Recently I started working with files in Linux. In this article I will be sharing my journey of learning about working with files on a Linux system.

  • Remember that each file on Linux has specific rules defined for how we can use them or access them. The rules defined for working with files are called as “permissions“. Each file has 3 permissions :

    Read (r) - you can just read the file and view content of it.

    Write (w) - you can write into files or modify the files content.

    Execute (x) - you can execute or run the file.

  • Let me tell you one more interesting thing!!! These permissions for files can vary depending on the user type. Each user category has their own read, write and execute permission. There are 3 categories of users or user roles :

    User (u) - Normal end users like someone who is interacting with the

    system directly.

    Group (g) - The users of a team or users of a common project.

    Others (o) - Users who are admins, guests or external users who are not

    the primary users.

I learnt these 2 essential commands :

  • chown - change ownership of the file.

  • chmod - modify permissions of the file.

I have changed the ownership of file from aani (that’s my username!) to the root user like this :

This is how I changed ownership of whole directory and it’s subdirectories and files from aani to root user :

Each file permission has a count associated with it :

  • read (r) - 4

  • write (r) - 2

  • execute (x) - 1

    Remember that -

  • If a file has all the permissions that is 4 + 2 + 1 = 7. So, 7 represents having all three read, write, execute permission for the file.

  • Each of three categories of users have their own separate count of permissions. So, if a file’s all three categories of users have all the permissions that count is 777. Each 7 represents permissions for each user role. You can change this count of 7 while assigning permissions to a file.

I had a lot of fun experimenting with these file permissions. Whatever I just said, you can see this implemented in a correct way. You can refer the following screenshots :

There is one more way to add permissions to specific type of users i.e. you select the type of user and with plus(+) you add the permission to it or with minus(-) you remove the permission. Use this format :

chmod <user-category>(+) or (-) <filename>

for example : chmod u+x aniket.txt

For more experimented usage you can refer to following screenshots :

Summary

So, I learnt to work with Linux file permissions this way :

  • Each Linux file has defined permissions which can be changes. Three permissions are : read(r) - 4, write(w) - 2, execute(x) - 1.

  • File having all permissions has count 7 i.e. (4+2+1 = 7)

  • Three user categories - user(u), group(g), others(o)

  • chown changes ownership of file / directory.

  • chmod modifies file permissions.

0
Subscribe to my newsletter

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

Written by

Aniket More
Aniket More

Hi, I'm Aniket. I'm passionate about coding responsive and dynamic web applications using React, TypeScript, HTML, and CSS. Proficient in Core Java and OOP concepts. Currently exploring Linux and diving into the exciting world of DevOps. Sharing my learning journey and projects to inspire others.