Script to fix account owner and permissions on cPanel
Many times I have encountered an issue that requires to fix account owner and permissions on cPanel. For example when you are migrating a website or when you are restoring an account from backup files.
I have found this script while looking into an automated solution to fix a bunch of accounts that had the owner and group wrongly set by a team mate.
This script also can be used to reset files and directories permissions to defaults when a customer messes with them.
How to run the script
Just paste the script code into a new file, give it user execution permits and run it like this:
vi fixuserperms.sh chmod u+x fixuserperms.sh ./fixuserperms.sh USER_ACCOUNT
It requires to run as root in order to be able to fix the user permissions that were changed to another user account.
Script code to fix account owner and permissions
#!/bin/bash # Script to fix permissions of accounts # Written by: Vanessa Vasile 5/13/10 # http://thecpaneladmin.com # Revised by Andrej July 13, 2017 at 5:02 pm # Updated and revised by LinuxAdminGeeks.com 30/03/2021 if [ "$#" -lt "1" ] || [[ $(echo $@ | egrep "\b(root|bin|nobody|cpanel|halt|system)\b") ]]; then echo "Must specify user or invalid user. Usage: $0 [-all | username [username…]]" exit fi USER=$@ if [ "$USER" = "-all" ]; then USER=`\ls -A /var/cpanel/users/ | egrep -v "\b(root|bin|nobody|cpanel|halt|system)\b"` fi for user in $USER; do HOMEDIR=$(egrep "^${user}:" /etc/passwd | cut -d: -f6) if [ ! -f /var/cpanel/users/$user ]; then echo "$user user file missing, likely an invalid user" elif [ "$HOMEDIR" == "" ]; then echo "Couldn’t determine home directory for $user" else echo "Setting ownership for user $user" chown -hR $user:$user $HOMEDIR chgrp -h nobody $HOMEDIR/public_html $HOMEDIR/.htpasswds for docroot in $(grep \ $user\=\= /etc/userdatadomains | awk -F"==" '{print $5}' | grep $HOMEDIR); do chgrp -h nobody $docroot done chown -h $user:mail $HOMEDIR/etc echo "Setting permissions for user $user" find $HOMEDIR -type f ! -path "*/mail/*" ! -path "*/.ssh/*" ! -perm 000 -exec chmod 644 {} \; find $HOMEDIR -type d ! -path "*/mail/*" ! -path "*/.ssh/*" ! -perm 000 -exec chmod 755 {} \; find $HOMEDIR -type d -name cgi-bin -exec chmod 755 {} \; find $HOMEDIR -type f \( -name "*.pl" -o -name "*.perl" -o -name "*.cgi" \) ! -perm 000 -exec chmod 755 {} \; chmod 750 $HOMEDIR/public_html chmod 711 $HOMEDIR for docroot in $(grep \ $user\=\= /etc/userdatadomains | awk -F"==" '{print $5}' | grep $HOMEDIR); do chmod 750 $docroot done if [ -d "$HOMEDIR/.cagefs" ]; then chmod 775 $HOMEDIR/.cagefs chmod 700 $HOMEDIR/.cagefs/tmp chmod 700 $HOMEDIR/.cagefs/var fi fi done
I used this script in CentOS 7.9 with Cloud Linux installed, this script is provided ‘as-is’ and there is not guarantee that it will work with your system setup.
My recommendation is that you run it in a test server first and then give it a run in your production server as I did.
Conclusion
This user fixer owner and permits script is a big help for anyone doing cPanel hosting, it has saved me a bunch of time for me. I hope it works for you too.
If you to want learn more about this tutorial or have any questions, feel free to send your comments down below.
Don’t forget to check our other Tutorials, we are constantly submitting new ones every week.
Tags: fix cpanel account, fix permits cpanel, fix user owner permits cpanel