Users and Groups Creation and Deletion Batch

LinuxLinuxBeginner
Practice Now

Introduction

In this project, you will learn how to create and manage user accounts for teachers and students in a classroom server environment. You will use a bash script called userctr.sh to automate the process of adding and deleting user accounts, which can be a tedious task when done manually.

👀 Preview

## Add 1 teacher user and 6 student users (stu1 to stu6)
$ sh userctr.sh add teacher stu 6
teacher:617533
stu1:345893
stu2:183263
stu3:404525
stu4:571165
stu5:251940
stu6:716130

## Delete 1 teacher user and 6 student users (stu1 to stu6)
$ sh userctr.sh del teacher stu 6

🎯 Tasks

In this project, you will learn:

  • How to set up the necessary environment for the project
  • How to implement functions to add and delete users and groups
  • How to test the userctr.sh script to ensure it works as expected

🏆 Achievements

After completing this project, you will be able to:

  • Understand the importance of automating user management tasks
  • Create and manage user accounts for teachers and students using a bash script
  • Ensure that user accounts are created with the appropriate settings, such as default shell and sudo privileges
  • Efficiently add and delete user accounts in a classroom server environment

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicSystemCommandsGroup -.-> linux/exit("`Shell Exiting`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/BasicSystemCommandsGroup -.-> linux/test("`Condition Testing`") linux/UserandGroupManagementGroup -.-> linux/id("`User/Group ID Displaying`") linux/UserandGroupManagementGroup -.-> linux/useradd("`User Adding`") linux/UserandGroupManagementGroup -.-> linux/userdel("`User Removing`") linux/UserandGroupManagementGroup -.-> linux/usermod("`User Modifying`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/ControlFlowGroup -.-> shell/exit_status("`Exit and Return Status`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_ops("`Arithmetic Operations`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("`Arithmetic Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/AdvancedScriptingConceptsGroup -.-> shell/adv_redirection("`Advanced Redirection`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills shell/if_else -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} linux/exit -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} linux/echo -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} linux/pipeline -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} linux/redirect -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} linux/test -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} linux/id -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} linux/useradd -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} linux/userdel -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} linux/usermod -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} linux/sudo -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} shell/shebang -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} shell/comments -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} shell/quoting -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} shell/variables_decl -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} shell/variables_usage -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} shell/for_loops -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} shell/cond_expr -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} shell/exit_status -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} shell/arith_ops -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} shell/arith_expansion -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} shell/cmd_substitution -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} shell/subshells -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} shell/adv_redirection -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} shell/globbing_expansion -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} linux/wildcard -.-> lab-301459{{"`Users and Groups Creation and Deletion Batch`"}} end

Prepare the Environment

In this step, you will set up the necessary environment for the project.

  1. Open a terminal and navigate to the /home/labex/project directory.

  2. Create a new file called userctr.sh in the /home/labex/project directory using the following command:

    touch userctr.sh
  3. Open the userctr.sh file in a text editor and add the following shebang line at the beginning of the file:

    #!/bin/zsh

    This line specifies that the script should be executed using the zsh shell.

Implement the User and Group Management Functions

In this step, you will implement the functions to add and delete users and groups.

  1. Add the following code to the userctr.sh file:

    ## This script is used to manage user accounts for teachers and students.
    
    ## Check if the number of parameters is correct
    if [ $## -ne 4 ]; then
      echo "Parameter error"
      exit 1
    fi
    
    ## Check if the student count parameter is a positive integer and not exceeding 10
    if ! [[ "$4" =~ ^[1-9][0-9]?$|^10$ ]]; then
      echo "Parameter error"
      exit 1
    fi
    
    ## Check if the student name prefix contains only lowercase letters
    if ! [[ "$3" =~ ^[a-z]+$ ]]; then
      echo "Parameter error"
      exit 1
    fi
    
    ## Function to generate a random 6-digit password
    generate_password() {
      password=$(shuf -i 100000-999999 -n 1)
      echo "$password"
    }

    This code sets up the basic structure of the script, including parameter validation and a function to generate random passwords.

  2. Add the following code to the userctr.sh file to handle the "add" operation:

    ## Add users
    if [ "$1" == "add" ]; then
      ## Add teacher user
      if ! id -u "$2" >/dev/null 2>&1; then
        sudo useradd -m -s /bin/zsh "$2"
        sudo usermod -aG sudo "$2"
        password=$(generate_password)
        echo "$2:$password"
      else
        echo "$2:******"
      fi
    
      ## Add student users
      for ((i = 1; i <= $4; i++)); do
        username="$3$i"
        if ! id -u "$username" >/dev/null 2>&1; then
          sudo useradd -m -s /bin/zsh "$username"
          password=$(generate_password)
          echo "$username:$password"
        else
          echo "$username:******"
        fi
      done

    This code adds a teacher user and the specified number of student users. If a user already exists, it will display the user's password as six asterisks.

  3. Add the following code to the userctr.sh file to handle the "del" operation:

    ## Delete users
    elif [ "$1" == "del" ]; then
      ## Delete teacher user
      sudo userdel -rf "$2" >/dev/null 2>&1
      echo "$2" >/dev/null
    
      ## Delete student users
      for ((i = 1; i <= $4; i++)); do
        username="$3$i"
        sudo userdel -rf "$username" >/dev/null 2>&1
        echo "$username" >/dev/null
      done
    fi

    This code deletes the teacher user and the specified number of student users.

  4. Save the userctr.sh file

Test the Script

In this step, you will test the userctr.sh script to ensure it works as expected.

  1. Open a terminal and navigate to the /home/labex/project directory.

  2. Test the "add" operation by running the following command:

    sh userctr.sh add teacher stu 6

    This should create one teacher user and six student users, and display their randomly generated passwords.

    teacher:617533
    stu1:345893
    stu2:183263
    stu3:404525
    stu4:571165
    stu5:251940
    stu6:716130
  3. Test the "del" operation by running the following command:

    sh userctr.sh del teacher stu 6

    This should delete the teacher user and the six student users.

Congratulations! You have completed the project. You can now use the userctr.sh script to manage user accounts for teachers and students in your classroom servers.

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Linux Tutorials you may like