Before we cover the command, allowing you to change the permissions of your files and folders, let's go over what the permissions are and how do they work.
What are Unix permissions, and what is their purpose?
If you have used a Unix based server, you may have heard of the so-called permissions. They are used to protect files by setting a specific type of access for the classes of users on the Linux server.
Here are the three main categories:
- The owner of the file (called the "User")
- Each file belongs to a particular ownership group (called the "Group")
- Everyone else (called "Other") or you may see it in other sources as "World".
All these users can have up to 3 kinds of access:
- The "Read" access, allowing the specific class(User, Group, or World/Other) to look at the content of a file or directory.
- The "Write" access, providing the class(User, Group, or World/Other) to open and edit the file.
- The "Execute" access, giving a particular class(User, Group, or World/Other) the ability to run the program in the system.
The aggregation of all the above types of access forms the Unix file/folder permissions.
How are these permissions outlined, and where can I see them?
You may find them shown in two ways - either symbolically using letters such as "r" for “read”, "w" for “write”, and "x" for “execute”, or numerically - "4" for “read”, "2" for “write” and "1" for “execute”. These numbers can be added together to form permissions such as "3", "5", "6" and "7" which combine them and create collective permissions. For example "5" would represent read and "execute" because “4 + 1= 5"
Perfect! Now I understand how they work and where they are outlined. How can I change them, though?
There are two ways to do so on a cPanel web hosting server - using an SSH command or from the File Manager. In this article, we will focus on how you can do so using SSH.
Here are the steps:
1. Log into your account using SSH.
2. Please use the Command Line Basics to navigate to the desired directory or file which you wish to modify.
3. Upon reaching your destination, please execute the following command:
chmod <numeric_value/symbolic_value> <file/folder>
Here is what the command may look like for you:
chmod 644 index.php
In this case, we have provided the "User" with read + write permissions, the "Group" with “read” permissions, and the "Others/World" with “read” permissions as well. Of course, you can use symbolic representations as well:
chmod u=rw,g=r,o=r index.php
This command is the equivalent of the above. Each class is separated by a comma, and after the "equal" sign, you will find the specific permissions allowed for each category.
We will finish this article with a command you can use to set your file/folder permissions to 755(folder) and 644(file), which are the correct permissions for web applications hosted at HostArmada:
find . -type d -print0 | xargs -0 chmod 0755 && find . -type f -print0 | xargs -0 chmod 0644
Of course, this command can be changed, depending on your situation, by changing the 0755 and 0644 values with the desired numbers.