The files and directories on your account have permissions. As a rule, you do not have to remember about them, because they are assigned automatically, but there may be a situation where you need to change permissions for a file or directory, for example to protect it against being overwritten.

How to decode the permission code?

When checking the permissions of files or directories, you can find a description in two formats. One of them consists of three numbers, the other of letters.
At first, it may seem confusing, but just remember that each file or directory can have three forms of permissions based on a yes/no basis:

  • Read
  • Write
  • Execute

In addition, there is a division into the next three elements, so you define who is allowed to perform the operations on the fie or directory and to whom the rights apply to:

  • Owner
  • Group
  • Others

We have mentioned that the record of permissions can take the form of text and numbers.

The textual form takes a notation based on the first letters of the words: read, write and execute (exceptionally it is the letter x).

Examples:

rwx rwx rwx
 |   |   |
 ----------- r - read -- w - write -- x - execute -- owner
     |   |
     ------- r - read -- w - write -- x - execute -- group
         |
         --- r - read -- w - write -- x - execute -- others

Above is an example that will make a file or directory have full permissions.

Similarly, by deactivating individual options, we can turn on and off the ability to write, read and execute:

rw- r-- r--
 |   |   |
 ----------- r - read -- w - write -- x - execute -- owner
     |   |
     ------- r - read -- w - write -- x - execute -- group
         |
         --- r - read -- w - write -- x - execute -- others

The file to which we give the permissions presented above will be readable by anyone, but no one except the owner will be able to make any changes.

The numerical form works on a similar principle where a specific permission have a digit assigned, which creates a summary of the permissions specified for thw owner, group and others. To make it easier to remember, note that first permission of being able to read the file starts from 4 and as we go along, we divide each subsequent permission by two, finally adding them up.

Examples:

421 421 421
 |   |   |
 ----------- 4 - read -- 2 - write -- 1 - execute -- 4+2+1= 7 - owner
     |   |
     ------- 4 - read -- 2 - write -- 1 - execute -- 4+2+1= 7 - group
         |
         --- 4 - read -- 2 - write -- 1 - execute -- 4+2+1= 7 - others

The example above will result in obtaining a file with full read, write and execute permissions, expressed numerically with 777.

Default permissions:

Default permissions suggested for files is 664 which resolves to file giving the owner ability to read and write and everyone else just to read the file.

420 400 400
 |   |   |
 ----------- 4 - read -- 2 - write -- 0 - execute -- 4+2+0= 6 - owner
     |   |
     ------- 4 - read -- 0 - write -- 0 - execute -- 4+0+0= 4 - group
         |
         --- 4 - read -- 0 - write -- 0 - execute -- 4+0+0= 4 - others

For directories, the suggested default permissions are read, write, and execute for the owner, and read, and execute for the group and others. So in this case we end up with 755.

421 401 401
 |   |   |
 ----------- 4 - read -- 2 - write -- 1 - execute -- 4+2+1= 7 - owner
     |   |
     ------- 4 - read -- 0 - write -- 1 - execute -- 4+0+1= 5 - group
         |
         --- 4 - read -- 0 - write -- 1 - execute -- 4+0+1= 5 - others

What command to use on the SSH?

First you have to be sure that you have access to the SSH (Secure Shell). If you do not own a VPS or dedicated server and you are not sure if you have SSH access whatsoever, please contact your hosting provider and ask.

Change permissions for a single file:

For example, with the command below, we will change the file named your-file.php, giving it the default suggested permissions which is 644.

Similarly as needed, you can modify the permissions by selecting a different combination of read, write and execute for owner, group and others.

chmod 644 your-file.php

Change permissions for all files in the directory you are in:

In a case where you are not sure which files have incorrect permissions, you can use the command below to search for and set permissions of 644 for all files in the directory you are currently in and in any underlying directories.

find . -type f -print0 | xargs -0 -n 1 chmod -v 644

Change permissions for a directory:

For example, with the command below, we will change the directory named your-directory, giving it the default suggested permissionswhich is 755.

Similarly as needed, you can modify them by selecting a different combination of read, write and execute permissions for owner, group and others.

chmod 755 your-directory

Change permissions for all directories within the directory you are in at the moment:

In a case where you are not sure which directories have incorrect permissions, you can use the command below to search for and set permissions of 755 for all directories in the directory you are currently in and any underlying directories.

find . -type d -print0 | xargs -0 -n 1 chmod -v 755

How to grant or change permissions using an FTP client application?

In our example we will use the application named WinSCP to connect to the FTP server. You should receive all credentials needed fro your hosting provider. If you don’t have them, please contact the support team to hep you out.

You can download WinSCP client by clicking HERE.

After logging in, a list of directories and files on your server will appear in the application window.

Right-click on the selected file or directory and select the last item from the menu which will appear – Properties. You can also use the F9 functional key on your keyboard.

In the window that will appear, you will find a table which will allow you to make changes to the permissions of the selected element by activating or deactivating the selected values.

Changing the permissions of a file named my-file.php

Changing the permissions of a directory named my-directory

Now you know pretty much everything about the permissions or chmod as it’s called.
Have fun and if you like this article and found it helpful, please give it a heart, leave a comment and share with your friends. Take care.

Author

I’m Michael , a tech geek. Join me on the journey through interwebs to find latest news, tips and hints.

Write A Comment