<?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: Adebimpe peter</title>
    <description>The latest articles on DEV Community by Adebimpe peter (@adebimpe_peter_285cdfed0c).</description>
    <link>https://dev.to/adebimpe_peter_285cdfed0c</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%2F1718123%2F58a8c7d2-7b97-4702-82e3-9e7a086c8055.png</url>
      <title>DEV Community: Adebimpe peter</title>
      <link>https://dev.to/adebimpe_peter_285cdfed0c</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adebimpe_peter_285cdfed0c"/>
    <language>en</language>
    <item>
      <title>Automating User and Group Creation Using Bash script</title>
      <dc:creator>Adebimpe peter</dc:creator>
      <pubDate>Tue, 02 Jul 2024 14:29:39 +0000</pubDate>
      <link>https://dev.to/adebimpe_peter_285cdfed0c/automating-user-and-group-creation-using-bash-script-a00</link>
      <guid>https://dev.to/adebimpe_peter_285cdfed0c/automating-user-and-group-creation-using-bash-script-a00</guid>
      <description>&lt;p&gt;Automating the creation of users and groups can help with administrative tasks and ensure adequate consistency across systems. This demonstrates how to create a Bash script that reads user and group information from a file and processes it accordingly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Below is a Bash script that reads from a file called users.txt, which contains usernames and groups, and then creates the users and groups on the system.&lt;/strong&gt;&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

# Check if running as root
if [[ $UID -ne 0 ]]; then
   echo "This script must be run as root"
   exit 1
fi

# Define the input file, log file, and secure password file
INPUT_FILE="$1"
LOG_FILE="/var/log/user_management.log"
PASSWORD_FILE="/var/secure/user_passwords.csv"

# Check if the input file was provided and exists
if [[ -z "$INPUT_FILE" ]]; then
   echo "No input file provided."
   exit 1
fi
if [[ ! -f "$INPUT_FILE" ]]; then
   echo "File $INPUT_FILE not found."
   exit 1
fi

# Create the log file and password file if they don't exist
touch "$LOG_FILE"
mkdir -p /var/secure
touch "$PASSWORD_FILE"

# Function to generate a random password
generate_password() {
  tr -dc A-Za-z0-9 &amp;lt;/dev/urandom | head -c 12
}

# Function to log messages
log_message() {
  echo "$1" | tee -a "$LOG_FILE"
}

log_message "Backing up created files"
# Backup existing files
cp "$PASSWORD_FILE" "${PASSWORD_FILE}.bak"
cp "$LOG_FILE" "${LOG_FILE}.bak"

# Set permissions for password file
chmod 600 "$PASSWORD_FILE"

# Read the input file line by line
while IFS=';' read -r username groups || [[ -n "$username" ]]; do
   # Ignore whitespace
  username=$(echo "$username" | sed 's/ //g')
  groups=$(echo "$groups" | sed 's/ //g')

  # Parse the username and groups
  echo "$username"
  echo "$groups"

  # Create the user and their personal groups if they don't exist
  if id "$username" &amp;amp;&amp;gt;/dev/null; then
      log_message "User $username already exists. Skipping..."
  else
      # Create personal groups for the user
      groupadd "$username"
      # Create user with their personal groups
      useradd -m -s /bin/bash -g "$username" "$username"
      if [ $? -eq 0 ]; then
          log_message "User $username created with home directory."
      else
          log_message "Failed to create user $username."
          continue
      fi
      # Generate a random password and set it for the user
      PASSWORD=$(generate_password)
      echo "$username,$PASSWORD"
      if [ $? -eq 0 ]; then
          log_message "Password for user $username set."
      else
          log_message "Failed to set password for user $username."
      fi
      # Store the password securely
      echo "$username,$PASSWORD" &amp;gt;&amp;gt; "$PASSWORD_FILE"
      # Set the correct permissions for the home directory
      chmod 700 /home/"$username"
      chown "$username":"$username" /home/"$username"
      log_message "Home directory permissions set for user $username."
  fi

  # Add user to additional groups
  if [ -n "$groups" ]; then
      IFS=',' read -r -a groups_ARRAY &amp;lt;&amp;lt;&amp;lt; "$groups"
      for groups in "${groups_ARRAY[@]}"; do
          # Create groups if it doesn't exist
          if ! getent group "$groups" &amp;gt; /dev/null 2&amp;gt;&amp;amp;1; then
              groupadd "$groups"
              log_message "group $groups created."
          fi
          # Add user to the groups
          usermod -a -G "$groups" "$username"
          if [ $? -eq 0 ]; then
              log_message "User $username added to groups $groups."
          else
              log_message "Failed to add user $username to groups $groups."
          fi
      done
  fi
done &amp;lt; "$INPUT_FILE"
log_message "User creation process completed."

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Breakdown of the script
&lt;/h2&gt;

&lt;p&gt;Check if Running as Root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [[ $UID -ne 0 ]]; then
   echo "This script must be run as root"
   exit 1
fi

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

&lt;/div&gt;



&lt;p&gt;Define Input, 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;INPUT_FILE="$1"
LOG_FILE="/var/log/user_management.log"
PASSWORD_FILE="/var/secure/user_passwords.csv"

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

&lt;/div&gt;



&lt;p&gt;Check if Input File Exists:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [[ -z "$INPUT_FILE" ]]; then
   echo "No input file provided."
   exit 1
fi
if [[ ! -f "$INPUT_FILE" ]]; then
   echo "File $INPUT_FILE not found."
   exit 1
fi

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

&lt;/div&gt;



&lt;p&gt;Create 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;touch "$LOG_FILE"
mkdir -p /var/secure
touch "$PASSWORD_FILE"

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

&lt;/div&gt;



&lt;p&gt;Generate Random Password and log message functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;generate_password() {
  tr -dc A-Za-z0-9 &amp;lt;/dev/urandom | head -c 12
}

log_message() {
  echo "$1" | tee -a "$LOG_FILE"
}

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

&lt;/div&gt;



&lt;p&gt;Backup Existing Files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
log_message "Backing up created files"
cp "$PASSWORD_FILE" "${PASSWORD_FILE}.bak"
cp "$LOG_FILE" "${LOG_FILE}.bak"

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

&lt;/div&gt;



&lt;p&gt;Set Permissions for Password File:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chmod 600 "$PASSWORD_FILE"

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

&lt;/div&gt;



&lt;p&gt;Read Input File and Process Each Line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while IFS=';' read -r username groups || [[ -n "$username" ]]; do
   username=$(echo "$username" | sed 's/ //g')
   groups=$(echo "$groups" | sed 's/ //g')

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

&lt;/div&gt;



&lt;p&gt;Create User and Groups:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if id "$username" &amp;amp;&amp;gt;/dev/null; then
    log_message "User $username already exists. Skipping..."
else
    groupadd "$username"
    useradd -m -s /bin/bash -g "$username" "$username"
    if [ $? -eq 0 ]; then
        log_message "User $username created with home directory."
    else
        log_message "Failed to create user $username."
        continue
    fi
    PASSWORD=$(generate_password)
    echo "$username,$PASSWORD"
    if [ $? -eq 0 ]; then
        log_message "Password for user $username set."
    else
        log_message "Failed to set password for user $username."
    fi
    echo "$username,$PASSWORD" &amp;gt;&amp;gt; "$PASSWORD_FILE"
    chmod 700 /home/"$username"
    chown "$username":"$username" /home/"$username"
    log_message "Home directory permissions set for user $username."
fi

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

&lt;/div&gt;



&lt;p&gt;Add User to Additional Groups:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [ -n "$groups" ]; then
    IFS=',' read -r -a groups_ARRAY &amp;lt;&amp;lt;&amp;lt; "$groups"
    for groups in "${groups_ARRAY[@]}"; do
        if ! getent group "$groups" &amp;gt; /dev/null 2&amp;gt;&amp;amp;1; then
            groupadd "$groups"
            log_message "group $groups created."
        fi
        usermod -a -G "$groups" "$username"
        if [ $? -eq 0 ]; then
            log_message "User $username added to groups $groups."
        else
            log_message "Failed to add user $username to groups $groups."
        fi
    done
fi

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

&lt;/div&gt;



&lt;p&gt;Complete User Creation Process:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;done &amp;lt; "$INPUT_FILE"
log_message "User creation process completed."

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

&lt;/div&gt;



&lt;p&gt;Example users.txt File&lt;br&gt;
Here is an example of what the users.txt file might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;light; umanager,datadev,devops
tosingh; datadev,devops
peter; umanager
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Running the Script&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Save the script to a file, e.g., create_users.sh.&lt;/li&gt;
&lt;li&gt;Ensure the script is executable&lt;/li&gt;
&lt;li&gt;Run the script with the input file as an argument
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chmod +x create_users.sh
sudo ./create_users.sh users.txt

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

&lt;/div&gt;



&lt;p&gt;After running , the password and log location should contain information needed.&lt;/p&gt;

&lt;p&gt;you can learn more about this and so much more by registering on HNG&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://hng.tech/internship"&gt;https://hng.tech/internship&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://hng.tech/premium"&gt;https://hng.tech/premium&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

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