DEV Community

Orisadare Damilola
Orisadare Damilola

Posted on

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
Enter fullscreen mode Exit fullscreen mode

2. Prepare the Input File

subomi;sudo,dev,www-data
chukwu;dev,www-data
tolani;sudo
hassan;sudo,www-data
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

6. Verification

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

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

  • Check Group Membership
groups USERNAME
Enter fullscreen mode Exit fullscreen mode
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.

Top comments (0)