This script automates the creation of users based on input from a specified file. It also sets up home directories, assigns appropriate permissions, generates random passwords, adds the users to specified groups and logs all important actions.
- This script must be run with root privileges.
- Ensure the input file is formatted correctly: each line should contain a username followed by groups, separated by a semicolon (;). Groups should be comma-separated.
light; sudo,dev,www-data
idimma; sudo
mayowa; dev,www-data
- Make the script executable:
chmod +x create_users.sh
- Run the script with the input file as an argument:
sudo ./create_users.sh <user_file>
Replace <user_file>
with the path to your input file.
- Log File: /var/log/user_management.log
- Holds logs of actions performed by the script.
- Password File: /var/secure/user_passwords.csv
- Stores generated passwords securely.
-
Root Privileges Check: The script verifies it is being run with root privileges.
-
Input File Check: Ensures the input file is provided and properly formatted.
-
User and Group Creation:
- Creates a user with a home directory and default shell.
- Creates a personal group for the user if it doesn't exist.
- Sets up appropriate permissions for the home directory.
- Adds the user to additional specified groups, creating those groups if they don't exist.
-
Password Generation:
- Generates a random password for each user.
- Sets the password and stores it in a secure file.
-
Logging:
- Logs all actions and any errors encountered.
- Logs successful completion of user creation.
- The script must be run as root.
- Ensure the input file follows the specified format to avoid errors.
- Check /var/log/user_management.log for a detailed log of actions and /var/secure/user_passwords.csv for the generated passwords.
This script automates the tedious process of creating users and groups, setting up directories, and managing passwords. It ensures consistency and security in some user management tasks.
For more details about the HNG Internship and opportunities, check out the HNG Internship and HNG Premium.