DEV Community

Cover image for Managing User Accounts and Groups in Linux
horlatayorr
horlatayorr

Posted on • Updated on

Managing User Accounts and Groups in Linux

System AdministrationIn the realm of system administration, managing user accounts and groups is a fundamental task. This article delves into a Bash script designed to automate the creation of user accounts and their association with specific groups, a common requirement in dynamic environments such as software development teams. The script not only streamlines the setup process but also ensures security practices by generating random passwords for each user.

The Script at a Glance
Link to code repository
This Bash script reads a text file containing usernames and group names, creates the users and groups, sets up home directories with appropriate permissions, generates random passwords, logs all actions to a log file, and stores the generated passwords securely.
Step-By-Step Explanation

  1. Input Validation and Set Up

Image description
The script begins by checking if an input file is provided and sets up necessary directories and files.
Explanation:

-Checks if the input file is provided as an argument.
-Defines variables for the input file, log file, and password file.
-Creates necessary directories and files with appropriate permissions.

  1. Helper Functions Defines helper functions for logging messages and generating random passwords.

Image description
Explanation:
-log_message: Logs messages with a timestamp to the log file.
-generate_password: Generates a random password using urandom.
-Backs up the existing log and password files before making any changes.

  1. Processing the Input File Reads the input file line by line, extracting usernames and groups, and processes each user.

Image description

Image description

Explanation:

  • Reads the input file line by line, trimming whitespace.
  • Skips empty or malformed lines.
  • Checks if the user already exists and logs a message if they do.
  • Creates a personal group and the user if they do not already exist.
  • Sets appropriate permissions for the user’s home directory.
  • Password Generation Generates and sets a random password for the user, then stores it securely.

Image description

Explanation:

  • Generates a random password for the user.
  • Sets the generated password for the user.
  • Stores the username and password securely in the password file.
  1. Group Management Adds the user to specified groups, creating any groups that do not already exist.

Explanation:

  • Checks if groups are specified for the user.
  • Splits the groups by comma and processes each group.
  • Creates the group if it does not already exist.
  • Adds the user to the group and log s the action.

Testing the Script
To test the script, create a sample input file and execute the script:

  1. Create the Sample Input File:
    Image description

  2. Run the script.

Image description

  1. Check Outputs

Image description

  • Log File: Review /var/log/user_management.log for detailed logs.
  • Password File: Verify /var/secure/user_passwords.txt for generated passwords.
  • User Groups: Check the groups for each user using the id command:

Conclusion
By following this guide, you can effectively manage users and groups on your system, enhancing both efficiency and security.
For those interested in further enhancing their skills or exploring opportunities in software development and system administration, the HNG Internship offers a comprehensive platform to learn, grow, and connect with a vibrant tech community. Additionally, for hiring top tech talent or exploring premium tech solutions, consider visiting HNG Hire and HNG Premium.

Top comments (0)