<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: James Chima</title>
    <description>The latest articles on DEV Community by James Chima (@james_chima).</description>
    <link>https://dev.to/james_chima</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1729606%2F7f72db99-36fc-41a8-9784-58cae3ffca79.jpg</url>
      <title>DEV Community: James Chima</title>
      <link>https://dev.to/james_chima</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/james_chima"/>
    <language>en</language>
    <item>
      <title>Automation of User management with Bash scripting</title>
      <dc:creator>James Chima</dc:creator>
      <pubDate>Thu, 04 Jul 2024 12:32:13 +0000</pubDate>
      <link>https://dev.to/james_chima/automation-of-user-management-with-bash-scripting-3g3o</link>
      <guid>https://dev.to/james_chima/automation-of-user-management-with-bash-scripting-3g3o</guid>
      <description>&lt;p&gt;Managing user accounts in a Linux environment can be repetitive and error-prone, especially in large organizations. To simplify this process as part of a task given to me on my journey with &lt;a href="https://hng.tech/internship"&gt;&lt;/a&gt;, you can use a bash script that automates user creation, and group assignments and sets appropriate permissions. This article provides a comprehensive script for this purpose and explains its functionality.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

#Create directory for logging
sudo mkdir /var/log/user_management.log
sudo mkdir /var/secure/user_passwords.txt

# Define the log file and password storage file
ACTION_LOG="/var/log/user_management.log"
PASSWORD_LOG="/var/secure/user_passwords.txt"

# Check if a file is provided as an argument
if [ $# -ne 1 ]; then
    echo "Usage: $0 &amp;lt;filename&amp;gt;" | tee -a "$ACTION_LOG"
    exit 1
fi

FILENAME=$1

# Check if the file exists
if [ ! -f "$FILENAME" ]; then
    echo "File $FILENAME does not exist." | tee -a "$ACTION_LOG"
    exit 1
fi

# Ensure the password file exists and is secured
sudo touch "$PASSWORD_LOG"
sudo chmod 600 "$PASSWORD_LOG"

# Function to generate a random password
generate_password() {
    openssl rand -base64 12
}

# Read the file line by line
while IFS=';' read -r username groups; do
    # Check if the user already exists
    if id -u "$username" &amp;gt;/dev/null 2&amp;gt;&amp;amp;1; then
        echo "User $username already exists." | tee -a "$ACTION_LOG"
    else
        # Create the user with a home directory and generate a random password
        password=$(generate_password)
        encrypted_password=$(openssl passwd -1 "$password")

        sudo useradd -m -p "$encrypted_password" "$username"
        if [ $? -eq 0 ]; then
            echo "User $username created with home directory." | tee -a "$ACTION_LOG"
            echo "$username:$password" | sudo tee -a "$PASSWORD_LOG" &amp;gt; /dev/null
        else
            echo "Failed to create user $username." | tee -a "$ACTION_LOG"
            continue
        fi
    fi

    # Assign the user to the groups
    IFS=',' read -ra group_array &amp;lt;&amp;lt;&amp;lt; "$groups"
    for group in "${group_array[@]}"; do
        # Check if the group exists, create it if it does not
        if ! getent group "$group" &amp;gt;/dev/null 2&amp;gt;&amp;amp;1; then
            sudo groupadd "$group"
            if [ $? -eq 0 ]; then
                echo "Group $group created." | tee -a "$ACTION_LOG"
            else
                echo "Failed to create group $group." | tee -a "$ACTION_LOG"
                continue
            fi
        fi

        # Add the user to the group
        sudo usermod -aG "$group" "$username"
        if [ $? -eq 0 ]; then
            echo "User $username added to group $group." | tee -a "$ACTION_LOG"
        else
            echo "Failed to add user $username to group $group." | tee -a "$ACTION_LOG"
        fi
    done

    # Set appropriate permissions for the home directory
    sudo chmod 700 "/home/$username"
    sudo chown "$username:$username" "/home/$username"
    if [ $? -eq 0 ]; then
        echo "Set permissions for home directory of $username." | tee -a "$ACTION_LOG"
    else
        echo "Failed to set permissions for home directory of $username." | tee -a "$ACTION_LOG"
    fi

done &amp;lt; "$FILENAME"

echo "User and group creation process completed." | tee -a "$ACTION_LOG"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above script reads a text file containing employee usernames and group names formatted as user;groups. It then creates the users, assigns them to the specified groups, sets up home directories, generates random passwords, and logs all actions to user_management.log.&lt;/p&gt;

&lt;p&gt;Firstly, we created the user_management.log and user_passwords.txt in the /var/log directory to be sure that the logs and the user password are saved as required. &lt;/p&gt;

&lt;p&gt;Next, we created a variable for log and password storage and assigned the variable to the location of the log file and password save file, we then created the user, added the user to a group and then set the right permission for the users.&lt;/p&gt;

&lt;p&gt;To learn about Bash scripting and more, you can check out this link  &lt;a href="https://hng.tech/hire"&gt;&lt;/a&gt;, to learn more about how to become part of the HNG internship.&lt;/p&gt;

</description>
      <category>bash</category>
      <category>linux</category>
      <category>automation</category>
    </item>
  </channel>
</rss>
