Skip to main content

WHM: Script to Correct Ownership and Permissions for cPanel Accounts

A step-by-step guide to setting up a script that corrects the ownership and permissions for a cPanel account.

Scott avatar
Written by Scott
Updated over 2 months ago

If you’re experiencing permission or ownership issues with a specific cPanel account after a migration or restore, you can easily fix them by updating the ownership and permissions for that user. The process only takes a few minutes and can be done directly from your server via SSH. Follow the steps below to create and run a simple script that will automatically correct the ownership and permission settings for the selected cPanel account.

  1. Access your server via SSH. If you’re not sure how to do this, please refer to our support article .

  2. Create a file named updateownership.sh in your server’s home directory (or any preferred location).

  3. Add the following code to the newly created updateownership.sh file.

    #!/bin/bash


    if [ "$#" -lt "1" ];then
    echo "Must specify user"
    exit;
    fi

    USER=$@

    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 -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

    chmod 750 $HOMEDIR/public_html

    if [ -d "$HOMEDIR/.cagefs" ]; then
    chmod 775 $HOMEDIR/.cagefs
    chmod 700 $HOMEDIR/.cagefs/tmp
    chmod 700 $HOMEDIR/.cagefs/var
    chmod 777 $HOMEDIR/.cagefs/cache
    chmod 777 $HOMEDIR/.cagefs/run
    fi
    fi
    done

  4. Make the updateownership.sh file executable by running the command below.

    chmod +x updateownersip.sh

  5. Now, run the command below with your cPanel username; it will update the ownership and permissions for that specific user.

    bash updateownersip.sh cpaneluser

  6. Congratulations, you’re all set! The ownership and permissions for your cPanel account have been successfully corrected.



    Congratulations! You’ve successfully corrected the ownership and permissions of your cPanel account.

    If you have any questions, please don’t hesitate to contact our team via live chat.

    For technical inquiries, please feel free to reach our support team by emailing support@bigscoots.com from your registered email or by submitting a support ticket.

Did this answer your question?