Creating Users and Groups with a Bash Script: An Automated Approach

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 i…


This content originally appeared on DEV Community and was authored by Laban🔌💻

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

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.
  1. Setting Up Home Directories: Creates home directories for each user with appropriate permissions and ownership.

  2. Generating Random Passwords: Utilizes openssl to generate random passwords for each user and securely sets them.

  3. Logging Actions: Logs all script actions to /var/log/user_management.log for audit and troubleshooting purposes.

  4. Storing Passwords Securely: Stores generated passwords securely in /var/secure/user_passwords.csv with strict file permissions.

  5. 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 program and discover top tech talent through HNG 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.

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


This content originally appeared on DEV Community and was authored by Laban🔌💻


Print Share Comment Cite Upload Translate Updates
APA

Laban🔌💻 | Sciencx (2024-07-01T16:35:31+00:00) Creating Users and Groups with a Bash Script: An Automated Approach. Retrieved from https://www.scien.cx/2024/07/01/creating-users-and-groups-with-a-bash-script-an-automated-approach/

MLA
" » Creating Users and Groups with a Bash Script: An Automated Approach." Laban🔌💻 | Sciencx - Monday July 1, 2024, https://www.scien.cx/2024/07/01/creating-users-and-groups-with-a-bash-script-an-automated-approach/
HARVARD
Laban🔌💻 | Sciencx Monday July 1, 2024 » Creating Users and Groups with a Bash Script: An Automated Approach., viewed ,<https://www.scien.cx/2024/07/01/creating-users-and-groups-with-a-bash-script-an-automated-approach/>
VANCOUVER
Laban🔌💻 | Sciencx - » Creating Users and Groups with a Bash Script: An Automated Approach. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/01/creating-users-and-groups-with-a-bash-script-an-automated-approach/
CHICAGO
" » Creating Users and Groups with a Bash Script: An Automated Approach." Laban🔌💻 | Sciencx - Accessed . https://www.scien.cx/2024/07/01/creating-users-and-groups-with-a-bash-script-an-automated-approach/
IEEE
" » Creating Users and Groups with a Bash Script: An Automated Approach." Laban🔌💻 | Sciencx [Online]. Available: https://www.scien.cx/2024/07/01/creating-users-and-groups-with-a-bash-script-an-automated-approach/. [Accessed: ]
rf:citation
» Creating Users and Groups with a Bash Script: An Automated Approach | Laban🔌💻 | Sciencx | https://www.scien.cx/2024/07/01/creating-users-and-groups-with-a-bash-script-an-automated-approach/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.