Services built by a real sysadmin.

Every product in this shop was tested in production, optimized under pressure, and designed to make your infrastructure safer, faster, and cleaner.

Browse services

Fix cPanel ownership and permissions [Script]

When a cPanel server experiences file permission issues-after a migration, manual file operations, or a misbehaving script-websites may become inaccessible, emails may fail, or security might be at risk. This script automates the process of fixing file ownership and permissions for one or more cPanel users, ensuring everything is back to a secure and functional state.

Use Case

You may need to run this script when:

  • Website files show 403 Forbidden errors
  • Email delivery fails due to etc/ permissions
  • Files were copied or restored without --preserve flags
  • CageFS directories have incorrect modes

How to run the script

for i in `ls -A /var/cpanel/users` ; do ./fixperms $i ; done

The Script (save as ./fixperms and chmod +x fixperms)

#!/bin/bash
# Script to fix permissions and ownerships for one or more cPanel users

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

USERS=$@

for user in $USERS; do
  HOMEDIR=$(getent passwd "$user" | cut -d: -f6)

  if [ ! -f /var/cpanel/users/"$user" ]; then
    echo "User file missing for $user, skipping"
    continue
  elif [ -z "$HOMEDIR" ]; then
    echo "Could not determine home directory for $user, skipping"
    continue
  fi

  echo "Fixing ownership and permissions for $user"

  # Ownership
  chown -R "$user:$user" "$HOMEDIR" >/dev/null 2>&1
  chmod 711 "$HOMEDIR" >/dev/null 2>&1
  chown "$user:nobody" "$HOMEDIR/public_html" "$HOMEDIR/.htpasswds" 2>/dev/null
  chown "$user:mail" "$HOMEDIR/etc" "$HOMEDIR/etc/"*/shadow "$HOMEDIR/etc/"*/passwd 2>/dev/null

  # File permissions (parallel)
  find "$HOMEDIR" -type f -print0 2>/dev/null | xargs -0 -P4 chmod 644 2>/dev/null
  find "$HOMEDIR" -type d ! -name cgi-bin -print0 2>/dev/null | xargs -0 -P4 chmod 755 2>/dev/null
  find "$HOMEDIR" -type d -name cgi-bin -print0 2>/dev/null | xargs -0 -P4 chmod 755 2>/dev/null

  chmod 750 "$HOMEDIR/public_html" 2>/dev/null

  # CageFS fixes
  if [ -d "$HOMEDIR/.cagefs" ]; then
    chmod 775 "$HOMEDIR/.cagefs" 2>/dev/null
    chmod 700 "$HOMEDIR/.cagefs/tmp" "$HOMEDIR/.cagefs/var" 2>/dev/null
    chmod 777 "$HOMEDIR/.cagefs/cache" "$HOMEDIR/.cagefs/run" 2>/dev/null
  fi

done

This is a improved script from: https://www.casbay.com/guide/kb/script-to-fix-cpanel-account-permissions-2

Need Expert Help?

If you’re still having issues with your server or network setup, let’s fix it together. Schedule a one-on-one consultation now.

Schedule a Consultation

Comments

Leave a Reply

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