Automating User Management in Linux with Bash
Tags: linux, bash, automation, devops
Published: 2024-07-01 17:48:01

Hey DevOps enthusiasts!

If you're anything like me, you've probably found yourself stuck in the repetitive cycle of managing user accounts and groups, especially when onboarding new employees. It's one of those essential tasks that, while critical, can eat up a lot of your valuable time. But what if I told you there's a way to automate this process, ensuring consistency, efficiency, and security?

Today, I'm excited to walk you through a nifty Bash script that takes the hassle out of user management. This script reads a text file containing usernames and groups, creates users, assigns them to groups, generates random passwords, logs all actions, and ensures that all passwords are stored securely. By the end of this tutorial, you'll have a powerful tool in your arsenal that will make user management a breeze.


![running scripts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dqno1hbagopyr7gkb9ia.PNG)

I will use the bash script I completed as part of the requirements for stage1 of the HNG Internship

You can access the bash script [here](https://github.com/patty6339/hng11/blob/main/stage1_bash_script/create_users.sh)

## Script Overview

The script create_users.sh performs the following tasks:

- Reads Input File: It reads a text file containing usernames and groups.
- Creates Users and Groups: It creates users and their primary groups, as well as additional groups if specified.
- Generates Passwords: It generates random passwords for the users.
- Logs Actions: It logs all actions to /var/log/user_management.log.
- Stores Passwords Securely: It stores the generated passwords in /var/secure/user_passwords.csv with appropriate permissions.

## Key Components of the Script

1. Logging: The function log_message writes messages to both the console and a log file, ensuring that actions are documented.
2. Root Privileges Check: The script ensures it is run as root to perform user and group management tasks.
3. Directory and File Permissions: The script ensures that the /var/secure directory and the password file have the correct permissions to prevent unauthorized access.
4. User and Group Management: The script handles the creation of users and groups, adding users to their respective groups, and generating random passwords using openssl.
5. Error Handling: The script includes error handling for scenarios like existing users and groups, ensuring robust execution.

### Example Input File

```
user1;group1,group2
user2;group3
user3;
```
Save the code above in a plaintext file e.g. test_input.txt

Next,
Run the Script:
`sudo ./create_users.sh test_input.txt`

**_Security Considerations_**

- Password Storage: Passwords are stored in /var/secure/user_passwords.csv with permissions set to ensure only the owner can read the file.
- Logging: Logs are stored in /var/log/user_management.log for audit purposes.

Parting Shot

Automating user and group management with a Bash script is a game-changer for simplifying the onboarding process and ensuring consistency. This approach is not only efficient but also enhances security by ensuring proper permissions and logging. By following the principles outlined in this script, you can tailor it to meet your specific requirements.

As I continue in the journey of honing my DevOps skills, I highly recommend that you check out the HNG Internship program. It's an excellent platform for budding engineers to learn, grow, and connect with industry professionals. Dive into the details of the [HNG Internship](https://hng.tech/internship) to learn more about the opportunities they offer, or explore how they can help you [hire top talent](https://hng.tech/hire) for your next big project.

Happy scripting, and keep pushing the boundaries of what's possible in DevOps!