DEV Community

Uyi Oboite
Uyi Oboite

Posted on

Managing Users in PraisePHS Microsystems Ltd with a Bash Script

In any corporate environment, managing users efficiently and securely is paramount. PraisePHS Microsystems Ltd. employs a systematic approach to user management using a Bash script. This article will explain how the provided Bash script functions, ensuring smooth and secure user creation and management.

Overview
The script takes a file containing user and group information as input, creates users, assigns them to specified groups, sets passwords, and logs the process. The users.txt file, for example, provides the necessary user and group details in a semicolon-separated format.

Bash Script Breakdown
Here's a step-by-step explanation of the script:

  1. The script starts by defining the necessary files: the input file containing user information, the log file for logging actions, and the password file for storing generated passwords.
    Image description

  2. This function generates a random password using OpenSSL's rand function, ensuring a strong and unique password for each user.
    Image description

  3. This section ensures the existence and correct permissions of the log and password files. If these files or directories do not exist, the script creates them and sets appropriate permissions to ensure security

Image description

  1. The script reads each line of the input file, removes any carriage returns, trims whitespace, and skips empty lines to ensure clean data processing.

Image description

  • User Existence Check: The script checks if a user already exists. If they do, it logs this information.
  • User Creation: If the user does not exist, the script generates a password, creates the user with a home directory, sets the password, and logs these actions.
  • Group Management: The script adds the user to specified groups. If a group does not exist, it creates the group before adding the user.
  • Permissions and Ownership: Finally, the script sets the permissions and ownership for the user's home directory to ensure security.

Image description

Example Input File: users.txt
The users.txt file contains user information in the format username; group1,group2

Image description

For more information about the HNG internship, visit the HNG Internship page

If you are looking to hire talented interns, check out the HNG Hire page

Top comments (0)