Fixing cPanel File Permissions

Sometimes users mess with their own file permissions that they no longer work. They will probably then ring you and blame you in a loud, irritated voice. Here’s a simple shell script to fun that will fix all permissions in a users home directory on a cPanel server.

[codesyntax lang=”bash”]
#!/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

Copy, save as /bin/fixperms.sh and make it executable.

[codesyntax lang=”bash”]

chmod a+x /bin/fixperms.sh

Usage:

[codesyntax lang=”bash”]
fixperms.sh <username>

Author: Kieran Barnes

Kieran is a PHP developer with 15 years commercial experience. Specialist in WordPress, CakePHP, CubeCart and all things PHP.

4 thoughts on “Fixing cPanel File Permissions”

  1. Correction… You need to markup the parenths and the semicolon.

    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

Leave a Reply

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