EASY SCRIPT: Change main username on an account

brandonk

Well-Known Member
Aug 13, 2001
66
0
306
I just finished transferring some accounts over to a new server. Unfortunately many of the accounts that I transferred had over 8 characters in their username (they were transferred from Ensim) so a ton of them failed.

I compiled this little script to change the main username of a domain, as most of you know this feature is not available in WHM. The script changes the username and ALSO goes into every single file of theirs and changes the old path (/home/old user) to the new (/home/newuser) which should make the change unnoticeable to your clients (especially if they have cgi). It worked flawlessly for me.

USE AT YOUR OWN RISK!

# old username
$username1 =

# new username
$username2 =


# edit /etc/passwd
perl -pi -e "s/$username1/$username2/g" /etc/passwd

# edit /etc/shadow
perl -pi -e "s/$username1/$username2/g" /etc/shadow

# edit /etc/group
perl -pi -e "s/$username1/$username2/g" /etc/group

# chaning cpanel file
mv /var/cpanel/users/$username1 /var/cpanel/users/$username2

# editing httpd.conf
perl -pi -e "s/User $username1/User $username2/g" /usr/local/apache/conf/httpd.conf
perl -pi -e "s/Group $username1/Group $username2/g" /usr/local/apache/conf/httpd.conf
perl -pi -e "s&/home/$username1/&/home/$username2/&g" /usr/local/apache/conf/httpd.conf

# moving main directory
mv /home/$username1 /home/$username2

# replacing every instance of /home/username in every file in the new main directory
# this will fix most cgi problems with changing directory paths
# if you don't need this comment it out as it takes awhile
find /home/$username1 -type f -exec perl -pi -e "s/\/home\/$username1/\/home\/$username2/g" {} \;

# setting permissions
chown -R $username2 /home/$username2
chgrp -R $username2 /home/$username2

# running necessary cpanel updates
/scripts/updateuserdomains
/scripts/mailperm
/scripts/ftpupdate