HoganHost blog

[email protected]

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

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

How to install fixperms.sh on your server using ssh

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 hoganhost.com.ng  for this script.

#######################################################
#!/bin/bash
# Script to fix permissions of accounts
# Written by: Joseph Effiok Hogan
# https://hoganhost.com.ng
 
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
#######################################################

OR

you can also use the below code to install fixperms.sh on your server using ssh access

  1. Login to your server using ssh
  2. type nano fixperms.sh
  3. and paste the below code
[QUOTE]#!/bin/bash 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

then control+X to close, and Y to save. then whenever you have permissions problems just ssh to server and type: sh fixperms.sh USERNAME (replace USERNAME with the username of the Cpanel user with the issue) this will fix the permissions of the users account.

Leave a Comment

Your email address will not be published. Required fields are marked *