How To Use The Chmod Command In Linux

Command-line has an important role in the Linux OS. You can do everything through the terminal. There are so many Linux commands you can use. Chmod is one of those Linux commands.

It’s a very important command. You might need to use this command occasionally. This is why I am here with a beginner’s guide on how to use the chmod command in Linux.

The main purpose of this command is to modify file permissions. File permissions control how a file should behave. Linux control every directory and file using the permissions. Chmod can modify these permissions. It is an essential part of the Linux OS.

Therefore, if you want to learn chmod then you are in the right place. Here I will explain the different ways to use the chmod command in Linux. So let us get started.

What Are File Permissions And Chmod Command In Linux

Chmod is an easy command in Linux. However, it becomes difficult when you use all of its variations. This command executes in so many ways. Nevertheless, you need to know about file permissions. File permissions decide whether a file is readable, writable, or both.

Understanding File Permissions Attributes

To use the chmod command in Linux. First, you should know what permissions a file has. You need to execute ls –l command. Go to the file location using the cd command and then execute ls –l command. Here you can see the file permission attributes given to a file or folder.

From the left-hand side, you will see characters like “drwx –rw-xw” or “–rwx –rw r-“. These are the file permission attributes that the chmod command uses in Linux. Here is what they mean.

  • If the line starts from “d” then it is a directory. If it starts from “-” then it is a file.
  • After ‘”d” or first “-”, the first three characters are the permission given to the user.
  • The second three characters are the permissions given to the group of users.
  • The last three characters are the permission given to public accounts or other permissions.

You got the idea about the line that determines permissions. And how the chmod command determines what to do in Linux.  Now it’s time to understand what these attributes stand for.

  • “r” means the file is readable.
  • “w” means the file is writable.
  • “x” means the file is executable.
  • “-” means no permissions have been assigned to the file or folder.
  • If you see all the attributes “rwx” then all the permissions are there for the file or folder.

It is all you need to know about file permission. Now it is time to learn how to use the chmod command.

Before you Start Using Chmod Command In Linux

These are three things you need to keep in mind. First, the user to which you are giving the command. Second, a sign that determines what permissions you want to add or remove.

Third, the permission itself that you want to set. First, you need to know about the attributes that determine the output of the command.

First, is the user or group of users whom you want to give permissions.

To choose a user you want to give permission using the chmod command in Linux.

  • “u” means the user himself.
  • “g” means a group to that you want to give permissions.
  • “o” means other users that are under the above two.
  • “a” means all the users.

Second, is the sign that determines the addition or removal of permission.

To add or remove permission.

  • “+” means that you want to grant permission.
  • “-” means you want to remove the permission.
  • “=” means to set the given permission and remove others.

Third, you need to know what permissions you can give.

To choose what permission to give.

  • “r” means only readable.
  • “w” means only writable.
  • “x” means only executable.

Phew!! That’s all about it. You are all set to use the chmod command. Now I will explain how to use the chmod command in Linux.

How To Use The Chmod Command In Linux

  • First, open the terminal.
  • Then use the cd command to go to the directory where the file you want to edit is.
  • Now use the following command to see the permission granted to the file.

Ls –l filename

  • Now you just need to use the attributes explained above. Use the following example to execute the chmod command in Linux.

Chmod {user}{add or remove permission}{permoission}

  • Now to add permission for group users.

chmod g+rw filename

  • To remove

chmod g-rw filename

  • To add the given permission and remove others

chmod g=x filename

This is how you can use the chmod command in Linux. you can also use this method to set permission for other users.

  • To add or remove permission from admin users or other users.

chmod u+rw filename

or

chmod u-r filename

or

chmod o=x filename

  • You can use the “a” attribute to give permission to all the users.

chmod a=rw filename

Using numbers To Use The Chmod Command In Linux

You can also use numbers instead of “r” “w” “x” to use chmod. Here are the numbers that determine the permission.

Number

Permission

0

1

–x

2

-w-

3

-wx

4

r-

5

r-x

6

rw-

7

rwx

Above is the table you can use to determine what number dos what.

You can use the following command to give all permissions to all users.

chmod 777 filename

I hope you find this article helpful to know that Chmod is an essential command in Linux. You can set or change permission using this command.

For more related articles read our what is Windows Subsystem for Linux [WSL] and you can also read this must have Apps for Ubuntu Linux and many more.