Simple Bash Script to Fix Account Permissions

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 your server, chmod 755, and pass the usernames as arguments:
./fixperms user1 user2 user3

You can also run a server-wide loop like this:
for i in `ls -A /var/cpanel/users` ; do ./fixperms $i ; done

Below is the script, but I recommend downloading it from here to ensure that the formatting is correct.

#!/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

2 comments:

  • sell this domain at sedo

    I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success.

  • What a wonderful blog. I invest several hours on the web reading through blogs, about tons of various subjects. I have to to begin with give kudos to whoever produced your theme and second of all to you for writing what i can only describe as an post. I honestly think there is a skill to writing articles that only a few posses and frankly you have it. The combination of informative and high quality content is definitely very rare with the big quantity of blogs on the internet.

  • Post a Comment