How to Create Users in Linux OS Using a Text file

If you want to add, delete and verify the users on a bash script from a text file, then below is a guideline for your convenience. It also helps to add, delete and modify the users manually. You can also add, delete and authenticate the users to the Linux OS with the help of this script by getting the users’ information from any text file.

In this tutorial, both of these functions have been put in one script file. Administrator can specify the text file. Since this script is completely user friendly, you can learn it very easily.

Instructions

  • 1

    Starting the Script

    When you start writing the script, you will come across a given menu:

    ### MENU ###

    1. ADD USERS

    2. Varify Users

    3. Delete Users

    4. EXIT

  • 2

    Add Users

    By selection the option 1 “ADD USERS” from the given menu, it will allow the script to confirm that the user must be root, and since UID of root is zero (0), so first of all you need to compare the UID of the current user with the zero (0). In case it doesn’t match with the UID of root, then you will come across the following message:

    ****You must be the root user to run this script!****

    However, if it matches with the root’s UID then this message will be displayed on your screen:

    ***Identity Verified_You are the Root***

    In order to check the UID of the present user, type the given command in the terminal:

    echo $UID

    The ADD USERS functions will only be performed it the user is root.

    Now, a question will be asked from the user by the script that whether he/she wants to add the user manually or let the script automatically fetch the user’s information from the text file. For that, you will come across the following menu:

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

    "Please Select the Mode!!!

    1. Add the Users Manually

    2.Read the Users Automatically from Text File

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

    In case, you select the option 1, then the script will feature username, group and password; following commands will be used for username and group:

    read usr_nameread usr_group

    Now, with the help of above information, a user will be added by using the given command:

    useradd -g $usr_group -m $usr_name

    Following command will be used for password:

    passwd $usr_name

    In case the user selects the option 2, then it will allow the script to identify the text file and verifies that it is available or not. After that, it will examine the information such as username, password, and group from the text file. Now, the script will feature the present working directory where the text file should be located to the user, by using the given command.

    echo $(pwd)/users.txt

    Now, you will be asked by the script that whether the upper path of the text file is correct or not, or you want to write the file path manually. Following message will be displayed to the user:

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

    In case the user enters 1 for Yes, then the script will open the text file from the given path, by using the following command:

    Path=$($pwd)users.txt

    If the user goes for 2 for No, then the script will ask him/her to write correct path for the text file, and follow the path by using the given command:

    Read path

    The path of the text file is then stored in the variable with the name of “Path”. Given below is the command that will be used to authenticate the Existence of the file:

    if [ -e $Path ];

    It will take out the password from the text file with the help of this command:

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

    Now, all the variables having information about the users will be used by the script and insert them to the Linux OS, with the help of the given command:

    In order to add a group, following command will be used:

    groupadd $Group

    In order to add the users to the group, following command will be used:

    useradd -g $Group -m $Username

    In order to set the password for all the users, following command will be used:

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

  • 3

    Verify Users

    In the given menu:

    ### MENU ###

    1. ADD USERS

    2. Varify Users

    3. Delete Users

    4. EXIT

    If the user presses the option 2, it will feature this menu:

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

    Please Select the Mode!!!

    1.Varify All the Users of System

    2.Varify All the Users of TEXT file

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

    From the above menu, if the user goes for option 1, then the user can see all the users of the system. Use the following for this purpose:

    cat /etc/passwd |grep bash

    In case the user presses the option 2, then it will verify all the users mentioned in the text file. In order to verify users, use the following command in your script:

    Path=$($pwd)users.txt

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

    cat /etc/passwd | grep $varify

    In order to display the total number of users, use the following command:

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

  • 4

    Delete Users

    From the menu:

    ### MENU ###

    1. ADD USERS

    2. Varify Users

    3. Delete Users

    4. EXIT

    If the user goes for option 3, then the verification of the user as root will take place before proceeding anymore. If this process is successfully done then another menu will be displayed like:

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

    Please select the Mode!!!

    1.Delete Specific User

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

    From the above menu, if the user presses the option 1, then the script will feature the present users of the system, and user will be asked to write the name of the user to delete.

    In order to display all the users of the system, use the following command:

    cat /etc/passwd |grep bash

    The above command will give 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, it will be asked from the user to write the name of the user to delete, and when the name is written, it will find out the user in the above fie and delete him/her with the help of following command:

    read user_name
    userdel -r $user_name

    In case the user presses the option 2, then the script will delete all the users available in the given text file. Below is the given command that will extract the usernames from the text file:

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

    The given script segment will go at the end of the file and stop the loop:

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

    Below is the given command that will delete the users available in the text file:

    userdel -r $Username

Leave a Reply

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


9 × = twenty seven