How to Use the chmod Command in Linux

Confused about how to use the $chmod command in Linux?

In Linux and its distros, access to each file and directory is governed through a set of variables. These variables include permissions, the attributes of the file, and the ownership situation of the said file. This is important as it ensures that only authorized users can access and make changes to the directory and files.  

There are three different user classes defined in Linux and its distros. These are.

  1. The owner
  2. The group
  3. Everybody else

You can choose the type of access permissions you want to give to each class of users. These permissions include.

  1. Read.
  2. Write. 
  3. Execute.

$chmod is the command that you can use to define the access permissions for each file. The usage and understanding of how to use the $chmod command in Linux and its distros can be confusing, so we will be guiding you through the details of $chmod in this article. 

So, let’s get right into it!

Understanding and Viewing File Permissions.

Before we look into how to use the $chmod command in Linux, let’s look at an example. We will tell you how to look at the permissions rights for a file and understand what is shown in the output. To view the permissions, use the following command:

$ ls -l filename
chmod Command in Linux

The first character can either be a dash(-) representing a regular file, a “d” representing a directory, or another symbol to describe a special file type.

For the next 9 characters, an “r”(read), a “w”(write), an “x”(executable), or a dash(-) can be used. The first three characters are the permissions for the owner. The next three are for the group, and the last three are for everyone else. 

Let’s say the first three characters are “rw-”. This means that the owner has read and write permissions but no execution permission. If the second three-character combination is “rwx”. This means that the group has read, writte,n and executed permissions for the file. 

The chmod Command. 

$chmod stands for “Change Mode”. It is used for changing the permission rights for a given file. 

When using the $chmod command, you need to tell the system who are you setting the permission for? Next, you must tell the system whether we add or remove the permission. Lastly, we need to tell the computer which permissions we are setting.

The syntax of $chmod command is such that it answers the questions mentioned above in the same sequence.

$ chmod operators u/g/o/a +/-/= r/w/x/- filename

The characters represent the following.

  • u’: File owner
  • g’: Group
  • o’: Everyone else
  • a’: All users 
  • +’: Permission is being added
  • -’: Permission is being removed
  • =’: Changes the current permissions to the specified permissions in the syntax.

The syntax mentioned above for $chmod is not the only one. You can change the permissions of a file using a combination of numbers/digits as well.

$ chmod operators number filename

The “number” part of the syntax can be a 3-digit or a 4-digit number. The first digit represents the permission given to the owner, the second one represents the permission given to the group, and the third is for everyone else.

The characters r,w,x, and are given values 4,2,1, and 0, respectively. We obtain each user digit by adding these values. For example, if you want to give the owner read, write and execute permissions. You simply add 4,2, and 1. The answer you get is 7, and you use 7 as the first digit in the syntax. You do the same for other user types and get the 3-digit number.

This was all you needed to know about the syntax of $chmod command. Next, we will look at examples of using $chmod. But before we move on to the examples, let’s talk about the basename command in Linux.

When dealing with directory trees, there are situations where you just want to know the name of the base directory or go to the base directory of a given file. You can easily find them out when using small trees, but it can be difficult to find the base directory if the tree is very large. This is where the basename command comes in handy.

Click on the link above to find out exactly how you can use the basename command to your advantage.

Examples of $chmod Command.

We will set permissions for each user type using both the number and character syntax of $chmod. To do the same for your files, take the following steps as a template. 

  1. Open the Terminal.
  2. Use the following command as a template to set read, write, and no execute permissions for the owner, group, and everyone else: 
$ chmod a=rw filename
chmod Command in Linux
  1. You can use the above command for doing it separately for all user types. For that, you will use u,g, and o instead of ‘a’.
  2. Use the following command to remove the write permission for the group: 
$ chmod g-w filename

After pressing Enter, you should see a similar output:

chmod Command in Linux
  1. For adding the write permission for the owner, you will use:
$ chmod u+w filename
  1. To give everyone else all the permissions for a particular file, use:
$ chmod 0=rwx filename
  1. Next, we look at how to use the number syntax of $chmod.

To give the owner all permissions, the group only the read permission and give everyone else all permissions, use the following command. 

$ chmod 727 filename

After pressing Enter, you should see a sequence like this.

chmod Command in Linux
  1. If you want to give zero permissions to a specific user type, just use zero for their respective digit. 

This was a guide on using the $chmod command in Linux. We looked at how Linux has a set of protocols that define how a specific user can access a file. Next, we looked at $chmod and how to use it to change permissions. 

We hope that we were able to help you understand the working of $chmod. If there are any further queries, please let us know in the comments. 

If this guide helped you, please share it. 🙂

Author

Leave a Reply

Your email address will not be published. Required fields are marked *