
File permissions are crucial for the security and functionality of your website. Incorrect permissions can lead to errors, security vulnerabilities, or even prevent your site from loading properly. If you’re using cPanel, you can easily fix file permissions using a simple shell script.
Steps to Fix File Permissions in cPanel
1. Create a Shell Script
You can use the nano
text editor to create and edit a shell script. Run the following command:
nano fixperms.sh
2. Paste the Script Code
Copy and paste the following code into the fixperms.sh
file:
#!/bin/bash # Set defaults USER_DIR="/home" for user in $(ls -1 $USER_DIR); do if [ -d "$USER_DIR/$user/public_html" ]; then echo "Fixing permissions for $user" chown -R $user:$user $USER_DIR/$user/public_html find $USER_DIR/$user/public_html -type d -exec chmod 755 {} ; find $USER_DIR/$user/public_html -type f -exec chmod 644 {} ; fi done
Explanation of the Script:
Explanation of the Script:
- The script scans the
/home
directory for all users. - For each user with a
public_html
folder, it:- Sets correct ownership (
chown -R
). - Changes directory permissions to 755 (read, write, execute for owner; read and execute for others).
- Changes file permissions to 644 (read and write for owner; read-only for others).
- Sets correct ownership (
3. Save and Exit
After pasting the code:
- Press CTRL+S to save the file.
- Press CTRL+X to exit
nano
.
4. Make the Script Executable
Before running the script, you need to grant execute permissions:
./fixperms.sh
5. Run the Script
Execute the script with the following command:
./fixperms.sh
The script will process all user accounts in /home
and fix permissions for their public_html
directories. The time taken depends on the number of accounts on your server.
Conclusion
Fixing file permissions in cPanel is a straightforward process with this script. Regularly checking and correcting permissions helps maintain security and prevents website issues. Always back up your data before running scripts to avoid accidental data loss.
For more advanced permission fixes, consider consulting your hosting provider or server administrator.
Delloweb International – Your Trusted Hosting and Server Management Partner.
Would you like a version of this script for specific directories or custom permission sets? Let us know in the comments! 🚀