User Creation Automation in Linux with Bash Scripts

This article details creating a bash script for automating user creation in a Linux machine with an input file containing usernames and group memberships. This script was created for the HNG Internship program and demonstrates practical scripting appli…


This content originally appeared on DEV Community and was authored by Folayan Donald Michael

This article details creating a bash script for automating user creation in a Linux machine with an input file containing usernames and group memberships. This script was created for the HNG Internship program and demonstrates practical scripting applications.

What does the script do?

The script accomplishes the following key tasks:

  • Reads User Data: It parses a user data file (user_data.txt) containing usernames and their corresponding groups (separated by semicolons).
  • User Creation: It creates user accounts with home directories based on the provided usernames.
  • Group Management: It allows specifying user groups in the user data file. If groups exist, the script adds users to the respective groups using usermod.
  • Home Directory Permissions: It sets appropriate permissions for user home directories.
  • Password Generation: It generates random passwords for each user.
  • Password Storage: It stores usernames and passwords in a CSV file (user_password.csv).
  • Logging: It logs actions throughout execution for reference and troubleshooting.

Security Considerations

The provided script demonstrates password storage in a plain text file. In a production environment, implement secure password management practices like password hashing or integration with a directory service. Additionally, the script should be run with least privilege principles in mind.

Prerequisites

Before using this file, ensure you have the following:

  • Linux System: The script is designed for use on Linux systems with Bash as the default shell.
  • Bash Shell: Basic understanding of Bash scripting is helpful.
  • Essential Utilities: The script utilizes utilities like useradd, groupadd, sed, chmod, openssl, chpasswd, and date. Make sure these are available on your system.
  • Root Privileges: The script requires running with root privileges to create user accounts and modify system directories. You can use sudo to run the script with elevated permissions.
  • user_data.txt File: This file needs to exist in the same directory as the script and define usernames and groups (one line per user, semicolon separated).

Explanation of the script

  1. Script Setup:
  2. The script starts with a #!/bin/bash line, specifying the interpreter (Bash) to execute the script.

  3. Error Handling and Input Validation:

  4. The script checks if it's running with root privileges ($EUID -ne 0) using if statements. If not, it displays an error message and exits.
    It verifies if the user provides the input file path using if [ -z "$1" ]. If missing, it displays an error message and exits.

  5. User Data Processing:

  6. It assigns the provided file path (first command-line argument) to a variable, input_file.

  7. It opens the user data file and reads each line in a loop.

  8. Each line is expected to be formatted as username;group1,group2,... (username followed by semicolon-separated groups).

  9. User and Group Management:

  10. The script extracts the username and group information from each line.

  11. It uses useradd to create a user account with a home directory based on the username.

  12. If groups are specified, it uses usermod to add the user to the corresponding groups listed in the user data file.

  13. Home Directory Permissions:

  14. The script set appropriate permissions for the user's home directory using chmod.

  15. Password Generation and Storage:

  16. The script generates random passwords for each user using openssl rand -base64 12.

  17. Script Completion and Logging:

  18. Upon successful user creation, the script logs a completion message (log_message).

  19. It displays a success message indicating the location of the log file (/var/log/user_management.log).

Script Usage

You can clone the script from my github page

Running the script

  1. Clone the script using git clone git@github.com:donfolayan/hng-stage-1-project.git.
  2. Make the script executable using chmod +x create_users.sh.
  3. Run the script with root privileges (using sudo) and provide the user data file path as an argument:
sudo ./create_users.sh user_data.txt

For further exploration of system administration and automation techniques, consider exploring the HNG Internship program (https://hng.tech/hire) or the HNG Premium membership (https://hng.tech/premium) for access to additional resources and professional development opportunities.


This content originally appeared on DEV Community and was authored by Folayan Donald Michael


Print Share Comment Cite Upload Translate Updates
APA

Folayan Donald Michael | Sciencx (2024-07-04T21:57:26+00:00) User Creation Automation in Linux with Bash Scripts. Retrieved from https://www.scien.cx/2024/07/04/user-creation-automation-in-linux-with-bash-scripts/

MLA
" » User Creation Automation in Linux with Bash Scripts." Folayan Donald Michael | Sciencx - Thursday July 4, 2024, https://www.scien.cx/2024/07/04/user-creation-automation-in-linux-with-bash-scripts/
HARVARD
Folayan Donald Michael | Sciencx Thursday July 4, 2024 » User Creation Automation in Linux with Bash Scripts., viewed ,<https://www.scien.cx/2024/07/04/user-creation-automation-in-linux-with-bash-scripts/>
VANCOUVER
Folayan Donald Michael | Sciencx - » User Creation Automation in Linux with Bash Scripts. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/04/user-creation-automation-in-linux-with-bash-scripts/
CHICAGO
" » User Creation Automation in Linux with Bash Scripts." Folayan Donald Michael | Sciencx - Accessed . https://www.scien.cx/2024/07/04/user-creation-automation-in-linux-with-bash-scripts/
IEEE
" » User Creation Automation in Linux with Bash Scripts." Folayan Donald Michael | Sciencx [Online]. Available: https://www.scien.cx/2024/07/04/user-creation-automation-in-linux-with-bash-scripts/. [Accessed: ]
rf:citation
» User Creation Automation in Linux with Bash Scripts | Folayan Donald Michael | Sciencx | https://www.scien.cx/2024/07/04/user-creation-automation-in-linux-with-bash-scripts/ |

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.