How to fix permissions of Files and Directories on a cPanel Server

How to fix permissions of Files and Directories on a cPanel Server

 

On a linux server permission of website files should be 644 (rw-r–r–) and directories should 755 (rwxr-xr-x). Setting full permission (777) for files and directories under the document root of the website is a big mistake because hackers can modify and execute the files. Setting incorrect permission for files and directories is the main reason why sites are infected and hacked. Go to the document root of your website and run the below commands to check whether you have set incorrect permission for files and directories.

The below command will list all the files which are not having 644 permission

find \! -perm 644 -type f

The below command will show all directories which are not having 755 permission

find \! -perm 755 -type d

Do the below to correct the permission of files and directories under a website. The below command will set file permission as 644 and directory permission as 755.

Note : The below commands should be run ONLY within document root of the website. Running the command outside document root of your website will lead to issues.

Command to correct the permission of directories under a website :

find . -type d -exec chmod 755 {} \;

Above command will set 755 permission for all directories.

Command to correct the permission of files under a website :

find . -type f -exec chmod 644 {} \;

Above commands will set 644 permission for all files and 755 permission for Directories.