Skip to main content

Script to fix users home permissions?

Comments

12 comments

  • User Avatar
    RicardoFC
    Hi, You can use this commands: cd /home/cpaneluser ls -all | grep public_html chown -R cpaneluser.cpaneluser public_html
    0
  • User Avatar
    cPAusaf
    Hello, The main issue is knowing which PHP handler is being used and adjusting the permissions and ownership accordingly. cPanel does not provide any scripts that automatically fixes this unfortunately. There are a few places online which have permission/ownership scripts that usually work good when using the suPHP handler. If using DSO or CGI, that is where issues may occur if using one of the scripts incorrectly.
    0
  • User Avatar
    akust0m
    Hi, You can use this commands: cd /home/cpaneluser ls -all | grep public_html chown -R cpaneluser.cpaneluser public_html

    Unfortunately this wouldn't work for all situations I'm afraid, especially if you are using lsapi with Litespeed as public_html and addon document roots need to have 'nobody' as the group. I was more looking at an script to fix everything within a users home directory, not just public_html.
    Hello, The main issue is knowing which PHP handler is being used and adjusting the permissions and ownership accordingly. cPanel does not provide any scripts that automatically fixes this unfortunately. There are a few places online which have permission/ownership scripts that usually work good when using the suPHP handler. If using DSO or CGI, that is where issues may occur if using one of the scripts incorrectly.

    No worries at all. It would be amazing if cPanel developed a script that would fix permissions with auto handler recognition :) I'll look around, but I might end up trying to write my own bash script :)
    0
  • User Avatar
    Muhammed Fasal
    This is a simple bash script I wrote to fix the permissions and ownership of files within a cpanel account. To use, simply copy the script into your server, chmod 755, and pass the usernames as arguments: ./fixperms user1 user2
    You can see the script here, let me know if it helps.
    0
  • User Avatar
    NabiKAZ
    0
  • User Avatar
    NabiKAZ
    I found this script: Simple Bash Script to Fix Account Permissions - The cPanel Admin Is this script acceptable?
    0
  • User Avatar
    cPRex Jurassic Moderator
    The cPanel Admin is a great resource, but since that script is ten years old, it might be a good idea to test this on a test machine before trying it on a production server.
    0
  • User Avatar
    Futureen
    I found this script: Simple Bash Script to Fix Account Permissions - The cPanel Admin Is this script acceptable?

    Thanks, worked for me : )
    0
  • User Avatar
    cPRex Jurassic Moderator
    I'm glad to hear that worked well!
    0
  • User Avatar
    Dr Ahmed

    Hello,

    Iam sorry to elevate the post again,

    but is this script valid in our new cPanel 11.138 version

    #!/bin/bash
    #
    # fixperms - full cPanel account permission/ownership repair
    # Base script: PeachFlame/cPanel-fixperms (GPLv3)
    #
    # Extended to also cover: home directory perms, mail, .ssh, etc/shadow-passwd,
    # .htpasswds and CageFS - so it's a complete account fix, not just web files.
    # Updated By Dr.Ahmed Abdallah

    # Set verbose to null
    verbose=""

    # Print the help text
    helptext () {
        tput bold
        tput setaf 2
        echo "Fix Permissions (fixperms) Script help:"
        echo "Sets file/directory ownership and permissions for a full cPanel account"
        echo "USAGE: fixperms [options] -a account_name"
        echo "-------"
        echo "Options:"
        echo "-h or --help: Print this screen and exit"
        echo "--account or -a: Specify a cPanel account"
        echo "-all: Run on all cPanel accounts"
        echo "-v: Verbose output"
        tput sgr0
        exit 0
    }

    # Main workhorse, fix perms per account passed to it
    fixperms () {

        # Get account from what is passed to the function
        account=$1

        # Make sure account isn't blank
        if [ -z "$account" ]; then
            tput bold
            tput setaf 1
            echo "Need a cPanel account!"
            tput sgr0
            helptext
        fi

        # Check account against cPanel user files
        if [ ! -f /var/cpanel/users/$account ]; then
            tput bold
            tput setaf 1
            echo "Invalid cPanel account: $account"
            tput sgr0
            return 1
        fi

        # Get the account's homedir
        HOMEDIR=$(grep "^${account}:" /etc/passwd | cut -d: -f6)

        if [ -z "$HOMEDIR" ]; then
            echo "Could not find home directory for $account"
            return 1
        fi

        tput bold
        tput setaf 4
        echo "(fixperms) for: $account"
        tput setaf 3
        echo "--------------------------"

        # --- Home directory itself ---
        tput setaf 4
        echo "Fixing home directory ownership/permissions..."
        tput sgr0
        chown $verbose $account:$account $HOMEDIR
        chmod $verbose 711 $HOMEDIR

        # --- public_html ---
        tput bold
        tput setaf 4
        echo "Fixing website files in public_html..."
        tput sgr0

        # Fix ownership recursively first (this was missing in the base script -
        # it only chowned the top public_html dir, leaving files inside untouched)
        chown -R $verbose $account:nobody $HOMEDIR/public_html

        # Fix individual files and directories in public_html
        find $HOMEDIR/public_html -type d -exec chmod $verbose 755 {} \;
        find $HOMEDIR/public_html -type f ! -name "*.cgi" ! -name "*.pl" -exec chmod $verbose 644 {} \;
        find $HOMEDIR/public_html -name '*.cgi' -o -name '*.pl' | xargs -r chmod $verbose 755

        # Hidden files and .htaccess
        find $HOMEDIR/public_html -mindepth 1 -name ".*" -exec chown $verbose $account:$account {} \;
        find $HOMEDIR/public_html -name .htaccess -exec chown $verbose $account:$account {} \;
        find $HOMEDIR/public_html -name .htaccess -exec chmod $verbose 644 {} \;

        tput bold
        tput setaf 4
        echo "Fixing public_html itself..."
        tput sgr0
        chmod $verbose 750 $HOMEDIR/public_html

        # --- .htpasswds ---
        if [ -d "$HOMEDIR/.htpasswds" ]; then
            tput setaf 4
            echo "Fixing .htpasswds..."
            tput sgr0
            chown -R $verbose $account:nobody $HOMEDIR/.htpasswds
        fi

        # --- Domains with docroots outside public_html ---
        tput setaf 3
        tput bold
        echo "--------------------------"
        tput setaf 4
        echo "Fixing domains with docroots outside public_html..."
        tput sgr0

        for SUBDOMAIN in $(grep -ri "documentroot" /var/cpanel/userdata/$account/ | grep -v '\.cache\|_SSL' | awk '{print $2}' | grep -v public_html | sort -u)
        do
            if [ -d "$SUBDOMAIN" ]; then
                tput bold
                tput setaf 4
                echo "Fixing docroot: $SUBDOMAIN"
                tput sgr0
                chown -R $verbose $account:$account $SUBDOMAIN
                find $SUBDOMAIN -type d -exec chmod $verbose 755 {} \;
                find $SUBDOMAIN -type f ! -name "*.cgi" ! -name "*.pl" -exec chmod $verbose 644 {} \;
                find $SUBDOMAIN -name '*.cgi' -o -name '*.pl' | xargs -r chmod $verbose 755
                chmod $verbose 755 $SUBDOMAIN
                find $SUBDOMAIN -name .htaccess -exec chown $verbose $account:$account {} \;
            fi
        done

        # --- Mail ---
        if [ -d "$HOMEDIR/mail" ]; then
            tput bold
            tput setaf 4
            echo "--------------------------"
            echo "Fixing mail..."
            tput sgr0
            chown -R $verbose $account:mail $HOMEDIR/mail
            find $HOMEDIR/mail -type d -exec chmod $verbose 755 {} \;
            find $HOMEDIR/mail -type f -exec chmod $verbose 644 {} \;
        fi

        # --- etc (mail auth: shadow/passwd per domain) ---
        if [ -d "$HOMEDIR/etc" ]; then
            tput bold
            tput setaf 4
            echo "Fixing etc/ (mail auth files)..."
            tput sgr0
            chown -R $verbose $account:mail $HOMEDIR/etc
            find $HOMEDIR/etc -name shadow -exec chmod $verbose 640 {} \;
            find $HOMEDIR/etc -name passwd -exec chmod $verbose 644 {} \;
        fi

        # --- .ssh ---
        if [ -d "$HOMEDIR/.ssh" ]; then
            tput bold
            tput setaf 4
            echo "Fixing .ssh..."
            tput sgr0
            chown -R $verbose $account:$account $HOMEDIR/.ssh
            chmod $verbose 700 $HOMEDIR/.ssh
            find $HOMEDIR/.ssh -type f -exec chmod $verbose 600 {} \;
        fi

        # --- CageFS (CloudLinux) ---
        if [ -d "$HOMEDIR/.cagefs" ]; then
            tput bold
            tput setaf 4
            echo "Fixing CageFS..."
            tput sgr0
            chmod $verbose 775 $HOMEDIR/.cagefs
            chmod $verbose 700 $HOMEDIR/.cagefs/tmp 2>/dev/null
            chmod $verbose 700 $HOMEDIR/.cagefs/var 2>/dev/null
            chmod $verbose 777 $HOMEDIR/.cagefs/cache 2>/dev/null
            chmod $verbose 777 $HOMEDIR/.cagefs/run 2>/dev/null
        fi

        # Finished
        tput bold
        tput setaf 3
        echo "Finished! (User: $account)"
        echo "--------------------------"
        printf "\n"
        tput sgr0

        return 0
    }

    # Parses all users via cPanel's users/domains file
    all () {
        for user in $(cut -d: -f1 /etc/domainusers | sort -u)
        do
            fixperms "$user"
        done
    }

    # Main function, switches options passed to it
    case "$1" in
        -h|--help) helptext ;;
        -v)
            verbose="-v"
            case "$2" in
                -all) all ;;
                --account|-a) fixperms "$3" ;;
                *)
                    tput bold; tput setaf 1; echo "Invalid option!"; tput sgr0
                    helptext
                ;;
            esac
        ;;
        -all) all ;;
        --account|-a) fixperms "$2" ;;
        *)
            tput bold; tput setaf 1; echo "Invalid option!"; tput sgr0
            helptext
        ;;
    esac

     

    thank you

    0
  • User Avatar
    cPRex Jurassic Moderator

    Dr Ahmed - I reached out to the creator of that script and was informed it should still work, but it's important to note this isn't a tool developed or maintained by cPanel.

    0
  • User Avatar
    Dr Ahmed

    Hello cPRex ,

    I developped it to include mail directory in cPanel User directory

    But I want to ensure me that it is correct and compatible with the new versions of cPanel.

    Thank you for your help

    0

Please sign in to leave a comment.