Changing File and Folder Permissions in Linux
We all know the chmod command in Linux to change file and folder (directory) permission recursively to the same permission but what if you need the file and folder permission to be different. Here are the commands to do just that.
To change the permission of the folders and files (recursively)
The following command changes the file and folder permissions recursively for the folder, all subfolders and all files in the path.
chmod -R 777 /path/to/folder OR chmod -R 777 /var/www/vhosts/httpdocs
But, what if you need the folders and files to have different permissions. Read on to find out more.
Scenario: Moodle Folder Permissions Security Recommendation
A case in point, Moodle recommends the following permissions for its folders and files if you are running it on a dedicated server. To help tighten the security of your Moodle system the following are recommended:
1. moodledata directory and all of its contents (and subdirectories, includes sessions):
owner: apache user (apache, httpd, www-data, whatever; see above) group: apache group (apache, httpd, www-data, whatever; see above) permissions: 700 on directories, 600 on files
2. moodle directory and all of its contents and subdirectories (including config.php):
owner: root group: root permissions: 755 on directories, 644 on files.
If you allow local logins for regular users, then 2. should be:
owner: root group: apache group (apache, httpd, www-data, whatever; see above) permissions: 750 on directories, 640 on files.
To change the owner, use the chown command e.g.
chown -R apache /path/to/folder OR chown -R apache /var/www/vhosts/httpdocs
To change the permission of the directory (d)
Based on 1 above, to change the directory permission for the moodledata directory and all of its subdirectories, your command should look like:
find /var/www/vhosts/domain/moodledata -type d -exec chmod 700 {} \;
Note: if you have installed moodle into a subfolder or as a sub-domain, be sure to use the correct directory path.
Based on 2 above, to change the directory permission for the moodle directory and all of its subdirectories, your command should look like:
find /var/www/vhosts/domain/httpdocs -type d -exec chmod 755 {} \;
The alternative would be:
find /var/www/vhosts/domain/httpdocs -type d -exec chmod 750 {} \;
if you allow local logins for regular users.
To change the permission of the files (f)
Based on 1 above to change the file permission in the moodledata directory and all its subdirectories, your command should look like:
find /var/www/vhosts/domain/moodledata -type f -exec chmod 600 {} \;
Note: if you have set your moodledata folder in a different location, be sure to use the correct directory path.
Based on 2 above, to change the folder permission for the moodle directory and all its subdirectories, your command should look like:
find /var/www/vhosts/domain/moodle -type f -exec chmod 644 {} \;
The alternative would be:
find /var/www/vhosts/domain/moodle -type f -exec chmod 640 {} \;
if you allow local logins for regular users.
Here is a chmod calculator if you need to convert between numerical to symbols.