jump to navigation

Script To Create New Users January 11, 2009

Posted by ModelR in UBUNTU LAMP Project.
trackback
THIS IS A SUBSECTION OF THE VERY VITAL UBUNTU LAMP SERVER PROJECT

You can find the full outline of this project here:
LINK: Very Vital Ubuntu LAMP Server Project Outline

Please support this project.
Make A Donation

Download a copy of the script to look over: newuser.sh in text format

Create a new file called “/usr/local/bin/newuser.sh:
$ sudo nano /usr/local/bin/newuser.sh

Edit the file with the following:
#!/bin/bash
# Script to add a user to Linux system
echo ""
echo "******************************"
echo "*Add a new user to the system*"
echo "******************************"
echo ""
if [ $(id -u) -eq 0 ]; then
read -p "Enter username : " username
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username already exists!"
exit 1
else
#CREATE THE USER
sudo useradd -d /home/$username -m $username
echo ""

#SET THE UNIX PASSWORD FOR THE USER
echo "Create a SYSTEM password for $username"
let done=0
while [ $done -eq 0 ]; do
passwd $username
echo ""
egrep "^$username\:!:" /etc/shadow >/dev/null
if [ $? -eq 0 ]; then
echo " Could not create a SYSTEM password for $username."
echo " Please try again."
echo ""
else
let done=1
echo " SYSTEM password for $username was successfully created."
echo ""
fi
done

#CREATE THE public_html DIRECTORY AND CHANGE OWNERSHIP TO USER
mkdir /home/$username/public_html
chown $username:$username /home/$username/public_html

#ADD THE APACHE USER ACCOUNT TO THE USER'S GROUP
sudo usermod -a -G $username www-data

#SET THE PASSWORD FOR MYSQL AND SAMBA
echo "Create a MySQL and Samba password"
let done=0
while [ $done -eq 0 ]; do
read -s -p "MySQL & Samba Password: " PASS1
echo ""
read -s -p "Confirm the MySQL & Samba password: " PASS2
echo ""
echo ""
if [ "$PASS1" == "$PASS2" ]; then
let done=1
#CREATE THE MYSQL AND SAMBA PASSWORD
echo "Creating the Samba User and Password"
smbpasswd -e $username
(echo $PASS1; echo $PASS1) | smbpasswd -s -a $username
echo "Please enter the MySQL root password to complete the user setup"

echo ""

echo " MySQL & Samba password successfully created for $username."
echo ""
else
echo "The passwords do not match, please try again."
echo ""
fi
done

#FINISH
echo " $username was successfully created."
echo ""

echo ""
echo "***********************"
echo "RESTARTING APACHE"
sudo /etc/init.d/apache2 restart
echo "***********************"
echo ""

echo ""
echo "***********************"
echo "*Your home directory is /home/$username"
echo "***********************"
echo ""
echo "**********************"
echo "*Your Web directory is /home/$username/public_html"
echo "**********************"
echo ""
echo "******************************"
echo "*The Samba share directory is" '\\'$(hostname)'\'"homes"'\'
echo "******************************"
echo ""
fi
else
echo "Only root may add a user to the system"
exit 2
fi

Make the script executable
$ sudo chmod 755 /usr/local/bin/newuser.sh

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: