Cookies Psst! Do you accept cookies?

We use cookies to enhance and personalise your experience.
Please accept our cookies. Checkout our Cookie Policy for more information.

Automating User Management on Linux with Bash Scripting

Managing user accounts and permissions on a Linux system is a fundamental task for system administrators and DevOps teams. Automating this process can streamline operations and ensure consistency across environments. In this guide, we'll walk through how to use a Bash script (create_users.sh) to automate user and group management efficiently.

Overview of create_users.sh
The create_users.sh script is designed to read user and group information from an input file (input_file.txt), create user accounts with specified groups, generate secure passwords, and log all actions for audit purposes. This script is particularly useful in environments where user provisioning and access control need to be automated.

Prerequisites
Before you begin, ensure you have the following:

Access to a Linux-based system (e.g., Ubuntu, CentOS)
MobaXterm for SSH operations (optional for remote execution)
Basic understanding of Bash scripting and command-line operations

Step-by-Step Guide

1. Clone the repository

first, clone the github repository containing the 'create_users.sh' script:

git clone https://github.com/Madeblaq/Bash-script.git

2. Prepare the Input File

subomi;sudo,dev,www-data
chukwu;dev,www-data
tolani;sudo
hassan;sudo,www-data

3. Upload Files to Your Linux System using Mobaxterm

  • Open MobaXterm and connect to your Linux server via SSH.

  • Use the SFTP panel on the left side to upload create_users.sh and input_file.txt to your Linux server. Drag and drop the files into the desired directory.

4. Convert Line Endings and Make the Script Executable

Connect to your Linux server via SSH using MobaXterm and navigate to the directory where create_users.sh is located. Then, ensure proper line endings with dos2unix and make the script executable:

sudo apt-get install dos2unix
dos2unix create_users.sh
chmod +x create_users.sh

5. Execute the Script

Run the create_users.sh script with sudo to create users and groups based on input_file.txt:

sudo ./create_users.sh input_file.txt

6. Verification

  1. Check the Log File:
sudo cat /var/log/user_management.log
  1. Check the Password File:
sudo cat /var/secure/user_passwords.txt
  1. Verify User Creation
grep 'USERNAME' /etc/passwd

Replace the 'USERNAME' with one of the usernames from the input file.

  1. Check Group Membership
groups USERNAME
7. Conclusion

By leveraging create_users.sh, you streamline user management tasks on your Linux system, enhancing operational efficiency and security. This script is not only a time-saver but also promotes consistency and accuracy in user provisioning, crucial for maintaining a well-managed IT infrastructure.

8. Learn More About HNG Internship

This project is part of the HNG Internship, aimed at developing practical skills in software development. Interested in hiring from the HNG Internship? Explore opportunities here or discover premium services here.

This blog post provides a technical guide on setting up and using create_users.sh for automated user management on Linux. The step-by-step format and copyable code snippets make it easy to follow and implement. Adjust the paths and commands as per your specific setup, and feel free to enhance the content with visuals or additional technical insights as needed.

Thank you for reading! If you have any questions or feedback, feel free to leave a comment below.

Last Stories

What's your thoughts?

Please Register or Login to your account to be able to submit your comment.