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

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.

âœĻ Check Solution and Practice

Summary

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

Other Linux Tutorials you may like