Technical Article: Explaining the create_users.sh Script

Dru - Jul 2 - - Dev Community

In this article, we will walk you through the script for submission of the Stage 1, explaining each step and the reasoning behind it.

Managing users in a growing organization can be a daunting task, especially when it involves setting up accounts, assigning groups, creating home directories, and ensuring secure password handling. To streamline this process, we developed a bash script called create_users.sh.

Script Overview

The create_users.sh script reads a text file containing employee usernames and group names, creates users and groups, sets up home directories with appropriate permissions, generates random passwords, and logs all actions.

Key Features

  1. Reading Input File: The script takes a single argument – the name of the text file containing user information. Each line in the file is formatted as user;groups, where groups are optional and separated by commas.
  2. Logging Actions: All actions performed by the script are logged to /var/log/user_management.log to ensure transparency and ease of debugging.
  3. Secure Password Handling: Random passwords are generated for each user and stored securely in /var/secure/user_passwords.txt, with permissions set so that only the file owner can read it.

Detailed Breakdown

  1. Check Input File: The script first checks if the input file is provided as an argument. If not, it displays usage instructions and exits.

    if [ $# -ne 1 ]; then
        echo "Usage: $0 <name-of-text-file>"
        exit 1
    fi
    
  2. Initialize Log and Password Files: The script ensures that the log directory and files exist. It also sets appropriate permissions for the password file.

    mkdir -p /var/log
    touch $LOG_FILE
    mkdir -p /var/secure
    touch $PASSWORD_FILE
    chmod 600 $PASSWORD_FILE
    
  3. Log Function: A helper function to log actions with timestamps.

    log_action() {
        echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> $LOG_FILE
    }
    
  4. Generate Password Function: This function generates a random 12-character password using /dev/urandom.

    generate_password() {
        tr -dc A-Za-z0-9 </dev/urandom | head -c 12 ; echo ''
    }
    
  5. Processing Users: The script reads the input file line by line, processes each user, and performs the following actions:

    • Checks if the user already exists.
    • Creates a new user with a home directory.
    • Creates a personal group for the user.
    • Assigns the user to additional groups.
    • Sets permissions for the home directory.
    • Generates and sets a random password.
    • Logs the actions performed.
    while IFS=";" read -r username groups; do
        # Processing steps here
    done < "$INPUT_FILE"
    

By automating the user management process, create_users.sh ensures consistency, security, and efficiency. This script can significantly reduce the administrative burden on system administrators and help maintain a secure and well-organized user environment.

For more information on how to streamline your technical processes and to learn about opportunities to work with talented developers, visit the HNG Internship website and explore premium services.

.