Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
File Permissions in Linux
2.1.
Read
2.2.
Write
2.3.
Execute 
3.
Examples of File Permissions in Linux
4.
Changing File Permissions in Linux
4.1.
Absolute (Numeric) Mode of Permission
4.2.
Symbolic Mode of Permissions
4.2.1.
Adding Permissions
4.2.2.
Removing Permissions
4.2.3.
Setting Permissions
5.
Frequently Asked Questions
5.1.
What are the three types of file permissions in Linux?
5.2.
How do we check the file permissions?
5.3.
What are the different modes of changing file permissions in Linux?
5.4.
What does chmod 644 mean?
5.5.
What does chmod o=rwx mean?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

File Permissions in Linux

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

How many of you like watching F.R.I.E.N.D.S?

Many of you are probably humming the theme song right now, aren't you?

So, do you remember Monica?

Even if you've never watched the series or are wondering who Monica is, let's get a short introduction to her.

Monica loves everything perfect (according to her rules, of course), so she doesn't like people touching her stuff or moving them around. In other words, without her permission, no one can mess with Monica's arrangements, like where the green ottoman should be.

Now, on a computer, data is stored in files. Everything is in a file, whether it's the program that boots the system or your assignment in a word doc. So here, the Linux operating system is like Monica and doesn't give everyone permission to access all such files. This gives rise to the idea of file permissions in Linux.

In this article, we'll learn about the different file permissions in Linux and how to change them when needed. 

"File Permissions in Linux" written with the 6 cliparts of documents, and a thief with a red cross over it.

File Permissions in Linux

To begin with, we must first learn about the different file permissions in Linux.

The three file permissions in Linux are read, write and execute. Let's see what they are.

Read

Suppose we are reading a book. In the story, one of our favorite characters dies, and we are unhappy about that. So, can we change the storyline of the book?

Of course not, since we are a reader.

Similarly, specific files may be permitted to be read-only in Linux. Thus, we can only view such files.

A girl in a blue top, pink pant and black shoes sitting on the floor and reading a book

Write

With the knowledge of the read permission and the name of this one, you've probably already guessed what this permission does.

The write permission in Linux allows a user to modify the contents of a file.

Here, we must remember that the permissions for a file and a directory are given separately. So, the write permission for a file only allows us to edit the contents of the file. If the directory also has permission to write, then we can rename, move, add or remove files from it.

 

Note: To understand the difference between a file and a directory, consider a folder with papers. The folder is the directory, while each paper is a file.

Clipart of a paper with lines on it and a pencil beside it

Execute 

While downloading any software on your computer, have you noticed that the files we download are executable files with a .exe extension? This extension means that the files can be run as a program.

In Linux, the execute permission is given to such files instead of such a file extension. 

CMatrix

Let's see an example now that we know what the file permissions in Linux are.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Examples of File Permissions in Linux

Theoretically, knowing what the file permissions in Linux are is no fun. So let's see the different file permissions in Linux. 

Note: You must have Linux installed in your system for the following steps. You may also use online Linux terminals.

In the terminal, change the directory to one with some files. In the example below, there is only one file on the desktop, so that is the directory used.

Run the following command

ls -l FILE_NAME.EXTENSION

 

Note: The command mentioned above is the general one in case many files are in the directory. Since only one file is in the example shown below, only ls -l has been used. 

The output obtained should be similar to this.

Linux terminal showing the output for the "ls -l" command

Here, the output is "-rw-rw-r- -". Let's break it down to understand the meaning.

The first "-" means that we are checking the permissions of a file. Had we done so for a directory, there would have been a "d" instead. Next, the first "rw-" represents the file owner's permissions. It means the owner has permission to read and write the file but not execute it. The second "rw-" represents the group's permission and is the same as the owner's. The last "r- - "denotes the permission given to other users, who are only permitted to read. 

The meaning of each symbol in "-rw-rw-r- -"

Now that we know about file permissions in Linux let's learn how we can change them.

Changing File Permissions in Linux

Suppose you're cleaning your room, and you find a small place in the cupboard empty. So you decide to stock up on snacks over there, but you worry someone may find them before you. What would you do in such a situation?

Lock the cupboard barring everyone's access, right?

In Linux, you can do the same for files. You can change the file permissions in Linux.

The general syntax to do that is

chmod PERMISSIONS FILE_NAME.EXTENSION

 

Here, we can give permission in two ways

(i) Absolute (numeric) mode

(ii) Symbolic mode

Let's see what both of them are.

Absolute (Numeric) Mode of Permission

In the absolute mode, permissions are represented as numeric. For this reason, it is also called the numeric mode. Eight different permissions are represented using the numbers from 0 to 7, so it is also known as the octal permission representation.

The table below shows the different permissions and their corresponding numeric value.

Number

Permission

Symbol

0

No permission

- - -

1

Execute

- -x

2

Write

-w-

3

Execute and Write

-wx

4

Read

r- -

5

Read and Execute

r-x

6

Read and Write

rw-

7

Read, Write and Execute

rwx

Let us now change the permission of a file and see how it works. 

Linux terminal showing the output for the "chmod 764 abcd.odt" command

Here, we have first checked the permission of our file. It is initially "-rw-rw-r- -." Then we run the chmod command with the permission as "764." On checking from the table, the corresponding permission is rwx for the owner, rw- for the groups, and r - - for others. So our output should be, "-rwxrw-r- -." This matches the result obtained above.

This is all about the absolute mode of permissions. Let's see the symbolic mode now.

Symbolic Mode of Permissions

As the name suggests, the symbolic mode of permissions is representation using symbols, but what symbols?

You may have noticed that the symbols for the corresponding permissions were given in the table provided for the absolute mode of permissions. Apart from those symbols, there are a few more in the symbolic mode.

Firstly, in the absolute mode, we simultaneously changed the permissions for the owner, groups, and others. Here, we can only change the permissions for one of them at a time. Thus, there are symbols representing the owner, groups, and others.

Symbol

User

u

owner

g

groups

o

others

a

all

Next, the symbolic mode gives the provision to add, remove and set permissions. The symbols for those are.

Symbol

Description

+

Adds permission

-

Removes permission

=

Sets permission

Just looking at these seems confusing, doesn’t it? Let’s run some commands to get a clear picture.

Adding Permissions

Linux terminal showing the output for the "chmod u+x abcd.odt" command

Here, we can see that the initial permission is “-rw-rw-r- -.” The chmod command run is “u+x.” So, we are adding the execute permission for the owner. Thus, the new permission should be “-rwxrw-r- -” which corresponds with our output.

Removing Permissions

Linux terminal showing the output for the "chmod g-r abcd.odt" command

Here, we can see that the initial permission is “-rwxrw-r- -.” 

Notice that the change in permission from our previous command is still reflected. 

The chmod command run is “g-r.” So, we are removing the read permission for the groups. Thus, the new permission should be “-rwx-w-r- -” which corresponds with our output.

Setting Permissions

Linux terminal showing the output for the "chmod o=rw- abcd.odt" command

Here, we can see that the initial permission is “-rwx-w-r- -.” 

Notice that the change in permission from our previous command is still reflected. 

The chmod command run is “o=rw-.” So, we are giving others permission to read and write. Thus, the new permission should be “-rwx-w-rw-” which corresponds with our output.

With this, we have completed learning about file permissions in Linux.

Frequently Asked Questions

What are the three types of file permissions in Linux?

The three file permissions in Linux are Read, Write and Execute.

How do we check the file permissions?

The Linux command to check the file permissions is “ls -l FILE_NAME.EXTENSION”.

What are the different modes of changing file permissions in Linux?

There are two ways to change the file permissions in Linux - absolute (numeric) mode or symbolic mode. 

What does chmod 644 mean?

“chmod 644” is the absolute mode of permissions. It means that the owner has read and write permissions, while groups and others have only read permissions. 

What does chmod o=rwx mean?

“chmod o=rwx” is the symbolic mode of permissions. It means that others' permission is set to read, write and execute.

Conclusion

At the end of this article, we finish learning about file permissions in Linux. We also learned how to check the file permissions and change them. What’s next now?

File permissions are just a small part of Linux. Apart from it, there are lots more to learn about, but how will we do that?

Don’t worry, as Coding Ninjas has got you covered with the basics of Linux curated in one place, Coding Ninjas Studio

Apart from that, don’t forget to check out Data Structures and AlgorithmsCompetitive ProgrammingBasics of C++Data Structures and Algorithms in C++, and many more courses on Coding Ninjas.

Happy learning!

Live masterclass