**Automating User Creation on Ubuntu with a Bash Script ** Introduction
Efficiently managing user accounts is crucial for system security and operations. This article explains a Bash script, create_users.sh
, which automates user creation based on a provided text file. The script reads usernames and groups, creates users and groups, sets up home directories, generates passwords, and logs all actions.
The create_users.sh
script:
- Reads a text file with usernames and group names.
- Creates users and groups as specified.
- Sets up home directories with appropriate permissions.
- Generates random passwords for the users.
- Logs all actions to
/var/log/user_management.log
. - Stores generated passwords securely in
/var/secure/user_passwords.csv
.
-
Initial Setup (Lines 1-11)
- Initializes log and password file paths.
- Creates necessary directories and files with secure permissions.
-
Logging Function (Lines 13-15)
- Writes messages to the log file with a timestamp.
-
Password Generation (Lines 17-19)
- Generates random passwords using
openssl
.
- Generates random passwords using
-
Argument and File Checks (Lines 21-25)
- Checks if the user file is provided and exists.
-
Main Logic (Lines 27-65)
- Processes each line of the user file.
- Creates users and their personal groups.
- Assigns users to additional groups.
- Generates and stores passwords.
- Logs all actions.
- Create the Input File
Create a text file with the following format:
light; sudo,dev,www-data
idimma; sudo
mayowa; dev,www-data
- Make the Script Executable
chmod +x create_users.sh
- Run the Script
sudo ./create_users.sh $(pwd)/users.txt
- Verify Logs and Passwords
Check the log file:
sudo cat /var/log/user_management.log
Check the password file:
sudo cat /var/secure/user_passwords.csv
Verify user creation:
id light
id idimma
id mayowa
This Bash script simplifies user and group creation on Ubuntu, ensuring consistency and security. This was a project for HNG cohort 11 .For more information on the HNG Internship program, visit (https://hng.tech/internship) and (https://hng.tech/premium).
By following this guide, you can streamline user management and enhance your system administration skills.