How To Set Correct Permissions (CHMOD) on Linux with Single Command

DigitalOcean Referral Badge
Start your VPS now with FREE $100 credit.

Setting the correct permissions (CHMOD) for all files and folders in a Linux server environment is very important. The correct permission will secure all files and folders on your website.

Of course, we don’t want our website files and folders to be publicly accessible. For example, WordPress has strict standards permission for both files and folders – 644 for files (600 for wp-config.php and .htaccess files) and 755 for folders.

Well, what happens if we accidentally set file and folder permissions with incorrect values – for example set chmod to 777?

Obviously it will harm your site and will make it easy for someone to get access to your website. That’s why we need to set the correct permissions for both files and folders/directories.

Setting the Permissions on Linux

In this tutorial I will guide you on how to set the correct permissions (chmod) on a Linux server with a single command.

This guide should be used in almost all Linux distributions, including Fedora/RHEL/CentOS and Debian/Ubuntu servers. However, make sure you have root privileges to run this command via the SSH terminal.

READ:  How To Fix CSF Error open3: exec of /usr/bin/systemctl is-active firewalld failed

1. Set Folder Permissions to 755 (-rwxr-xr-x)

$ find /var/www/html/yourdomain.com/public -type d -exec chmod 755 {} \;

This single command will allow you to set the correct folder permissions to 755 (-rwxr-xr-x). This command will also set all sub-folders under the public directory to 755.

If you want to set folder permissions to other values (775 for example) you must change 755 to 775. However, please make sure to change the correct directory path according to your site/server directory path.

For example, the public directory of your site is located at /home/user/domain.com/public_html, here you need to change the directory path from:

$ /var/www/html/yourdomain.com/public

To

$ /home/user/domain.com/public_html

2. Set File Permissions to 644 (-rw-r–r–)

$ find /var/www/html/yourdomain.com/public -type f -exec chmod 644 {} \;

This command will allow you to set the correct file permissions to 644 for all files under the public folder.

With the single command above, you can also change the file permission value to another value, for example you can change from 644 to 600 (this will make your file more secure).

Just like setting the folder permissions above, here you must also set the correct public folder path according to the public directory path of your site. So, you can replace from:

$ /var/www/html/yourdomain.com/public

To

$ /home/user/domain.com/public_html

Leave a Comment