<?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: tochukwu odoh</title>
    <description>The latest articles on DEV Community by tochukwu odoh (@maximo20).</description>
    <link>https://dev.to/maximo20</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%2F1725618%2F4b8cf158-e273-4ea0-9810-bcce962e7801.jpeg</url>
      <title>DEV Community: tochukwu odoh</title>
      <link>https://dev.to/maximo20</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/maximo20"/>
    <language>en</language>
    <item>
      <title>Automating User Management with Bash Scripting</title>
      <dc:creator>tochukwu odoh</dc:creator>
      <pubDate>Wed, 03 Jul 2024 19:00:31 +0000</pubDate>
      <link>https://dev.to/maximo20/automating-user-management-with-bash-scripting-34an</link>
      <guid>https://dev.to/maximo20/automating-user-management-with-bash-scripting-34an</guid>
      <description>&lt;p&gt;As a SysOps engineer, managing user accounts efficiently is crucial. In this article, we will discuss how to automate the creation of users and groups using a bash script (create_users.sh).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
The create_users.sh script automates the creation of user accounts based on input from a text file. It handles the setup of home directories, password generation, group management, and logging, making it ideal for scaling user management tasks. This script is a practical example of how to streamline user management processes while ensuring security and compliance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Script Overview&lt;/strong&gt;&lt;br&gt;
Parsing Input&lt;br&gt;
The script reads from a formatted text file where each line specifies a username and associated groups separated by semicolons. This approach allows for easy configuration and bulk user management.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;User and Group Creation&lt;/strong&gt;&lt;br&gt;
The script checks if users and groups already exist, creates them if they don't, and assigns appropriate permissions and ownerships. This ensures that new users are correctly set up and integrated into the system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Password Security&lt;/strong&gt;&lt;br&gt;
Passwords are securely generated using OpenSSL's base64 random generator and stored in /var/secure/user_passwords.txt, accessible only to the script owner. This step is crucial for maintaining the confidentiality of user credentials.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Error Handling&lt;/strong&gt;&lt;br&gt;
The script gracefully handles errors such as existing users or groups and logs these events to /var/log/user_management.log for audit purposes. This ensures transparency and accountability in user management.&lt;/p&gt;

&lt;p&gt;Step-by-Step Implementation&lt;br&gt;
Initialize Variables and Files&lt;br&gt;
The script starts by defining log and password file paths and ensuring they exist with the correct permissions:&lt;/p&gt;

&lt;p&gt;`#!/bin/bash&lt;/p&gt;
&lt;h1&gt;
  
  
  Ensure the secure directory exists
&lt;/h1&gt;

&lt;p&gt;sudo mkdir -p /var/secure&lt;br&gt;
sudo touch /var/secure/user_passwords.txt&lt;/p&gt;

&lt;p&gt;LOG_FILE="/var/log/user_management.log"&lt;br&gt;
PASSWORD_FILE="/var/secure/user_passwords.txt"&lt;/p&gt;
&lt;h1&gt;
  
  
  Ensure log and password files exist
&lt;/h1&gt;

&lt;p&gt;sudo touch "$LOG_FILE"&lt;br&gt;
sudo touch "$PASSWORD_FILE"&lt;/p&gt;
&lt;h1&gt;
  
  
  Set permissions to secure the password file
&lt;/h1&gt;

&lt;p&gt;sudo chmod 600 "$PASSWORD_FILE"&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Processing Each Line&lt;/strong&gt;&lt;br&gt;
The script processes each line of the input file, trimming whitespace and extracting usernames and groups:&lt;/p&gt;

&lt;p&gt;`while IFS=';' read -r username groups || [ -n "$username" ]; do&lt;br&gt;
    username=$(echo "$username" | tr -d '[:space:]')&lt;br&gt;
    groups=$(echo "$groups" | tr -d '[:space:]')&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Debugging line to understand how each line is processed
echo "Processing: username=$username, groups=$groups"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;`&lt;/p&gt;

&lt;p&gt;User and Group Creation&lt;br&gt;
It checks if users and groups already exist and creates them if necessary:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```    if id "$username" &amp;amp;&amp;gt;/dev/null; then&lt;br&gt;
        echo "$(date) - User '$username' already exists. Skipping creation." | sudo tee -a "$LOG_FILE"&lt;br&gt;
        continue&lt;br&gt;
    fi&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if ! getent group "$username" &amp;amp;&amp;gt;/dev/null; then
    echo "$(date) - Creating group '$username'." | sudo tee -a "$LOG_FILE"
    sudo groupadd "$username"
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Assigning Users to Groups
The script assigns users to the specified groups:



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

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IFS=',' read -ra group_array &amp;lt;&amp;lt;&amp;lt; "$groups"

for group in "${group_array[@]}"; do
    if ! getent group "$group" &amp;amp;&amp;gt;/dev/null; then
        echo "$(date) - Creating group '$group'." | sudo tee -a "$LOG_FILE"
        sudo groupadd "$group"
    fi
done

echo "$(date) - Creating user '$username'." | sudo tee -a "$LOG_FILE"
sudo useradd -m -g "$username" -G "$(IFS=','; echo "${group_array[*]}")" "$username"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


Password Generation and Assignment
Passwords are generated and assigned to users, and securely stored:



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

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;password=$(openssl rand -base64 12)
echo "$username:$password" | sudo chpasswd
echo "$username,$password" | sudo tee -a "$PASSWORD_FILE"

echo "$(date) - User '$username' created and assigned to groups: ${group_array[*]}." | sudo tee -a "$LOG_FILE"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;done &amp;lt; "$1"&lt;/p&gt;

&lt;p&gt;echo "$(date) - Script execution completed." | sudo tee -a "$LOG_FILE"&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


Security Considerations
User passwords are generated securely and stored in a protected file to prevent unauthorized access. Proper permissions ensure that sensitive information remains confidential.

Conclusion
Automating user management tasks with create_users.sh improves operational efficiency and reduces human error in user account provisioning. This script exemplifies best practices in DevOps for maintaining a secure and organized user environment.
By implementing create_users.sh, SysOps teams can streamline user management processes while ensuring security and compliance.

For more details about HNG internship, visit [HNG Internship](https://hng.tech/internship) or click to know more https://hng.tech


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

&lt;/div&gt;

</description>
    </item>
  </channel>
</rss>
