Reset File and Folder Permissions and Ownership for a user account in Cpanel

Use this script to reset the permissions and ownership of files and folders on a Cpanel user account. This script will iterate through a users Cpanel account files in public_html and set the folder to 0755 and files to 0644.

Why Use This Script?
Sometimes permissions and ownership of folders and files can get conflicted, especially if you are moving accounts to different servers or renaming a Cpanel account. Also, account owners might mistakingly set a folder to 777 which is bad news and unnecessary on a Cpanel server.

NOTE: Before you start you need:

1. Logged in as root user to the WHM server with SSH
2. Know the user name for the account you wish to reset
3. Have already installed the fixperms.sh script at /root/fixperms.sh.

Replace {username} with the username you wish to repair.

/root/fixperms.sh {username}

With the command below you can make a server-wide fix which will iterate through all the users on your server and repair permissions. WARNING: if you have a lot of accounts on the server, this can take a little while.

for i in `ls -A /var/cpanel/users` ; do ./root/fixperms.sh $i ; done

Below is the script to install on WHM server.

Just install it to /root/ on your server and called it fixperms.sh

Thank you to thecpaneladmin.com for this script.

#######################################################
#!/bin/bash
# Script to fix permissions of accounts
# Written by: Vanessa Vasile 5/13/10
# http://thecpaneladmin.com
 
if [ "$#" -lt "1" ];then
        echo "Must specify user"
        exit;
fi
 
USER=$@
 
for user in $USER
do
 
        HOMEDIR=$(grep $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 -R $user:$user $HOMEDIR
                        chmod 711 $HOMEDIR
                        chown $user:nobody $HOMEDIR/public_html $HOMEDIR/.htpasswds
                        chown $user:mail $HOMEDIR/etc $HOMEDIR/etc/*/shadow $HOMEDIR/etc/*/passwd
 
                echo "Setting permissions for user $USER"
 
                      find $HOMEDIR -type f -exec chmod 644 {} \; -print
                      find $HOMEDIR -type d -exec chmod 755 {} \; -print
                      find $HOMEDIR -type d -name cgi-bin -exec chmod 755 {} \; -print
                      find $HOMEDIR -type f \( -name “*.pl” -o -name “*.perl” \) -exec chmod 755 {} \; -print
                fi
done
#######################################################
  • bash, whm, fix permissions, fix perms, perms
  • 3 Users Found This Useful
Was this answer helpful?

Related Articles

Named fails to start with "zone 'domain.com': already exists previous definition (Cpanel DNS Only)

This trick allows you to remove duplicated DNS entries on a DNS Only cluster with Cpanel.As you...

Reset Permissions for directories to 0755 and 0644 on Cpanel

Sometimes, file and folder permissions can get confused, for example, 644 will revert to 664...

What is the difference between Jailed Shell and Normal Shell?

You are reading this article because you have been granted shell access on our servers and want...