<?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>Forem: Lois Oseodion</title>
    <description>The latest articles on Forem by Lois Oseodion (@lois_oseodion_9055bdf056d).</description>
    <link>https://forem.com/lois_oseodion_9055bdf056d</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%2F1723714%2F79cf60b4-59f2-4210-abf8-1d2055e2fdb3.png</url>
      <title>Forem: Lois Oseodion</title>
      <link>https://forem.com/lois_oseodion_9055bdf056d</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lois_oseodion_9055bdf056d"/>
    <language>en</language>
    <item>
      <title>My HNG Experience Stage One: User Management and Automation With Bash script</title>
      <dc:creator>Lois Oseodion</dc:creator>
      <pubDate>Fri, 05 Jul 2024 14:53:28 +0000</pubDate>
      <link>https://forem.com/lois_oseodion_9055bdf056d/my-hng-experience-stage-one-user-management-and-automation-with-bash-script-1h0g</link>
      <guid>https://forem.com/lois_oseodion_9055bdf056d/my-hng-experience-stage-one-user-management-and-automation-with-bash-script-1h0g</guid>
      <description>&lt;p&gt;The &lt;a href="https://hng.tech/internship"&gt;HNG&lt;/a&gt; Internship has me on a thrilling ride! My first project is to create a Bash script to automate user management on a Linux server. This project showcases scripting's power and highlights the skills I'm gaining at &lt;a href="https://hng.tech/internship"&gt;HNG&lt;/a&gt;. Get ready to see how this script simplifies user and group management!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites and Requirements&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Prerequisites:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Access to a Linux environment (e.g., Ubuntu)&lt;br&gt;
Basic understanding of how to run scripts and manage files in a Linux terminal&lt;br&gt;
Permissions to create users, groups, and files&lt;br&gt;
Requirements:&lt;br&gt;
Input File Format: The script will read a text file where each line is formatted as {username; groups}.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kelvin; admin,dev
Hannah; dev,tester
Gift; admin,tester
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Script Actions:&lt;/strong&gt;&lt;br&gt;
Create users (kelvin, Hannah, Gift) and their personal groups (admin, dev, tester).&lt;br&gt;
Place users in the designated additional groups (admin, dev, tester).&lt;br&gt;
Create home directories for each user with the correct permissions.&lt;br&gt;
Create random passwords for each user.&lt;br&gt;
Record all actions in /var/log/user_management.log.&lt;br&gt;
Save passwords securely in /var/secure/user_passwords.txt.&lt;br&gt;
Gracefully manage errors, such as users or groups that already exist.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step-by-Step Implementation&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Step 1:&lt;/strong&gt; &lt;br&gt;
Script Initialization and Setup&lt;br&gt;
Set up the initial environment for the script, including defining file locations and creating necessary directories.&lt;br&gt;
Define File Locations: Initializes paths for logging and password storage.&lt;br&gt;
Create Directories: Ensures necessary directories exist.&lt;br&gt;
Set File Permissions: Create and set permissions for the log and password files.&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

# Define log and password file locations
LOG_FILE="/var/log/user_management.log"
PASSWORD_FILE="/var/secure/user_passwords.txt"

# Create Directories
mkdir -p /var/log
mkdir -p /var/secure

# Create and set permissions for the log file
touch $LOG_FILE
chmod 644 $LOG_FILE

# Create and set permissions for the password file
touch $PASSWORD_FILE
chmod 600 $PASSWORD_FILE

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt;&lt;br&gt;
 &lt;strong&gt;Logging Function Creation&lt;/strong&gt;&lt;br&gt;
Create a function to log actions performed by the script with timestamps.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Function to log messages with timestamps
log_action() {
  echo "$(date '+%Y-%m-%d %H:%M:%S') : $1" &amp;gt;&amp;gt; $LOG_FILE
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt;&lt;br&gt;
 &lt;strong&gt;Argument Checking&lt;/strong&gt;&lt;br&gt;
Verify that the script is provided with the correct number of arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Check if a correct number of arguments is provided.
if [ $# -ne 1 ]; then
  log_action "Usage: $0 &amp;lt;user-list-file&amp;gt;. Exiting."
  exit 1
fi

USER_LIST_FILE=$1

# Check if user list file exists
if [ ! -f $USER_LIST_FILE ]; then
  log_action "File $USER_LIST_FILE does not exist! Exiting."
  exit 1
fi

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt;&lt;br&gt;
 &lt;strong&gt;Reading and Processing User List&lt;/strong&gt;&lt;br&gt;
Read each line from the user list file, extracting usernames and associated groups.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Process each line in the user list file
while IFS=';' read -r username groups; do
  username=$(echo $username | xargs)
  groups=$(echo $groups | xargs)

  # Further actions based on extracted data will be performed in subsequent steps.
done &amp;lt; $USER_LIST_FILE

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt;&lt;br&gt;
 &lt;strong&gt;User Existence Checking and Creation&lt;/strong&gt;&lt;br&gt;
Verify if each user already exists; if not, create the user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Check if the user already exists
if id -u $username &amp;gt;/dev/null 2&amp;gt;&amp;amp;1; then
  log_action "User $username already exists. Skipping."
  continue
fi

# Create the user if they do not exist
useradd -m $username
if [ $? -eq 0 ]; then
  log_action "User $username created successfully."
else
  log_action "Failed to create user $username."
  continue
fi

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt;&lt;br&gt;
 &lt;strong&gt;Group Handling&lt;/strong&gt;&lt;br&gt;
Create the necessary groups for each user and assign them appropriately.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Assign user to specified additional groups
IFS=',' read -ra USER_GROUPS &amp;lt;&amp;lt;&amp;lt; "$groups"
for group in "${USER_GROUPS[@]}"; do
  group=$(echo $group | xargs)
  if ! getent group $group &amp;gt;/dev/null; then
    groupadd $group
    if [ $? -eq 0 ]; then
      log_action "Group $group created successfully."
    else
      log_action "Failed to create group $group."
      continue
    fi
  fi
  usermod -aG $group $username
  log_action "User $username added to group $group."
done

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt;&lt;br&gt;
 &lt;strong&gt;Home Directory Setup&lt;/strong&gt;&lt;br&gt;
Ensure each user has a home directory set up with appropriate permissions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Set up home directory permissions
chmod 755 /home/$username
chown $username:$username /home/$username
log_action "Home directory permissions set for user $username."

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 8:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Password Generation and Storage&lt;/strong&gt;&lt;br&gt;
Generate a secure password for each user and store it securely.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Generate and store passwords securely
password=$(date +%s | sha256sum | base64 | head -c 12 ; echo)
echo "$username,$password" &amp;gt;&amp;gt; $PASSWORD_FILE
log_action "Password for user $username set successfully."

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 9:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Script Completion and Finalization&lt;/strong&gt;&lt;br&gt;
Conclude the script execution, logging the completion of all actions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Final log entry
log_action "Script execution completed."

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

&lt;/div&gt;



&lt;p&gt;Putting It All Together&lt;br&gt;
Here's the complete script:&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

# Step 1: Define File Locations
LOG_FILE="/var/log/user_management.log"
PASSWORD_FILE="/var/secure/user_passwords.txt"

# Step 2: Create Directories
mkdir -p /var/log
mkdir -p /var/secure

# Step 3: Set File Permissions
touch $PASSWORD_FILE
chmod 600 $PASSWORD_FILE
touch $LOG_FILE
chmod 644 $LOG_FILE

# Step 4: Define Logging Function
log_action() {
  echo "$(date '+%Y-%m-%d %H:%M:%S') : $1" &amp;gt;&amp;gt; $LOG_FILE
}

# Step 5: Argument Checking
if [ $# -ne 1 ]; then
  log_action "Usage: $0 &amp;lt;user-list-file&amp;gt;. Exiting."
  exit 1
fi

USER_LIST_FILE=$1

if [ ! -f $USER_LIST_FILE ]; then
  log_action "File $USER_LIST_FILE does not exist! Exiting."
  exit 1
fi

# Step 6: Reading and Processing User List
while IFS=';' read -r username groups; do
  username=$(echo $username | xargs)
  groups=$(echo $groups | xargs)

  # Step 7: User Existence Checking and Creation
  if id -u $username &amp;gt;/dev/null 2&amp;gt;&amp;amp;1; then
    log_action "User $username already exists. Skipping."
    continue
  fi

  useradd -m $username
  if [ $? -eq 0 ]; then
    log_action "User $username created successfully."
  else
    log_action "Failed to create user $username."
    continue
  fi

  # Step 8: Group Handling
  IFS=',' read -ra USER_GROUPS &amp;lt;&amp;lt;&amp;lt; "$groups"
  for group in "${USER_GROUPS[@]}"; do
    group=$(echo $group | xargs)
    if ! getent group $group &amp;gt;/dev/null; then
      groupadd $group
      if [ $? -eq 0 ]; then
        log_action "Group $group created successfully."
      else
        log_action "Failed to create group $group."
        continue
      fi
    fi
    usermod -aG $group $username
    log_action "User $username added to group $group."
  done

  # Step 9: Home Directory Setup
  chmod 755 /home/$username
  chown $username:$username /home/$username
  log_action "Home directory permissions set for user $username."

  # Step 10: Password Generation and Storage
  password=$(date +%s | sha256sum | base64 | head -c 12 ; echo)
  echo "$username,$password" &amp;gt;&amp;gt; $PASSWORD_FILE
  log_action "Password for user $username set successfully."

done &amp;lt; $USER_LIST_FILE

# Step 11: Script Completion and Finalization
log_action "Script execution completed."

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Trying It Out&lt;/strong&gt;&lt;br&gt;
Save the file as create_user.sh.&lt;br&gt;
Upload it to a GitHub repository.&lt;br&gt;
Clone the repository to a Linux server.&lt;br&gt;
Run the script with the user list file as an argument.&lt;/p&gt;

&lt;p&gt;The HNG project is more than just an internship; it is a transformative experience that equips participants with the skills, knowledge, and confidence needed to thrive in the fast-paced tech industry. Honestly, I am enjoying it. Thanks for taking the time to read this far. Please kindly like and leave a comment. Thank you!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>devops</category>
      <category>bash</category>
      <category>aws</category>
    </item>
  </channel>
</rss>
