Managing User Accounts and Groups in Linux
Published: 2024-07-02 01:41:10

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](https://github.com/horlatayorr/HNG-Internship_Stage1.git): 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuu2opx93z72ecytdcda.PNG)
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.

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6dqwg4yz5ayeazw1qwb.PNG)
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.

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ssnq5tzwh037h9b1d49d.PNG)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1d0wp64ca6uk1iln1570.PNG)

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.
4. Password Generation
Generates and sets a random password for the user, then stores it securely.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/297n87t72c7qnckstxx2.PNG)

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.

5. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjpfnyib9hvg2xgu6l9k.PNG)

2. Run the script.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/441yfgoivexokrgtcauv.PNG)

3. Check Outputs

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bulo60v4di00zpjiopcx.PNG)

- 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](https://hng.tech/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](https://hng.tech/hire) and [HNG Premium](https://hng.tech/premium).