<?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: Powei Erewejoh</title>
    <description>The latest articles on DEV Community by Powei Erewejoh (@powei_erewejoh_1b18d805c2).</description>
    <link>https://dev.to/powei_erewejoh_1b18d805c2</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1711122%2F83d4479d-324d-455f-a4e5-e9d909b24039.jpg</url>
      <title>DEV Community: Powei Erewejoh</title>
      <link>https://dev.to/powei_erewejoh_1b18d805c2</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/powei_erewejoh_1b18d805c2"/>
    <language>en</language>
    <item>
      <title>Automating User Creation and Management with a Bash Script</title>
      <dc:creator>Powei Erewejoh</dc:creator>
      <pubDate>Tue, 02 Jul 2024 13:56:51 +0000</pubDate>
      <link>https://dev.to/powei_erewejoh_1b18d805c2/automating-user-creation-and-management-with-a-bash-script-2ib2</link>
      <guid>https://dev.to/powei_erewejoh_1b18d805c2/automating-user-creation-and-management-with-a-bash-script-2ib2</guid>
      <description>&lt;p&gt;As a SysOps engineer, automating user creation and management is essential for maintaining system efficiency and security, especially when onboarding new developers. This article presents a robust Bash script to read a text file with usernames and group names, create the necessary users and groups, set up home directories, generate random passwords, and log all actions. &lt;/p&gt;

&lt;p&gt;This task was given as part of HNG internship program - more info at &lt;a href="https://hng.tech/internship"&gt;hng internship&lt;/a&gt;&lt;br&gt;
Find and hire elite freelance talent at  &lt;a href="https://hng.tech/hire"&gt;https://hng.tech/hire&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Script Overview
&lt;/h2&gt;

&lt;p&gt;The script, create_users.sh, performs the following tasks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read a text file with user and group information.&lt;/li&gt;
&lt;li&gt;Create users and groups based on the information.&lt;/li&gt;
&lt;li&gt;Set up home directories with appropriate permissions.&lt;/li&gt;
&lt;li&gt;Generate random passwords for each user.&lt;/li&gt;
&lt;li&gt;Log actions to /var/log/user_management.log.&lt;/li&gt;
&lt;li&gt;Store passwords securely in /var/secure/user_passwords.txt.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Script Breakdown
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Setting Up Log and Password Files&lt;/strong&gt;&lt;br&gt;
We start by defining the paths 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;LOG_FILE="/var/log/user_management.log"
PASSWORD_FILE="/var/secure/user_passwords.txt"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;setup_files&lt;/strong&gt; function ensures these files and directories exist with the correct permissions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setup_files() {
  if [ ! -d "/var/log" ]; then
    mkdir -p "/var/log"
  fi

  if [ ! -f "$LOG_FILE" ]; then
    touch "$LOG_FILE"
    chmod 644 "$LOG_FILE"
  fi

  if [ ! -d "/var/secure" ]; then
    mkdir -p "/var/secure"
  fi

  if [ ! -f "$PASSWORD_FILE" ]; then
    touch "$PASSWORD_FILE"
    chmod 600 "$PASSWORD_FILE"
  fi
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Generating Random Passwords&lt;/strong&gt;&lt;br&gt;
The &lt;strong&gt;generate_password&lt;/strong&gt; function uses OpenSSL to generate a random password:&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() {
  local password=$(openssl rand -base64 12)
  echo "$password"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Logging Actions&lt;/strong&gt;&lt;br&gt;
The &lt;strong&gt;log_action&lt;/strong&gt; function logs messages to the log file 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;log_action() {
  local message="$1"
  echo "$(date +'%Y-%m-%d %H:%M:%S') - $message" &amp;gt;&amp;gt; "$LOG_FILE"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Processing the User File&lt;/strong&gt;&lt;br&gt;
The script processes the user file provided as an argument. It reads each line, splits the username and groups, and creates the necessary users 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;USER_FILE="$1"

if [ ! -f "$USER_FILE" ]; then
  echo "User file not found!"
  exit 1
fi

setup_files

while IFS=';' read -r username groups; do
  username=$(echo "$username" | xargs)
  groups=$(echo "$groups" | xargs)

  if [ -z "$username" ]; then
    continue
  fi

  if id "$username" &amp;amp;&amp;gt;/dev/null; then
    log_action "User $username already exists. Skipping creation."
  else
    if ! getent group "$username" &amp;amp;&amp;gt;/dev/null; then
      groupadd "$username"
      log_action "Created group $username."
    fi

    useradd -m -g "$username" "$username"
    log_action "Created user $username with personal group $username."

    password=$(generate_password)
    echo "$username:$password" | chpasswd
    log_action "Set password for user $username."

    echo "$username:$password" &amp;gt;&amp;gt; "$PASSWORD_FILE"
  fi

  if [ -n "$groups" ]; then
    IFS=',' read -ra group_array &amp;lt;&amp;lt;&amp;lt; "$groups"
    for group in "${group_array[@]}"; do
      group=$(echo "$group" | xargs)
      if ! getent group "$group" &amp;amp;&amp;gt;/dev/null; then
        groupadd "$group"
        log_action "Created group $group."
      fi
      usermod -aG "$group" "$username"
      log_action "Added user $username to group $group."
    done
  fi

  chmod 700 "/home/$username"
  chown "$username:$username" "/home/$username"
  log_action "Set permissions for /home/$username."

done &amp;lt; "$USER_FILE"

log_action "User creation and configuration completed."

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

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;Save the Script: Save the script as &lt;strong&gt;create_users.sh&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Make the Script 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 u+x create_users.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Prepare the User File: Create a text file (e.g., users.txt) with the following format:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;light; sudo,dev,www-data
idimma; sudo
mayowa; dev,www-data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Run the Script:
&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;p&gt;&lt;strong&gt;Explanation of the Script&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Setup Files&lt;/strong&gt;: The script first ensures that the log file and secure password file exist and have the appropriate permissions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read and Process User File&lt;/strong&gt;: It reads the user file line by line, processes each username and associated groups, and handles the creation or modification of users and groups.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Handling&lt;/strong&gt;: The script checks if users or groups already exist and skips creation if they do, logging the actions appropriately.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security&lt;/strong&gt;: Random passwords are generated for each user and stored securely. Permissions for home directories and the password file ensure security and privacy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Logging:&lt;/strong&gt; All actions are logged to /var/log/user_management.log with timestamps for easy auditing and troubleshooting.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This script automates the tedious task of user creation and management, ensuring consistency and security across your systems. By logging all actions and securely storing passwords, it provides a reliable way to onboard new developers and manage user accounts efficiently.&lt;/p&gt;

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