Users and Groups Creation and Deletion Batch

LinuxBeginner
Practice Now

Introduction

In this challenge, you will create a bash script named userctr.sh to automate the batch addition and deletion of users on a server. You will write a script that accepts command-line arguments to specify the operation (add or del), a teacher's username, a prefix for student usernames, and the number of students to manage. This task will enhance your skills in shell scripting, user management, and automation, which are crucial for system administration.

This is a Challenge, which differs from a Guided Lab in that you need to try to complete the challenge task independently, rather than following the steps of a lab to learn. Challenges are usually a bit difficult. If you find it difficult, you can discuss with Labby or check the solution. Historical data shows that this is a intermediate level challenge with a 67% pass rate. It has received a 93% positive review rate from learners.

Users and Groups Creation and Deletion Batch

A systems administrator needs a way to efficiently add a teacher and multiple student users to classroom servers. Manually creating each account is tedious and prone to errors. Your task is to create a bash script, userctr.sh, to automate this process. The script will take the operation type, teacher's name, student name prefix, and the number of students as parameters.

Tasks

  • Create a Bash script named userctr.sh in the /home/labex/project directory.
  • The script must implement two main operations: add and del, determined by the first command-line argument.
  • For the add operation, create a teacher user and a specified number of student users.
  • For the del operation, delete the specified teacher and student users.
  • The script must validate all input parameters according to the requirements below.

Requirements

  • Execution Format: The script must be executable with four arguments:

    sh userctr.sh <operation> <teacher_name> <student_prefix> <student_count>
  • Parameter Validation:

    • The script must receive exactly four arguments. If not, it should print parameter error and exit.
    • The student_count (fourth argument) must be an integer between 1 and 10, inclusive. Otherwise, print parameter error and exit.
    • The student_prefix (third argument) must consist of only lowercase letters. Otherwise, print parameter error and exit.
  • add Operation Details:

    • If a user to be added already exists, do not attempt to create them. Instead, print their username followed by ****** (e.g., stu3:******).
    • For new users, generate a random 6-digit numeric password.
    • Print the username and its password for each newly created user, separated by a colon (e.g., teacher:901231).
    • All created users must use /bin/zsh as their default shell.
    • The teacher user must be added to the sudo group to grant administrative privileges.
  • del Operation Details:

    • The script should delete the specified teacher and student users, including their home directories.
    • The del operation should not produce any output.
    • If a user to be deleted does not exist, the script should not report an error and should proceed with deleting the other existing users.

Example

The following examples demonstrate the expected behavior of your script.

  • Adding Users: This command adds one teacher (teacher) and six students (stu1 to stu6).

    sh userctr.sh add teacher stu 6
  • Example Output:

    teacher:901231
    stu1:271828
    stu2:928172
    stu3:******
    stu4:384712
    stu5:098273
    stu6:921098

    In this output, the asterisks for stu3 indicate that the user already existed, so a new password was not generated.

  • Deleting Users: This command deletes the same set of users. It should produce no output upon successful completion.

    sh userctr.sh del teacher stu 6

Hints

  • Use sudo before commands that require root privileges, like useradd and userdel.
  • Check the number of arguments with $#.
  • Access arguments using $1, $2, etc.
  • To check if a user exists, you can use id -u <username>. The command will have a non-zero exit code if the user does not exist.
  • Use useradd -m -s /bin/zsh <username> to create a user with a home directory and set the Zsh shell.
  • Use usermod -aG sudo <username> to add a user to the sudo group.
  • Use userdel -r <username> to delete a user and their home directory.
  • To generate a random 6-digit number, you can use shuf -i 100000-999999 -n 1.
  • A for loop is useful for iterating through the number of students: for ((i=1; i<=$student_count; i++)).
  • Use if [[ "$string" =~ "regex" ]] for pattern matching to validate parameters.
✨ Check Solution and Practice

Summary

Congratulations! You have successfully created a bash script to automate user management. This script can add and delete users in batches, validate input parameters, and handle existing users, which are essential skills for any system administrator. You have practiced using conditional statements, loops, command-line arguments, and external commands like useradd and userdel within a script.