Creating Users and Groups with a Bash Script: An Automated Approach
Published: 2024-07-01 16:35:31



As a SysOps engineer, efficiently managing user accounts and groups is crucial for maintaining a secure and organized system environment. Automating the user creation process not only saves significant time but also reduces human errors. This article introduces a Bash script, `create_users.sh`, designed to read a text file with user information, create users and groups, set up home directories, generate random passwords, log actions for auditing purposes, and securely store passwords.

Script file is located in  [this link](https://github.com/Laban254/user-management-automation-script/blob/main/create_users.sh)
#### Overview of the Script Functionality

The Bash script automates the following tasks:

1.  **Reading User Data**: Reads a text file (`users.txt`) containing usernames and associated groups. Each line in the file is formatted as `username;groups`, where groups are comma-separated.
    
2.  **Creating Users and Groups**:
    
    -   Each user gets a personal group with the same name as the username.
    -   Additional groups specified in the file are created if they do not exist.
    -   Users are added to their personal group and any additional groups.
3.  **Setting Up Home Directories**: Creates home directories for each user with appropriate permissions and ownership.
    
4.  **Generating Random Passwords**: Utilizes `openssl` to generate random passwords for each user and securely sets them.
    
5.  **Logging Actions**: Logs all script actions to `/var/log/user_management.log` for audit and troubleshooting purposes.
    
6.  **Storing Passwords Securely**: Stores generated passwords securely in `/var/secure/user_passwords.csv` with strict file permissions.
    
7.  **Error Handling**: Includes robust error handling for scenarios such as existing users or groups.
    

#### Key Features

-   **Verbose Mode**: Enables detailed output during execution with the `-v` option.
-   **Dry Run Option**: Simulates user creation without making changes using the `-d` option.
-   **Backup Existing Password File**: Safeguards the existing password file by creating a backup before modifications.

#### Usage

`sudo ./create_users.sh users.txt` 

For verbose output:

`sudo ./create_users.sh -v users.txt` 

For a dry run (simulation):


`sudo ./create_users.sh -d users.txt` 

Ensure your `users.txt` file is formatted correctly:


    light;sudo,dev,www-data
    idimma;sudo
    mayowa;dev,www-data

#### Benefits of Automation

Automating user creation offers numerous benefits:

-   **Efficiency**: Saves time by automating repetitive tasks.
-   **Consistency**: Ensures uniform user setup across systems.
-   **Security**: Generates strong, random passwords and stores them securely.
-   **Auditing**: Logs all actions for accountability and auditing purposes.

### Conclusion

The `create_users.sh` script provides a reliable solution for automating user management tasks, enhancing system security, and maintaining consistency. By securely storing passwords and logging all operations, it supports accountability and facilitates efficient system administration.

For internship opportunities, explore the [HNG Internship](https://hng.tech/internship) program and discover top tech talent through [HNG Hire](https://hng.tech/hire). These platforms offer valuable resources for skill development and career advancement in the tech industry.

For the complete script and detailed implementation, visit the [GitHub repository](https://github.com/Laban254/user-management-automation-script).

By leveraging automation tools like Bash scripting, you can streamline system administration tasks, bolster security, and improve overall operational efficiency in managing your infrastructure.