Script to fix users home permissions?
Hello,
I don't have a permissions issue currently, but I was just wondering whether there is a script provided by cPanel that fixes up a users home directory in case it gets messed up for some reason?
As I understand, the primary domain and add-on domains wont work if the group isn't set to "nobody" (using Litespeed). And also things like "etc" in a users home directory needs to have the group set to "mail".
Is there a script that goes through and fixes up all owner/group permissions of all the various files and folders in a users home directory?
Thanks!
-
Hi, You can use this commands: cd /home/cpaneluser ls -all | grep public_html chown -R cpaneluser.cpaneluser public_html 0 -
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 -
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 -
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 -
I found this script: Simple Bash Script to Fix Account Permissions - The cPanel Admin Is this script acceptable? 0 -
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 -
I found this script: Simple Bash Script to Fix Account Permissions - The cPanel Admin Is this script acceptable?
Thanks, worked for me : )0 -
I'm glad to hear that worked well! 0 -
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 nullverbose=""
# Print the help texthelptext () {tput boldtput setaf 2echo "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 sgr0exit 0}
# Main workhorse, fix perms per account passed to itfixperms () {
# Get account from what is passed to the functionaccount=$1
# Make sure account isn't blankif [ -z "$account" ]; thentput boldtput setaf 1echo "Need a cPanel account!"tput sgr0helptextfi
# Check account against cPanel user filesif [ ! -f /var/cpanel/users/$account ]; thentput boldtput setaf 1echo "Invalid cPanel account: $account"tput sgr0return 1fi
# Get the account's homedirHOMEDIR=$(grep "^${account}:" /etc/passwd | cut -d: -f6)
if [ -z "$HOMEDIR" ]; thenecho "Could not find home directory for $account"return 1fi
tput boldtput setaf 4echo "(fixperms) for: $account"tput setaf 3echo "--------------------------"
# --- Home directory itself ---tput setaf 4echo "Fixing home directory ownership/permissions..."tput sgr0chown $verbose $account:$account $HOMEDIRchmod $verbose 711 $HOMEDIR
# --- public_html ---tput boldtput setaf 4echo "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_htmlfind $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 .htaccessfind $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 boldtput setaf 4echo "Fixing public_html itself..."tput sgr0chmod $verbose 750 $HOMEDIR/public_html
# --- .htpasswds ---if [ -d "$HOMEDIR/.htpasswds" ]; thentput setaf 4echo "Fixing .htpasswds..."tput sgr0chown -R $verbose $account:nobody $HOMEDIR/.htpasswdsfi
# --- Domains with docroots outside public_html ---tput setaf 3tput boldecho "--------------------------"tput setaf 4echo "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)doif [ -d "$SUBDOMAIN" ]; thentput boldtput setaf 4echo "Fixing docroot: $SUBDOMAIN"tput sgr0chown -R $verbose $account:$account $SUBDOMAINfind $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 755chmod $verbose 755 $SUBDOMAINfind $SUBDOMAIN -name .htaccess -exec chown $verbose $account:$account {} \;fidone
# --- Mail ---if [ -d "$HOMEDIR/mail" ]; thentput boldtput setaf 4echo "--------------------------"echo "Fixing mail..."tput sgr0chown -R $verbose $account:mail $HOMEDIR/mailfind $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" ]; thentput boldtput setaf 4echo "Fixing etc/ (mail auth files)..."tput sgr0chown -R $verbose $account:mail $HOMEDIR/etcfind $HOMEDIR/etc -name shadow -exec chmod $verbose 640 {} \;find $HOMEDIR/etc -name passwd -exec chmod $verbose 644 {} \;fi
# --- .ssh ---if [ -d "$HOMEDIR/.ssh" ]; thentput boldtput setaf 4echo "Fixing .ssh..."tput sgr0chown -R $verbose $account:$account $HOMEDIR/.sshchmod $verbose 700 $HOMEDIR/.sshfind $HOMEDIR/.ssh -type f -exec chmod $verbose 600 {} \;fi
# --- CageFS (CloudLinux) ---if [ -d "$HOMEDIR/.cagefs" ]; thentput boldtput setaf 4echo "Fixing CageFS..."tput sgr0chmod $verbose 775 $HOMEDIR/.cagefschmod $verbose 700 $HOMEDIR/.cagefs/tmp 2>/dev/nullchmod $verbose 700 $HOMEDIR/.cagefs/var 2>/dev/nullchmod $verbose 777 $HOMEDIR/.cagefs/cache 2>/dev/nullchmod $verbose 777 $HOMEDIR/.cagefs/run 2>/dev/nullfi
# Finishedtput boldtput setaf 3echo "Finished! (User: $account)"echo "--------------------------"printf "\n"tput sgr0
return 0}
# Parses all users via cPanel's users/domains fileall () {for user in $(cut -d: -f1 /etc/domainusers | sort -u)dofixperms "$user"done}
# Main function, switches options passed to itcase "$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 sgr0helptext;;esac;;-all) all ;;--account|-a) fixperms "$2" ;;*)tput bold; tput setaf 1; echo "Invalid option!"; tput sgr0helptext;;esacthank you
0 -
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 -
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.
Comments
12 comments