How to change File Permissions and Ownership via Command Line

How to change File Permissions and Ownership via Command Line

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

  1. Make sure you have SSH access to your server with root or administrator privileges.
  2. Create a backup of your files before making any modifications.
  3. Check the current permission of your public_html directory; it is usually 0750. Save it because you will need it later.
  4. 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

  1. Run the permission changes (chmod) first and then the ownership changes (chown) to avoid conflicts.
  2. If your site shows errors after applying the changes, verify that the public_html directory permission has been correctly restored.
  3. On servers with CloudLinux and CageFS, the user's group may vary. Confirm the correct group with the id user command before running chown.
  4. Always keep an updated backup before making any bulk modifications on your server.

    • Related Articles

    • How to Block and Manage IPs in CSF via Command Line

      CSF (ConfigServer Security & Firewall) is one of the most widely used firewalls on servers running cPanel/WHM. It allows advanced server access management, blocking or allowing IP addresses from the command line. In this article, you will learn how ...
    • How to Export and Import a MySQL Database Using Command Line

      Having an up-to-date backup of your database is essential to protect your critical information and ensure business continuity. In this tutorial, we will show you how to export a database using the command line. It is important to perform a complete ...
    • Change Reserved Disk Space in Linux

      The Linux operating system reserves a percentage of disk space when mounting a partition. This is done to allow the root user to log in even when the disk is full. This reservation, while useful, may seem like a waste of space, especially on ...
    • How to List All CronJobs for Every cPanel User

      Cron jobs are scheduled tasks that run automatically on the server at specific times. These tasks can include updates, backups, sending scheduled emails, among others. Knowing the active cron jobs for each user gives you a clear view of the automated ...
    • How to Manually Move a cPanel Account to Another Partition

      cPanel includes a native feature called rearrange that allows moving accounts between partitions from WHM. However, this process requires enough free space on the source partition to create a temporary copy, and when the partition is nearly full, the ...