nav-left cat-right
cat-right

Bash script to generate cPanel user files

Don’t have your domains listed in WHM/cPanel. The cPanel takes information from cPanel user files under /var/cpanel/users/. The accounts won’t be listed in WHM if corresponding files are missing. This script will help you to generate cPanel user files.

This script may fail if you have cPanel user files existing under /var/cpanel/users/. So I would request you to backup users directory and then remove all user files.

mv /var/cpanel/users /var/cpanel/users.bkp
mkdir /var/cpanel/users

You must run /scripts/updateuserdomains after generating cPanel user files to rebuild the system caches.

#!/bin/bash

#################################################################################################
#                                                                                               #
# Bash script to generate cPanel user files.                                                    #
#                                                                                               #
# Don't have your domains listed in WHM/cPanel. The cPanel takes information from               #
# cPanel user files under /var/cpanel/users/. The accounts won't be listed in WHM               #
# if corresponding files are missing. This script will help you to generate cPanel user files.  #
#                                                                                               #
# You must run /scripts/updateuserdomains afterwards to rebuild the system caches.              #
#                                                                                               #
# Written By: Mohammed Abdurahiman                                    #
# Date: Feb-5-2010                                                                              #
#                                                                                               #
#################################################################################################

# create a temporary directory and copy required files to it.

mkdir /root/tmpdir
cd /root/tmpdir/
cp /etc/userdomains /root/tmpdir/

# Edit the line below to correct username
cp /var/cpanel/users/user1 /root/tmpdir/template

if [ ! -f /root/tmpdir/template ]; then

cat << EOL

/root/tmpdir/template file missing.
/root/tmpdir/template is a cPanel user file for reference.
If you have any file exists under /var/cpanel/users/, edit line 21 and run this script again.
If you don't have, you can create a test domain through cPanel which will generate one.
I will be posting an example file in my blog .

EOL
fi

# remove the extra space before usernames
sed -i '
s/ //' /root/tmpdir/userdomains

# remove last entry from userdomains *:nobody
sed -n -i $d /root/tmpdir/userdomains

# number of domains
numb=`wc -l /root/tmpdir/userdomains`

# select domain:user pair from /root/tmpdir/userdomains and then extract them.
for i in `seq 1 $numb`; do
dom_usr=`sed -n ${i}p /root/tmpdir/userdomains`
dom=`echo $dom_usr |awk -F: '
{print $1}'`
usr=`echo $dom_usr |awk -F: '
{print $2}'`
cp /root/tmpdir/template /var/cpanel/users/$usr

# change DNS entry
sed -i '
/DNS/c\
DNS='$dom'
' /var/cpanel/users/$usr

# change IP entry
ip=`dig +short $dom`
sed -i '
/IP/c\
IP='$ip'
' /var/cpanel/users/$usr

# change USER entry
sed -i '
/USER/c\
USER='$usr'
' /var/cpanel/users/$usr

# change MXCHECK entry
sed -i '
/MXCHECK/c\
MXCHECK-'$dom'=0
' /var/cpanel/users/$usr

# change group to username
chgrp $usr /var/cpanel/users/$usr

echo Created cPanel user file for $usr

done
echo
echo Run /scripts/updateuserdomains to rebuild the system caches.
echo

cPanel user file template :

BWLIMIT=unlimited
CONTACTEMAIL=contact@yourdomain.com
CONTACTEMAIL2=
DEMO=0
DNS=domainname.com
FEATURELIST=default
HASCGI=1
IP=192.168.1.100
LOCALE=en
MAXADDON=0
MAXFTP=unlimited
MAXLST=unlimited
MAXPARK=0
MAXPOP=unlimited
MAXSQL=unlimited
MAXSUB=unlimited
MTIME=1265375424
MXCHECK-domainname.com=0
OWNER=root
PLAN=default
RS=x3
STARTDATE=1265375423
USER=username

~mohammed

One Response to “Bash script to generate cPanel user files”

  1. [...] Today found this great post, here is a quick excerpt : Don’t have your domains listed in WHM/cPanel. The cPanel takes information from cPanel user files under /var/cpanel/users/. The accounts won’t be listed in WHM if corresponding files are missing. This. Read the rest of this great post Here [...]

Leave a Reply