Automating User Management on Linux with Bash Scripting
Published: 2024-07-03 08:01:39

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:

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



#### 2. Prepare the Input File


```bash
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:

```bash
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`:

```bash
sudo ./create_users.sh input_file.txt
```


#### 6. Verification

- Check the Log File:

```bash
sudo cat /var/log/user_management.log
```
- Check the Password File:

```bash
sudo cat /var/secure/user_passwords.txt
```

- Verify User Creation
```bash
grep 'USERNAME' /etc/passwd
```
Replace the 'USERNAME' with one of the usernames from the input file.

- Check Group Membership 

```bash
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] (https://hng.tech), aimed at developing practical skills in software development. Interested in hiring from the HNG Internship? Explore opportunities [here] 
 (https://hng.tech/hire) or discover premium services [here] (https://hng.tech/premium).


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.