Creating Users and Groups with a Bash Script: An Automated Approach
Kibe🔌💻
Posted on July 1, 2024
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:
Reading User Data: Reads a text file (
users.txt
) containing usernames and associated groups. Each line in the file is formatted asusername;groups
, where groups are comma-separated.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.
Setting Up Home Directories: Creates home directories for each user with appropriate permissions and ownership.
Generating Random Passwords: Utilizes
openssl
to generate random passwords for each user and securely sets them.Logging Actions: Logs all script actions to
/var/log/user_management.log
for audit and troubleshooting purposes.Storing Passwords Securely: Stores generated passwords securely in
/var/secure/user_passwords.csv
with strict file permissions.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.
Posted on July 1, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.