๐Ÿš€Day 5: Mastering Linux Shell Scripts for DevOps User Management

ยท

3 min read

๐Ÿš€Day 5: Mastering Linux Shell Scripts for DevOps User Management

Hey everyone! ๐ŸŒŸ Welcome back to my 90 Days of DevOps challenge. Today, we're diving into advanced Linux shell scripting, focusing on user management. Let's get started! ๐Ÿš€

Why Advanced User Management? ๐Ÿง‘โ€๐Ÿ’ป

In DevOps, managing users efficiently is crucial for maintaining security and organization. Advanced scripting helps automate complex tasks, saving time and reducing errors.

Advanced User Management Tasks ๐Ÿ› ๏ธ

1. Bulk User Creation ๐Ÿ‘ซ

Creating multiple users manually is time-consuming. Let's automate it with a script!

#!/bin/bash
# Script to create multiple users from a file

input="users.txt"
while IFS= read -r user
do
    sudo useradd $user
    echo "Created user: $user"
done < "$input"

Here, users.txt contains the usernames you want to create, one per line. The script reads each line and creates a user.

2. User Password Management ๐Ÿ”

Setting or changing passwords for users can be automated too.

#!/bin/bash
# Script to set passwords for users

input="user_passwords.txt"
while IFS= read -r line
do
    user=$(echo $line | cut -d':' -f1)
    password=$(echo $line | cut -d':' -f2)
    echo "$user:$password" | sudo chpasswd
    echo "Password set for user: $user"
done < "$input"

In user_passwords.txt, list the username and password separated by a colon (:), like user1:password1.

3. User Expiration Dates ๐Ÿ“…

Set expiration dates for user accounts to enhance security.

#!/bin/bash
# Script to set expiration dates for users

input="user_expirations.txt"
while IFS= read -r line
do
    user=$(echo $line | cut -d':' -f1)
    exp_date=$(echo $line | cut -d':' -f2)
    sudo chage -E $exp_date $user
    echo "Expiration date set for user: $user"
done < "$input"

In user_expirations.txt, list the username and expiration date in YYYY-MM-DD format, like user1:2024-12-31.

4. User Group Management ๐Ÿ‘ฅ

Adding users to groups can also be automated.

#!/bin/bash
# Script to add users to groups

input="user_groups.txt"
while IFS= read -r line
do
    user=$(echo $line | cut -d':' -f1)
    group=$(echo $line | cut -d':' -f2)
    sudo usermod -aG $group $user
    echo "Added $user to group $group"
done < "$input"

In user_groups.txt, list the username and group name separated by a colon, like user1:devops.

Putting It All Together ๐Ÿงฉ

Imagine combining these scripts into a single, powerful user management tool. Here's a simple example:

#!/bin/bash
# Comprehensive user management script

create_users() {
    input="users.txt"
    while IFS= read -r user
    do
        sudo useradd $user
        echo "Created user: $user"
    done < "$input"
}

set_passwords() {
    input="user_passwords.txt"
    while IFS= read -r line
    do
        user=$(echo $line | cut -d':' -f1)
        password=$(echo $line | cut -d':' -f2)
        echo "$user:$password" | sudo chpasswd
        echo "Password set for user: $user"
    done < "$input"
}

set_expirations() {
    input="user_expirations.txt"
    while IFS= read -r line
    do
        user=$(echo $line | cut -d':' -f1)
        exp_date=$(echo $line | cut -d':' -f2)
        sudo chage -E $exp_date $user
        echo "Expiration date set for user: $user"
    done < "$input"
}

add_to_groups() {
    input="user_groups.txt"
    while IFS= read -r line
    do
        user=$(echo $line | cut -d':' -f1)
        group=$(echo $line | cut -d':' -f2)
        sudo usermod -aG $group $user
        echo "Added $user to group $group"
    done < "$input"
}

create_users
set_passwords
set_expirations
add_to_groups

This script can simultaneously handle user creation, password setting, expiration dates, and group assignments! ๐Ÿš€

Happy scripting! ๐Ÿ˜Š


Connect with me on LinkedIn to follow my journey and learn together! ๐Ÿš€

ย