What does the chmod 755 command do?

The chmod command modifies the permissions of a file or directory on a Linux system. The three numbers after the chmod command represent the permissions assigned to user owner, group owner and others. The numbers 755 assign read-write-execute permissions to the user ower and read-execute permissions to group owner and others.

In this article I will explain the basics of the chmod command and you will learn how to use it. It’s probably one of the most important Linux commands.

We will also go through an example of the Linux command chmod 755.

Basics of the Chmod Linux Command

First of all, let’s start from the fact that Linux is a multi-user system…

…that’s why setting permissions of files and directories is a must know if you work with Linux.

There are different types of permissions for users and groups:

  • read permissions
  • write permissions
  • execute permissions

And, how can they be set?

The chmod command is used in Linux (and Unix-like systems) to set the permissions of files and directories.

First of all, here is the generic syntax of the chmod command:

chmod <permissions> <file or directory>

The permission part of the command can have different formats. One format is a group of number like the one you see below:

chmod 755 <file or directory>

So, we have assigned 755 to the permissions part of the command.

But, what does it mean?

User Owner and Group Owner

To understand that let’s use the ls command in the current directory to look at the details that Linux provides about a file and a directory:

[myuser@localhost ~]$ ls -al
total 2592
drwxrwxr-x  5 myuser mygroup  4096 Nov 10 16:05 .
drwx------  6 myuser mygroup   225 Mar 10 23:50 ..
-rwxrwxr-x  1 myuser mygroup    39 Oct 26 13:13 test_script.sh 
drwxrwxr-x  6 myuser mygroup    98 Nov  8 18:09 data 

I have highlighted in bold the file and the directory we are looking at.

Which one is the file and which one is the directory?

The left hand part of the line starts with a d for a directory and with a dash ( – ) for a file.

The fragment of the lines I have highlighted shows the user owner and group owner, in this specific case the user owner is myuser and the group owner is mygroup.

The user owner is the user who has created the file or the directory. The group owner defines the group that has access to the file or directory.

The idea of a group is that multiple users can belong to it and hence have the same level of access.

For instance, an example of group in a company could be the marketing team whose members should have the same level of access to files and directories. This access is defined by a set of permissions.

The same permissions that as mentioned before can be set with the chmod command.

Read, Write and Execute Permissions

So, let’s look again at the output of the ls command:

[myuser@localhost ~]$ ls -al
total 2592
drwxrwxr-x  5 myuser mygroup  4096 Nov 10 16:05 .
drwx------  6 myuser mygroup   225 Mar 10 23:50 ..
-rwxrwxr-x  1 myuser mygroup    39 Oct 26 13:13 test_script.sh
drwxrwxr-x  6 myuser mygroup    98 Nov  8 18:09 data 

This time we focus on the first part of the output, the permissions assigned to the file and the directory.

Here is how you can break down the permissions:

  • Character 1: as we said before it indicates if this is a file () or a directory (d).
  • 2-4: permissions for the user owner (in this case rwx)
  • 5-7: permissions for the group owner (in this case rwx)
  • 8-10: permissions for others (any user who is not the user owner or is not part of the group owner – in this case r-x)

It’s time to cover the basic values for permissions (characters between 2-10):

  • r: read permission
  • w: write permission
  • x: execute permission

The dash () shows permissions that are not set (e.g. r-x indicates that the read and execute permissions are set but the write permission is not set).

So, if we go back to our file:

-rwxrwxr-x  1 myuser mygroup     39 Oct 26 13:13 test_script.sh

The user owner has read-write-execute permissions, the group owner has read-write-execute permissions and others have read-execute permissions (the write permission is not set).

And the same applies to the directory:

drwxrwxr-x  6 myuser mygroup     98 Nov  8 18:09 data

So, always remember to read permissions as groups of three letters that always follow the same logical order: r (read), w (write) and x (execute).

The Octal Representation of Permissions Used by Chmod

So, how is this related to the initial question?

What does the command chmod 755 mean?

The permissions we have seen expressed using the letters r, w, x can also be expressed with an octal representation (using the numbers 0 through 7).

And that’s exactly what 755 is, the octal representation of a set of permissions for user owner, group owner and others.

The octal number comes from the sum of the following numbers:

  • 4 for the read permission
  • 2 for the write permission
  • 1 for the execute permission

Where permissions for user owner, group owner and others are considered separately.

In other words the sum of the permissions for the user owner will produce one octal number (from 0 to 7) and the same applies for the permissions for the group owner and for others.

That’s why we end up with 3 octal numbers (there could be a fourth number before them but it’s out of scope for this tutorial).

Going back to 755, here is what it means:

  • 7: 4 + 2 + 1 = read + write + execute permissions
  • 5: 4 + 1 = read + execute permissions
  • 5: 4 + 1 = read + execute permissions

Does it make sense?

How would you translate the permissions 644 instead?

Also remember to stay away from the permissions 777 that gives full access to everyone.

Using the chmod 777 command on files or directories is not a recommended practice for obvious security reasons.

Using the Chmod 755 Command on Files and Directories

Now that we know what 755 means, let’s have a look at the effect of this set of permissions on a file and on a directory.

We want to understand what read, write and execute permissions do from a user perspective when applied to files or directories.

  • Read permission:
    • file: read the content of the file
    • directory: list the content of the directory
  • Write permission:
    • file: modify the content of the file
    • directory: add, rename and delete files in the directory (assuming that the execute permission is set on the directory)
  • Execute permission:
    • file: define an executable file (e.g. a Bash script)
    • directory: access the directory

So, you now know what the command chmod 755 means applied to files and directories.

To get full understanding of it try to create a file and a directory and experiment with different permissions.

See any errors returned by the Linux shell when you execute an operation not allowed on a file or directory based on the permissions you assigned to it.

Recursive Chmod 755 Command

In all our examples so far we have only used the chmod 755 command followed by the set of permissions and by the file or directory we apply those permissions too.

Sometimes you might want to assign the same permissions to a directory and to all the files and subdirectories under it.

How can you do it?

The chmod command provides a flag that allows to apply the change in permissions recursively to anything under the directory you apply the command to.

Let’s see an example…

…I have created a directory called test_dir:

drwxr-xr-x   5 myuser mygroup  160 Jul 23 23:56 test_dir

As you can see the permissions for the directory are rwx (7), r-x (5) and r-x (5).

Then inside this directory I have created two subdirectories (test_dir1 and test_dir2) and one file (testfile).

[myuser@localhost ~]$ ls -al test_dir/
total 0
drwxr-xr-x  5 myuser mygroup   160 Jul 23 23:56 .
drwxr-xr-x  3 myuser mygroup    96 Jul 23 23:56 ..
drwxr-xr-x  2 myuser mygroup    64 Jul 23 23:56 test_dir1
drwxr-xr-x  2 myuser mygroup    64 Jul 23 23:56 test_dir2
-rw-r--r--  1 myuser mygroup     0 Jul 23 23:56 testfile

If I want to make sure everything inside the test_dir directory has permissions set to 755, I can use the chmod command with the -R flag:

chmod -R 755 test_dir

And here is the content of the directory after running the command:

[myuser@localhost ~]$ ls -al test_dir/
total 0
drwxr-xr-x  5 myuser mygroup   160 Jul 23 23:56 .
drwxr-xr-x  3 myuser mygroup    96 Jul 23 23:56 ..
drwxr-xr-x  2 myuser mygroup    64 Jul 23 23:56 test_dir1
drwxr-xr-x  2 myuser mygroup    64 Jul 23 23:56 test_dir2
-rwxr-xr-x  1 myuser mygroup     0 Jul 23 23:56 testfile

As you can see the permission of the file testfile have been updated from rw-r–r– (644) to rwxr-xr-x (755).

Makes sense?

Conclusion

We have covered quite a lot of concepts in this article. Now you should know:

  • The purpose of the chmod command.
  • What is the number 755 applied to chmod.
  • Which command gives user and group owner certain permissions.
  • How to map read, write and execute permissions with their octal representation.
  • The meaning of chmod 755 applied to files and directories.

Do you have any questions? 🙂

Leave a Comment