In this guide, we will be showing you how to make use of the chmod command in Linux.
In Linux, you will often need to make use of the chmod
command. Chmod stands for “Change Mode” and is used to modify the permissions of files and directories in a Linux based system.
By using this command, we can set the read, write, and execute permissions for all three of the permission groups (Owner, Group and Other) in Linux.
The command is relatively simple to use and involves using chmod
, followed by the permissions you want to set, then the name of the directory or file you want to modify.
chmod [OPTIONS] PERMISSIONS FILE
With the chmod command, there are two different notations that you can utilize to specify the permissions that you want to set.
You can either use numeric (Number based) or symbolic (Letter based) notation to define your permissions with this command.
In this chmod guide, we will run you through both of these notations so that you can decide when its best to use one over the other.
Table of Contents
Changing Permissions Using Numeric Notation
Using numeric notation, you will be making use of numbers such as 777
to represent permissions.
With numeric notation, each of the three digits represents an individual permission group.
The order that these permission groups are defined is the following.
- The first digit is the permissions for the Owner of the file/directory.
- The second digit represents the permissions for the Group that owns the file/directory.
- The third digit holds the permissions for the Other permission group.
With numeric notation each permission is assigned a numerical value, you can see the value assigned to each of the permissions by looking at our table below.
Number | Usage | Description |
---|---|---|
4 | Read permission | Allows reading the contents of a file or directory. |
2 | Write permission | Allows creating new files in a directory or modifying the contents of a file. |
1 | Execute permission | Allows you to “run” a file. |
0 | No Permissions | Setting a permission group to 0 means they won’t be able to do anything with the file/directory. |
If you want to set multiple permissions to a file or directory, then you need to add the numeric values of each permission together.
Calculating Numeric Permissions
In this section, we are going to run you through some quick examples of adding together the permission values to set your desired permissions.
Calculating the numeric permissions is a reasonably straightforward process to understand and is as simple as just adding the numeric value of each permission together.
Owner Permissions Example: Read | Write | Execute
For example, if we wanted to assign the owner the read (4), write (2), and execute (1) permissions, we need to add the numeric value that represents each permission.
4 + 2 + 1 = 7
Group Permissions example: Read | Execute
Likewise, if we only want to give the group the right to read (4) and execute (1) the file, then we need to add those two numeric values together.
4 + 1 = 5
Other Permissions example: Read
Now to finish it up, let’s say that we wanted all other users to only be able to read (4) the file, then we need to use the read permission value in its place.
4 = 4
Putting it All Together
Now to get our final permissions mode that we can set using the chmod
command, we need to put each of our permission group’s values next to each other.
The order these values should be put together is the following: Owner, Group, Other.
754
Numeric Permission Table
Alternatively, you can make use of our table below to see the different number combinations and the permissions that they set.
Number | Permissions | rwx |
---|---|---|
0 | No Permissions | ‐‐‐ |
1 | Execute Only | ‐‐x |
2 | Write Only | ‐w‐ |
3 | Write and Execute | ‐wx |
4 | Read Only | r‐‐ |
5 | Read and Execute | r‐x |
6 | Read and Write | rw‐ |
7 | Read, Write and Execute | rwx |
As you can see there isn’t a huge amount of combinations that you have to remember.
With a bit of practice, you will be able to write down the correct number for the permissions you want without having to do any calculations.
Reading Numeric Permissions
Once you have a basic understanding of the numeric notation system, reading the permissions becomes relatively simple.
To work out the permissions that are set, we need to do the opposite of what we did earlier.
This means we need to subtract each of the permissions numerical values to see if it has been set.
For example, let’s use the number 7 as our permission value.
permission = 7
To start, we should first check to see if the read permission has been set as it has the largest numerical value.
If the permission value is greater than or equal to 4, then it contains the read permission.
Subtract 4 from your value. If your value is not 0, then you can proceed to check for both the Write permission and the Read permission.
permission = 7 - 4 = 3
Next, we can check if the value contains the write permission. If the value is equal to or greater than 2, then it will have the write permission.
Subtract 2 from your value and continue if the resulting value is not equal to 0.
permission = 3 - 2 = 1
Finally, if your value is equal to 1 after all other subtractions, then we can tell that the execute permission is set.
Dealing with the full permission string is just as simple.
All you need to do is separate the three digits into there three separate groups to work out the permissions being set for the Owner, Group, and Other permission groups.
The more you practice this, the faster you will be able to work out what permissions are being set based on the numerical value.
Example of using Numeric Notation With chmod
In this section, we are going to run you through an example of using numeric notation with the chmod
command to set permissions.
You will find that numeric notation is especially useful when you want to set permissions for all of the three permission groups at once.
For this example, we are going to be setting the following permissions.
- Owner – Read (4), Write (2), Execute (1) – 7
- Group – Read (4), Execute (1) – 5
- Other – Read (4) – 4
To add multiple permissions to a group, you need to add the numeric value of the permissions together.
Using the values we have calculated for each permission group, we can run the following command to set all of the permissions for a file called numerictest.
chmod 754 numerictest
As you can see, the Owner group has the Read(4), Write(2), and Execute(1) permissions set. Combining the numeric value of each of these permissions gives us the value 7.
Next, the Group permission group has the Read (4)and Execute (1) permissions set, combining these values means we set these using the value 5.
Finally for the Other permission group we just need to use the value for the Read permission, which is 4.
Changing Permissions Using Symbolic Notation
Instead of using the numeric notation with the chmod
command, you can also make use of the symbolic notation.
chmod [GROUP][OPERATOR][PERMISSIONS] examplefile
Permission Groups with Symbolic Notation
With the symbolic notation, permissions are applied to permission groups based on four different letters
The chmod
command uses these letters to apply the permissions to the desired permission group.
You can see the group that each letter represents by using the table below.
Letter | Group | Explanation |
---|---|---|
u | Owner | File/Directory Owner |
g | Group | File/Directory Group Owner |
o | Other | Users who aren’t the owner or a member of the group that owns the file |
a | All | Equivalent to using ugo . Sets permissions for all three groups. |
You can combine letters to modify multiple permission groups at once.
For example, to modify both the Owner(u
) and Other(o
) permission groups, you can use their letters like so: uo
.
Permission Operators with Symbolic Notation
Next, we need to look at the three different operators that you can use for the symbolic notation.
These operators dictate how the chmod command should apply the permissions to the specified permission groups.
Operator | Explanation |
---|---|
+ | Adds the specified permissions to the permission groups |
– | Removes the specified permissions from the permission groups |
= | Replaces all permissions of the permission groups with the ones that are specified. |
These operators are relatively simple to understand and work very similarly to basic arithmetic.
Permissions with Symbolic Notation
Finally, we need to take a look at the letters that represent the permissions we want to utilize.
You will be familiar with these letters if you have followed our file permissions in Linux guide.
Using the table below, you can see the individual letter that represents each of the three permissions.
Permission Letter | Permission | Explanation |
---|---|---|
r | Read Permission | Allows the group to read a file or list the contents of a directory |
w | Write Permission | Allows the group to write to the file or directory |
x | Execute Permission | Allows the group to execute a file or list the contents of a directory. |
Now that we have an understanding of all the different operators that we can use and the letters that represents the permission groups and permissions, we can use these in practice.
Examples of Symbolic Notation with chmod
The symbolic notation is incredibly simple to use and is especially useful when you want to add or remove specific permissions without making changes to the other permissions.
Unlike numeric notation, you do not need to specify all permissions for each of the permission groups.
Adding a Permission using Symbolic Notation
To show you this behavior, we are going to add the write permission to the Owner permission group for a file called examplefile
.
chmod u+w examplefile
Breaking this down, you can see that we use the u
character to specify that we want to modify the owner permission group.
Next, we use the addition(+
) operator as we want to add the permission to our group.
Finally, we use w
to specify that we want to add the write permission.
Removing a Permission using Symbolic Notation
Removing a permission using symbolic notation is just as easy as adding a permission.
For this example command, we are going to remove the read permission from the other permission group for a file called nowrite
.
chmod o-r nowrite
Breaking down this command, we start by using the letter o
to specify that we want to modify the Other permission group.
We then proceed to use the minus(-
) operator to tell the command that we want to remove the specified permission from our group.
Our final letter, r
specifies the permission we want to remove, is the read permission.
Setting the permissions using Symbolic Notation
While you are able to add or remove specific permissions, you can also set the permissions using symbolic notation.
By setting permissions, you are replacing all existing permissions with the ones specified.
For our example command, we are going to give the group permission group, read and write privileges on a file called readandwrite.
chmod g=rw readandwrite
As you can see, we start by first specifying the group permission group by utilizing the g
letter.
We then use the equals (=
) operator to tell the chmod command that we want to set our permission groups permissions to the specified ones.
Finally, we specify the two permissions that we want to set by using the letter r
for the read permission and the letter w
for the write permission.
You can see how you can easily specify more than one permission by just combining the letter for each different permission.
Using Multiple Permission Groups with Symbolic Notation
The last thing that we need to touch on for symbolic notation with the chmod
command is how you can use multiple groups.
Luckily this is a super simple process and involves combining the letter that represents each permission group.
For example, if we wanted to add the execute permission to both our Owner and Group permission groups for a file called usergroup
then we can use the following.
chmod ug+x usergroup
You can see that all we needed to do to add the permissions to two different groups was to combine the letter for each permission group.
Here we used u
for the owner permission group followed by g
for the group permission group.
We then proceed to do everything just as we did in the previous examples, using the addition operator (+
) to add a new permission then using the letter x
to specify that we want to add the execute permission.
Conclusion
Hopefully, at this point, you will now have an idea of how to use numeric or symbolic notation with the chmod command.
While the Linux permission system can prove a little daunting at first, the chmod command makes controlling it straightforward.
This guide should prove to be a valuable resource for whenever you need to work out how to remove or add permissions.
If you have run into any issues or need any assistance, then feel free to drop a comment below.
You can also find out other commands you can use on Linux by checking out our other guides.