Automating Linux User Creation with Bash Script
Published: 2024-07-05 13:49:45

🔆♥️


In today's fast-paced technology environment, efficiency and automation are key. 
**Automating tasks with a Bash
script** can save a significant amount of time and reduce errors. In this technical report, we will walk through the process of creating a Bash script to automate user and group creation, setting up home directories, and managing permissions and passwords.

**Project Overview**
Your company has recently hired several new developers, and you need to create user accounts and groups for them. To streamline this process, we will write a Bash script called create_users.sh.
This script will;

1. Read a text file containing usernames and group names,
2. Create users and groups as specified,
3. Set up home directories,
4. Generate random passwords, and
5. Log all actions to /var/log/user_management.log and store the generated passwords securely in /var/secure/user_passwords.txt.
We can create the Bash script called "create_users.sh" with this command;
Bash script

**Implementation steps**
Let's walk through the script step-by-step to understand its functionality.

1. **Checking root privileges;**
This line specifies that the script should be executed with the Bash shell.
shabang
The script checks if it is being run as root. If not, it prompts the user to run the script with root privileges and exits.
_Root privileges_
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxii7e4l6eriof36gaci.png)

2. **Checking for User Data File;**
The script checks if the filename (user-data-file) is provided as an argument. If not, it displays the correct usage and exits.
_user data file_
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvqm67zi03dt1m1657oj.png)

3. **Initializing Variables and Creating Directories;**
The script creates the necessary directories and sets appropriate permissions to ensure security.
Here, The 'user_data_file' stores the filename provided as an argument. Additionally 'log_file' and 'password_file' store the paths for logging actions and storing passwords.
_Initialize variables_
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wm421nvk4l05qho3gea6.png)

4. **Generating Random Passwords:**
A function to generate random passwords using openssl.
_Random password_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrzc0wlngrdis4we1nwr.png)

5. **Reading User Data File and Creating Users;**
The script reads the user data file line by line. For each line, it:
. Trims any leading or trailing whitespaces from the username and groups.
. Checks if the user already exists. If so, it logs the information and moves to the next user.
. Creates the user and assigns them a personal group.
_creating users_
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3iyk51w61eauqgjej88e.png)

6. **Adding Users to Additional Groups;**
If additional groups are specified, the script adds the user to these groups, creating the groups if they do not exist.
_Adding users_
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/78d33rqv62350v69nvny.png)

7. **Setting Home Directory Permissions;**
The script sets appropriate permissions for the user's home directory.
_Directory permission_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d7wl3akdco5q42cwclxv.png)

8. **Generating and Storing Passwords;**
It generates a random password, sets it for the user, and stores it in the password file.
_Store passwords_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/acayhxjp5ic7ty4o3fgl.png)

9. **Logging Actions;**
Finally, the script logs all actions and completes the user creation process.
_Logging actions_
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjt8liiw4il6b9d96dxv.png)

**Running the script;**

1. **Create the txt file containing the users and the groups;**
The user accounts' structure is contained in this text file. Save and close the file.
_txt file._
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6wsubn20hg96om1eqjm.png)
Every line in the file identifies a user along with the groups (such "admin" or "finance") to which they are assigned. The semicolon divides the groups and users. users.txt has the structure:
_user datafile._
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9f5cj0lw2qylpod5uf03.png)

2. **Ensure the script is executable;**
_Execute script_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aypcl5f2xfxragsdkeqc.png)

3. **Run script;**
_Run script_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7l5ip9wdgyc2umg3tnbn.png)

**Verify the results**

1. Check the log file for actions performed;

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/43yjk3ab0ij0jda4pu1b.png)

2. Verify the user passwords file;

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w0nsvujruf4uhn8nuzvv.png)

3.Ensure the new users and groups are created correctly;

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gzu86ippe1i0zdsjoy20.png)

Conclusion
This script automates the creation of users and groups, ensuring a streamlined onboarding process. This article is a stage two task in the DevOps of HNG internship. For more information about the HNG Internship and how it can benefit your organization, visit [HNG Internship](https://hng.tech/internship) and [HNG Hire](https://hng.tech/hire).

By using this tutorial, you can make your organization's user management procedure more efficient and ensure that new developers are onboarded promptly

Wishing you the best as you continue your Tech journey 🙂💯.