How To Add Users To Linux OS From A Text file

Channel: Linux
Abstract: echo "****You must be the root user to run this scriptthen clear main fi The following command will delete the users listed in the text file
How To Add Users To Linux OS From A Text file

This tutorial is about a bash script to add, delete and verify the users either from a text file; it can also add, delete and modify the users manually. This script can be used to add , delete and verify the users to the Linux OS by fetching the user's information from any text file and this script can also add, delete or verify the users manually, I have put both of these functions in one script file. The text file can be specified by the administrator.

:-) !!!The script is totally user friendly!!! :-)

You can find the script at the end of this tutorial.

 

Starting the Script

When you start the script a menu will appear like this:

### MENU ###

1. ADD USERS

2. Varify Users

3. Delete Users

4. EXIT

 

1. Add Users

Now select the option 1. ADD USERS from the menu by pressing "1" from the Keyboard.

Now the Script confirms that the user must be root, and we know that the UID of root is zero( 0 ). so first I compare the UID of the current user with the zero (0), if the UID doesn't match with the UID of root then it will display the following message:

****You must be the root user to run this script!****
and if the UID matches with root's UID then it displays the following message and runs the script:
***Identity Verified_You are the Root***

We can check the UID of the current user by typing the following command in the terminal:

echo $UID

If the user is root then the 1.ADD USERS function will be performed.

Now the script will ask the user either if he/she wants to add the users manually or let the script get the user's information from the text file. For that it will display the following menu:

#########################################

"Please Select the Mode!!!

1. Add the Users Manually

2.Read the Users Automatically  from Text File

###########################################

Now if the user selects 1. Add the Users Manually then the script will prompt for user the username, group and password; it will use the following commands for username and group:

read usr_nameread usr_group

Now by using above information it will add the user by using the following command:

useradd -g $usr_group -m $usr_name

For the password it will use the following command:

passwd $usr_name

Now if the user selects the option 2.Read the Users Automatically from Text File then the script locate the text file and verifies that it exists, then it will read the information like username, group, password from the text file.

Now the script will display the current working directory where the text file should be located to the user, by using the following command:

echo $(pwd)/users.txt

Now the script asks if the above path of the text file is correct or if you want to enter the file path manualy, it will display the following message to the user:

Do you want to use the above Default PATH? Yes=1 & No=2

If the user presses 1 for Yes then the script will load the text file from the shown path, by using the following command:

Path=$($pwd)users.txt

If the user presses 2 for No, then the script will ask the user to enter the correct path for the text file, and read the file path by using the following command:

read Path

The path of the text file is stored in the variable "Path". The Following command will be used to verify the Existence of the file:

if [ -e $Path ];

It will extract the username from the text file by using the following command:

Username=`grep "Username00$num" $Path | cut -f2 -d:`

It will extract the group from the text file by using the following command:

Group=`grep "Group" $Path |cut -f2 -d:`

It will extract the password from the text file by using the following command:

Password=`grep "Password" $Path | cut -f2 -d:`

Now the script uses the variables which contain the information about the users and add them to the Linux OS, by using the following commands:

For adding a group it will use the following command:

groupadd $Group

For adding the users to the group it will use the following command:

useradd -g $Group -m $Username

For setting the password for all the users it uses the following command:

echo $Password | /usr/bin/passwd --stdin $Username

Password is the variable which contains the same password for all the users, it's extracted from the text file.

 

2. Verify Users

From the menu:

	### MENU ###

1. ADD USERS
2. Varify Users
3. Delete Users
4. EXIT

If user select the option 2. Varify Users the another menu will be appear like:

#################################

Please Select the Mode!!!

1.Varify All the Users of System

2.Varify All the Users of TEXT file

#################################

From the above menu if user select the option 1.Varify All the Users of System, then all the users of the system will be displayed to the user, I have used the following command for this purpose:

cat /etc/passwd |grep bash

The result of the command in my case will return the following bash users:

root:x:0:0:root:/root:/bin/bash
lucky:x:501:501:Lucky:/home/lucky:/bin/bash

Now if the user selects the option 2.Varify All the Users of TEXT file, then all the users that are mentioned in the text file will be verified. In my case I have used the following command to verify the users of the text file either that are added to the Linux OS or not, and how many users I have.

To verify the users I have used the following command in my script:

Path=$($pwd)users.txt
varify=`grep "varify" $Path |cut -f2 -d:`
cat /etc/passwd | grep $varify

Varify is the variable that contains the unique value of the usenames.

To display the total number of users I have used the following command:

echo -e "\nYou have Currently"
cat /etc/passwd | grep $varify |wc -l;
echo "users added from your Text File"

The result of the above command in my case is:

fa05btn001:x:502:502::/home/fa05btn001:/bin/bash
fa05btn002:x:503:502::/home/fa05btn002:/bin/bash
fa05btn003:x:504:502::/home/fa05btn003:/bin/bash
fa05btn004:x:505:502::/home/fa05btn004:/bin/bash
fa05btn005:x:506:502::/home/fa05btn005:/bin/bash
fa05btn006:x:507:502::/home/fa05btn006:/bin/bash
fa05btn007:x:508:502::/home/fa05btn007:/bin/bash
fa05btn008:x:509:502::/home/fa05btn008:/bin/bash

You have Currently 8 users added from your Text file

 

3. Delete Users From the menu:

### MENU ###

1. ADD USERS

2. Varify Users

3. Delete Users

4. EXIT

If user selects the option 3. Delete Users then the verification of the user as root will be done before going further. If the user verified as root succesfully then another menu will appear like:

##############################################

Please select the Mode!!!

1.Delete Specific User

2.Delete all Users Specified in the TEXT File
##############################################

Now from the above menu if user selects the option 1.Delete Specific User then the script will display the current users of the system and ask the user to enter the name of the user to delete.

To display all the users of the system I have used the following command:

cat  /etc/passwd |grep bash 

The above command will display the following result:

You have currently following USERS Added to your System
root:x:0:0:root:/root:/bin/bash
lucky:x:501:501:Lucky:/home/lucky:/bin/bash
fa05btn001:x:502:502::/home/fa05btn001:/bin/bash
fa05btn002:x:503:502::/home/fa05btn002:/bin/bash
fa05btn003:x:504:502::/home/fa05btn003:/bin/bash
fa05btn004:x:505:502::/home/fa05btn004:/bin/bash
fa05btn005:x:506:502::/home/fa05btn005:/bin/bash
fa05btn006:x:507:502::/home/fa05btn006:/bin/bash
fa05btn007:x:508:502::/home/fa05btn007:/bin/bash
fa05btn008:x:509:502::/home/fa05btn008:/bin/bash

Type the Name of the User you wants to Delete :

Now the user will asked to enter the name of the user to delete, and when the user entered the name of the user that he/she wants to delete the Linux OS will search the user in the above file and delete the user by using the following command:

read user_name
userdel -r $user_name

Now if the user selects the option 2.Delete all Users Specified in the TEXT File then the script will delete all the users listed in that text file.

The following command will extract the usernames from the text file:

Username=`grep "Username00$num" $Path | cut -f2 -d:`

The following script segment will go to the end of the file and terminates the loop:

if [ $Username == "EOF" ]; then
clear
main
fi

The following command will delete the users listed in the text file:

userdel -r $Username

Note: I have tested my ADDUSER.sh script on PCLinuxOS, and I hope that it will also work well on other Linux distributions. Anybody can modify the code as well, and please share it with others.

Have a nice time. :-)

 

THE FOLLOWING IS THE COMPLETE SCRIPT TO ADD, DELETE and VERIFY THE USERS IN LINUX OS. Created by USMAN AKRAM (ajaonchat)
#!/bin/bash
###############################################################
#This Script is Created by                                    #
#                USMAN AKRAM  (ajaonchat)                     #
#                                       FA05-BTN-005          #
#                            BTN-6                            #
###############################################################
add_users()
{
	ROOT_UID=0      #The root user has a UID of 0
	if      [ "$UID" -ne "$ROOT_UID" ]; then
        	echo "****You must be the root user to run this script!****"
        	exit
	fi
	echo
	echo Identity Verified_You are the Root
	echo 

	echo -e "\n#########################################\n"
	echo -e "Please Select the Mode!!!\n"
	echo -e "1. Add the Users Manually\n
2.Read the Users Automatically  from Text File\n"
	echo -e "###########################################"
	read add_opt
	case $add_opt in
		1)
		echo -e "Please Enter the User name:"
		read usr_name
		echo -e "Please enter the User Group"
		read usr_group
		groupadd $usr_group
       	useradd -g $usr_group -m $usr_name
    	        echo -e "Please enter the Password for User $usr_name"
 	passwd $usr_name ;;
	2)
       echo
       echo "Present working directory is: `pwd`/users.txt"
       echo
       echo -e "Do you want to use the above Default PATH? Yes=1 & No=2"
       read yn
if [ $yn == 1 ]; then
                      Path=$($pwd)users.txt
else
       echo -n "Please enter the correct path to the file (e.g. /root/folder/filename.txt): "
       read Path
fi
if [ -e $Path ]; then           #If the file user specified exists
Username=lucky
num=1
	while  [ $Username != "EOF" ]
		do
		Username=`grep "Username00$num" $Path | cut -f2 -d:`   #Extract Username from text file
num=$(($num+1))
		Password=`grep "Password" $Path | cut -f2 -d:`       #Extract Password from text file
		Group=`grep "Group" $Path |cut -f2 -d:`              #Extract Group From text file 
		
		groupadd $Group		
		
		               #Adds user to the system and gives them a password
           if [ $Username == "EOF" ]; then
                   clear
                   main
           fi
                #Adds user to the system
                useradd -g $Group -m $Username 
                #Add users password
                echo $Password | /usr/bin/passwd --stdin $Username #user Password will be assigned
	done
else  #If the user Specified file doesn't Exists
	echo -e "\n#############################################"
	echo -e "\n######CANNOT FIND or LOCATE THE FILE!!!!#####"
	echo -e "\n#############################################"
fi;;
*) echo -e "You have selected the Wrong Choice!!!"
esac
        
}
varify()
{
	echo -e "#################################"
	echo -e "Please Select the Mode!!!\n"
	echo -e "1.Varify All the Users of System\n
2.Varify All the Users of TEXT file\n"
	echo -e "#################################"
	read varify_user
case $varify_user in
		1) cat /etc/passwd |grep bash;;
		2)
 	echo
        echo "Present working directory is: `pwd`/users.txt"
        echo
        echo -e "Do you want to use the above Default PATH? Yes=1 & No=2"
        read yn
if [ $yn == 1 ]; then
                     Path=$($pwd)users.txt
else
        echo -n "Please enter the correct path to the file (e.g. /root/folder/filename.txt): "
        read Path
fi
if [ -e $Path ]; then
	Path=$($pwd)users.txt
	varify=`grep "varify" $Path |cut -f2 -d:`
	  cat  /etc/passwd | grep $varify
  echo -e "\nYou have Currently "
          cat /etc/passwd | grep $varify |wc -l
	  echo  "users added from your Text file" 
else  #If the user Specified file doesn't Exists
      echo -e "\n#############################################"
      echo -e "\n######CANNOT FIND or LOCATE THE FILE!!!!#####"
      echo -e "\n#############################################"
fi ;;
*) echo -e "Wrong Choice"
esac
					 
}

del_users()
{
#This Script will delete the Users from the HOME DIRECTORY!!!!
ROOT_UID=0      #The root user has a UID of 0
	if      [ "$UID" -ne "$ROOT_UID" ]; then
        	echo "****You must be the root user to run this script!****"
        	exit
	fi
	echo
	echo Identity Verified_You are the Root
	echo
	
	echo
	echo "Present working directory is: `pwd`/students.txt"
	echo
	
#This is the Menu to select the mode to Deletion the users, either delete selected user or to delete all the users you have in the TEXT file...???
echo -e "####################################"
echo -e "\nPlease select the Mode!!!\n
1.Delete Specific User\n
2.Delete all Users Specified in the TEXT File\n"
echo -e "####################################"
read del_opt
case $del_opt in
	1)
		echo -e "\n\nYou have currently following USERS Added to your System\n"
		 cat  /etc/passwd |grep bash
		echo -e "\n\n Type the Name of the User you wants to Delete :"
		read user_name
		userdel -r $user_name ;;
	2) 
       echo
       echo "Present working directory is: `pwd`/users.txt"
       echo
       echo -e "Do you want to use the above Default PATH? Yes=1 & No=2"
       read yn
if [ $yn == 1 ]; then
       Path=$($pwd)users.txt
else
       echo -n "Please enter the correct path to the file (e.g. /root/folder/filename.txt): "
       read Path
fi
if [ -e $Path ]; then 		#If the file user specified exists
		num=1
Username=lucky		
		while  [ $Username != "EOF" ]
		do
		Username=`grep "Username00$num" $Path | cut -f2 -d:`   #Extract Username from text file
	
		if [ $Username == "EOF" ]; then
	    		clear
			main
		fi
				                       
                userdel -r $Username
num=$(($num+1))
	done
else  #If the user Specified file doesn't Exists
	echo -e "\n#############################################"
	echo -e "\n######CANNOT FIND or LOCATE THE FILE!!!!#####"
	echo -e "\n#############################################"
fi ;;
*) echo -e "Wrong Choice" 
esac
}

main()
{
	opt=1
while [ $opt -le 4 ]
do
	clear
echo -e "			### MENU ###\n 
			1. ADD USERS\n
			2. Varify Users\n
			3. Delete Users\n
			4. EXIT\n"
read opt
case $opt in
	1) add_users ;;
	2) varify ;;
	3) del_users ;;
	4) exit 0 ;;
	*) echo -e "You have Entered the Wrong Choice!!!"
esac
echo -e "\nWant to run Script Again Yes=1 & No=4."
read opt
done
}
main
exit 0

 

This is the text file that I used with my script
#######################################################
CREATED By
                     USMAN AKRAM (LUCKY)
				FA05-BTN-005
					[email protected]
#######################################################
COMSATS Abbottabad Student's Account
	==============================
Filename:users.txt
Year:2008
Campus:CIIT-ABTD
Group:btn
##Following are the users that will be added to the HOME Directory!!!
Username001:fa05btn001
Username002:fa05btn002
Username003:fa05btn003
Username004:fa05btn004
Username005:fa05btn005
Username006:fa05btn006
Username007:fa05btn007
Username008:fa05btn008
Username009:EOF
#it will be the End user
varify:fa05btn
Password:123456
End of Text File!!!!

Created by USMAN AKRAM(Lucky), Student of COMSATS University Abbottabad [BS(TN)-6] Email Address: [email protected]

Ref From: howtoforge
Channels: pclinuxoslinux

Related articles