DEV Community

Shirlyne Thiong'o
Shirlyne Thiong'o

Posted on

Using a Bash script to Automate User Account Management in Linux

It's critical for Sysops engineers to adopt the habit of effectively managing user accounts, particularly when onboarding new clients, users, or staff members. This post will walk you through the process of writing a Bash script that adds users, adds them to groups, generates passwords, and logs all of your activities automatically. This module is a component of the HNG Devops Track job. To start working practically with devops and all of its relevant stacks, HNG is a great place to start. Applying is possible, or you can learn more at https://hng.tech/internship. Now let's begin the assignment for today.

Overview
Handling user accounts by hand can be laborious and prone to mistakes. This procedure needs to be automated for consistency, time savings, and reduced mistake. Using a file containing a list of usernames and groups, we will develop a script named "create_users.sh" that creates users and groups, configures their home directories, generates random passwords, and logs all tasks, actions, and errors to a management.log file.

Prerequisites:
It is essential that you possess the following knowledge before we start; if not, please review previous modules to familiarize yourself with the ideas;

1.Basic familiarity with Linux commands
2.Rights to administer the system and
3.A text editor (such as vim, nano, etc.)

Summary

The following tasks are completed by the script:

1.reads from a file a list of users and groups.
2.establishes users and places them in designated groups.
3.creates home directories and grants the necessary access.
4.creates passwords at random for users.
5.records every operation in the file /var/log/user_management.log.
6./var/secure/user_passwords.csv is where the created passwords are safely stored.

Script Breakdown
Below is a breakdown of the complete script with explanations.

Bash shells are commonly found on Linux operating systems. In this article, we will be working primarily with Ubuntu, a Linux distribution. You can download and set up Ubuntu here: (Canonical Ubuntu)[http://ubuntu.com/download].
Once you have your terminal open, you should write the following scrips:

Image description

To open the created script for editing,iput he following:

Image description

This will open a terminal window on the nano text editor.Type the following script;

Image description

Image description

  • Save the script: Press Ctrl + O (the letter O, not zero) to write out (save) the file. Press Enter to confirm.

  • Exit Nano: Press Ctrl + X to exit the editor

To populate the users list,enter the following script,this will also open a nano text editor:

Image description

On the terminal:
Image description

  • Save the script: Press Ctrl + O (the letter O, not zero) to write out (save) the file. Press Enter to confirm.
  • Exit Nano: Press Ctrl + X to exit the editor.

Type the following script to confirm if the users are populated:

Image description

To execute the script with a text file containing usernames and groups as an argument,write the following script:

Image description

To verify the script:
After running the script, verify the results:

Log File: Check

/var/log/user_management.log
Enter fullscreen mode Exit fullscreen mode

for logged actions.

sudo cat /var/log/user_management.log
Enter fullscreen mode Exit fullscreen mode
  1. Password File: Check
/var/secure/user_passwords.csv
Enter fullscreen mode Exit fullscreen mode

for generated passwords.

sudo cat /var/secure/user_passwords.csv
Enter fullscreen mode Exit fullscreen mode
  1. User and Group Existence: Verify that users and groups were created
id user1
Enter fullscreen mode Exit fullscreen mode
id user2
Enter fullscreen mode Exit fullscreen mode
id user3
Enter fullscreen mode Exit fullscreen mode

In Conclusion:
The onboarding process for new users, staff, or accounts can be greatly streamlined by automating user account management with bash scripts. You may build a strong script that guarantees users are created, assigned to groups, and have safe passwords while logging actions for transparency and auditability by following the instructions we provided in this post. Remember that HNG made these challenges possible. You can also join their VIP channel to receive perks and study in a very supportive setting. To sign up, visit https://hng.tech/premium. I'll see you in the upcoming module. Thank you.

Top comments (0)