Files and folders with incorrect permissions or ownership can be vulnerable to unauthorized access, potentially compromising the integrity of your data and the security of your website. Additionally, these issues can cause read, write, or execution errors on your site.
In this article, you will learn how to fix file and folder permissions and ownership using the command line. Both processes usually go hand in hand, and it is recommended to run them together to ensure your website functions correctly.
Before you begin
- Make sure you have SSH access to your server with root or administrator privileges.
- Create a backup of your files before making any modifications.
- Check the current permission of your
public_html directory; it is usually 0750. Save it because you will need it later. - Identify the correct user and group for the cPanel account. Generally, both match the cPanel username (for example,
user:user).
Part 1: Fix permissions (chmod)
Change file permissions to 0644
Run the following command to find all files within the directory and its subdirectories and set their permissions to 0644:
find /home/user/public_html -type f -exec chmod 0644 {} \;
Change folder permissions to 0755
Run the following command to find all folders within the directory and its subdirectories and set their permissions to 0755:
find /home/user/public_html -type d -exec chmod 0755 {} \;
Restore public_html permission
The previous steps also modified the permission of the public_html directory. Restore it to its original value with the following command:
chmod 0750 /home/user/public_html
Part 2: Fix ownership (chown)
Why is ownership important?
When a file or folder has an incorrect owner, the server may prevent the cPanel account from accessing them, causing 403 (forbidden) errors, file upload failures, or issues running PHP scripts and other applications.
Change ownership recursively
Run the following command to assign the correct user and group to all files and folders within public_html:
chown -R user:user /home/user/public_html
The -R option indicates that the change will be applied recursively, meaning it will affect all files and subdirectories within the specified path.
Verify the changes
To confirm that the ownership and permissions were applied correctly, you can run:
ls -la /home/user/public_html
Check that the user and group columns display the correct name, and that the permissions match the expected values.
Important: Make sure to replace user with the actual cPanel account name in all commands.
Recommendations
- Run the permission changes (chmod) first and then the ownership changes (chown) to avoid conflicts.
- If your site shows errors after applying the changes, verify that the
public_html directory permission has been correctly restored. - On servers with CloudLinux and CageFS, the user's group may vary. Confirm the correct group with the
id user command before running chown. - Always keep an updated backup before making any bulk modifications on your server.