<?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: Oluwabammydu</title>
    <description>The latest articles on DEV Community by Oluwabammydu (@oluwabammydu).</description>
    <link>https://dev.to/oluwabammydu</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%2F242686%2F94537379-beb5-4a28-a6a7-5d57c054f3dd.jpeg</url>
      <title>DEV Community: Oluwabammydu</title>
      <link>https://dev.to/oluwabammydu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oluwabammydu"/>
    <language>en</language>
    <item>
      <title>Scripting Wizardry: Automating User and Group Creation Like a Pro</title>
      <dc:creator>Oluwabammydu</dc:creator>
      <pubDate>Wed, 03 Jul 2024 15:56:48 +0000</pubDate>
      <link>https://dev.to/oluwabammydu/scripting-wizardry-automating-user-and-group-creation-like-a-pro-41jo</link>
      <guid>https://dev.to/oluwabammydu/scripting-wizardry-automating-user-and-group-creation-like-a-pro-41jo</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;In the world of system administration, managing users and groups is a crucial task. As organizations grow, manually creating and maintaining user accounts and group memberships can become a tedious and error-prone process. Fortunately, Bash scripting provides a powerful solution to automate this process, saving time and ensuring consistency.&lt;/p&gt;

&lt;p&gt;In this blog post, we will go through a bash script that streamlines user and group creation, and password generation. We'll break down the script section by section, explaining its functionality and the rationale behind the design choices.&lt;/p&gt;

&lt;h1&gt;
  
  
  The Script Breakdown
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Shebang and Log/Password Paths
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;#!/bin/bash&lt;/code&gt; indicates that the script, named create_users.sh, should be run using bash.&lt;/p&gt;

&lt;p&gt;The script starts by defining the paths for the log and password files. It then creates the necessary directories and files if they don't exist, setting appropriate permissions to ensure secure access.&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 the log and password file path
LOG_FILE="/var/log/user_management.log"
PASSWORD_FILE="/var/secure/user_passwords.txt"

#Ensure the necessary directories exist and set permissions
sudo mkdir -p /var/log
sudo mkdir -p /var/secure

# Create the log and password files if they do not exist and set permissions
sudo touch $LOG_FILE
sudo chmod 600 $LOG_FILE
sudo touch $PASSWORD_FILE
sudo chmod 600 $PASSWORD_FILE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Input Validation
&lt;/h3&gt;

&lt;p&gt;Before proceeding, the script checks if an input file containing user data is provided as an argument. If no file is provided, it exits with an error message, ensuring proper usage.&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 input file is provided
if [ -z "$1" ]; then
    echo "Error: Please provide a text file containing user data as an argument."
    exit 1
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Processing User Data
&lt;/h3&gt;

&lt;p&gt;The script reads the input file line by line, where each line represents a user entry. It skips empty lines and extracts the usernames and groups from each line using a delimiter (in this case, a semicolon).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Read the input file line by line
while IFS= read -r line; do
    # Skip empty lines
    [ -z "$line" ] &amp;amp;&amp;amp; continue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  User and Group Creation
&lt;/h3&gt;

&lt;p&gt;For each user, the script first checks if the user's personal group exists. If not, it creates the group. Then, it checks if the user already exists. If not, it creates the user account and assigns the personal group as the primary group.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; # Extract username and groups
    IFS=';' read -r username groups &amp;lt;&amp;lt;&amp;lt; "$line"
    username=$(echo $username | xargs) # Trim whitespace
    groups=$(echo $groups | xargs)     # Trim whitespace

    # Create the user's personal group if it doesn't exist
    if ! getent group "$username" &amp;gt; /dev/null; then
        groupadd "$username"
        echo "$(date): Created group $username" &amp;gt;&amp;gt; $LOG_FILE
    fi

    # Create the user if it doesn't exist
    if ! id -u "$username" &amp;gt; /dev/null 2&amp;gt;&amp;amp;1; then
        useradd -m -g "$username" "$username"
        echo "$(date): Created user $username" &amp;gt;&amp;gt; $LOG_FILE
    fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Group Membership Management
&lt;/h3&gt;

&lt;p&gt;The script parses the list of groups for each user, separated by commas. It checks if each group exists and creates it if necessary. Then, it adds the user to the specified groups using the &lt;code&gt;usermod&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Add the user to the specified groups
    IFS=',' read -ra group_array &amp;lt;&amp;lt;&amp;lt; "$groups"
    for group in "${group_array[@]}"; do
        group=$(echo $group | xargs) # Trim whitespace
        if ! getent group "$group" &amp;gt; /dev/null; then
            groupadd "$group"
            echo "$(date): Created group $group" &amp;gt;&amp;gt; $LOG_FILE
        fi
        usermod -aG "$group" "$username"
        echo "$(date): Added $username to group $group" &amp;gt;&amp;gt; $LOG_FILE
    done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Password Generation
&lt;/h3&gt;

&lt;p&gt;For each user, the script generates a random password using the &lt;code&gt;openssl&lt;/code&gt; command. It appends the username and password to the password file and sets the user's password using the &lt;code&gt;chpasswd&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; # Generate a random password
    password=$(openssl rand -base64 12)
    echo "$username,$password" &amp;gt;&amp;gt; $PASSWORD_FILE

    # Set the user's password
    echo "$username:$password" | chpasswd
    echo "$(date): Set password for $username" &amp;gt;&amp;gt; $LOG_FILE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Home Directory Configuration and Logging
&lt;/h3&gt;

&lt;p&gt;Finally, the script sets the appropriate permissions and ownership for the user's home directory, ensuring secure access.&lt;/p&gt;

&lt;p&gt;Throughout the process, the script logs all operations performed, including user and group creation, password setting, and permission changes, along with timestamps. This logging mechanism provides a detailed audit trail and aids in troubleshooting.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; # Set permissions and ownership for the home directory
    chown -R "$username:$username" "/home/$username"
    chmod 700 "/home/$username"
    echo "$(date): Set permissions for /home/$username" &amp;gt;&amp;gt; $LOG_FILE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  How to Run the Script
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Create the users file with:
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;nano users.txt&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Add your users and their groups in this format: user; groups&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bammy; sudo,dev,www-data
john; sudo
doe; dev,www-data
jane; www-data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save and exit the file with ctrl+o, followed by enter to save; then ctrl+x to exit.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make the script and file executable
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Run the script and pass the user file as an argument
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Automating user and group creation with Bash scripts can significantly streamline system administration tasks, reducing manual effort and ensuring consistency. The provided script offers a comprehensive solution for creating users and groups, generating passwords, and configuring home directories. By understanding the script's functionality and following best practices, you can leverage its power while maintaining a secure and efficient user management process.&lt;/p&gt;

&lt;p&gt;This article is Task 2 in the DevOps track of the HNG Internship. To learn more about HNG, visit &lt;a href="https://hng.tech/internship"&gt;https://hng.tech/internship&lt;/a&gt;.&lt;/p&gt;

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