<?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: Njoku Ujunwa </title>
    <description>The latest articles on DEV Community by Njoku Ujunwa  (@techynurse).</description>
    <link>https://dev.to/techynurse</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%2F1455756%2F45fb57dd-a8aa-4f53-9944-82633ae9f750.jpg</url>
      <title>DEV Community: Njoku Ujunwa </title>
      <link>https://dev.to/techynurse</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/techynurse"/>
    <language>en</language>
    <item>
      <title>Automating User and Group Management with Bash: A Step-by-Step Guide</title>
      <dc:creator>Njoku Ujunwa </dc:creator>
      <pubDate>Mon, 01 Jul 2024 10:44:14 +0000</pubDate>
      <link>https://dev.to/techynurse/automating-user-and-group-management-with-bash-a-step-by-step-guide-187b</link>
      <guid>https://dev.to/techynurse/automating-user-and-group-management-with-bash-a-step-by-step-guide-187b</guid>
      <description>&lt;p&gt;How to Automate User Creation and Group Assignment in Linux Using Bash.&lt;/p&gt;

&lt;p&gt;Bash is a powerful scripting tool used to automate various tasks on Unix-like operating systems. One common administrative task is managing users and groups. In this article, we will walk you through a Bash script, create_users.sh which automates the process of creating users and groups, setting up home directories, generating passwords, and logging all actions. This script helps simplifies user management, especially when dealing with multiple users.&lt;/p&gt;

&lt;p&gt;Prerequisites&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A basic understanding of Bash scripting and Linux.&lt;/li&gt;
&lt;li&gt;Access to a Linux terminal.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;The &lt;em&gt;create_users.sh&lt;/em&gt; script reads a text file containing usernames and group names, creates the users and groups as specified, sets up home directories with appropriate permissions, generates random passwords for the users, and logs all actions to &lt;code&gt;/var/log/user_management.log&lt;/code&gt;. It also stores the generated passwords securely in &lt;code&gt;/var/secure/user_passwords.txt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Below is my 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
if [ $# -ne 1 ]; then
    echo "Use: $0 &amp;lt;filename&amp;gt;"
    exit 1
fi
FILENAME=$1
mkdir -p /var/secure
PASSFILE="/var/secure/user_passwords.txt"
touch $PASSFILE
chmod 600 $PASSFILE
mkdir -p /var/log
LOGFILE="/var/log/user_management.log"
touch $LOGFILE
chmod 644 /var/log/user_management.log
echo "User management started at $(date)" &amp;gt; $LOGFILE


while IFS=';' read -r username groups; do
    username=$(echo "$username" | xargs)
    groups=$(echo "$groups" | xargs)
    if [ -z "$username" ]; then
        continue
    fi
    if id -u "$username"; then
        echo "User $username already exists" | tee -a $LOGFILE
    else
        useradd -m -s /bin/bash -U "$username"
        echo "User $username created with personal group $username" | tee -a $LOGFILE
    fi
    IFS=',' read -r -a group_array &amp;lt;&amp;lt;&amp;lt; "$groups"
    for group in "${group_array[@]}"; do
        if ! getent group "$group" &amp;gt;/dev/null 2&amp;gt;&amp;amp;1; then
            groupadd "$group"
            echo "Group $group created" | tee -a $LOGFILE
        fi
        usermod -aG "$group" "$username"
        echo "User $username added to group $group" | tee -a $LOGFILE
    done


    password=$(openssl rand -base64 12)
    echo "$username:$password" | chpasswd
    echo "Password set for user $username" | tee -a $LOGFILE
    echo "$username,$password" &amp;gt;&amp;gt; $PASSFILE
done &amp;lt; "$FILENAME"
echo "User management completed at $(date)" | tee -a $LOGFILE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can divide this script into three sections:&lt;/p&gt;

&lt;p&gt;Create directory, files and permission.&lt;br&gt;
Create User, group and assign each user to there group.&lt;br&gt;
Generate Password.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Detailed explanation of the script:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;START BASH&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Shebang line that tells the system to run this script using the Bash shell.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NUM. OF ARGUMENT&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Checks if exactly one argument (the filename) is provided. This is for error handling.&lt;/li&gt;
&lt;li&gt;If the argument is not equal to one, then print Use: &lt;code&gt;create_users.sh &amp;lt;filename&amp;gt;.&lt;/code&gt; to show you the correct way of running the task.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;exit 1&lt;/code&gt;:  This line stops running the script because something is wrong.&lt;/li&gt;
&lt;li&gt;If you run the script with &lt;code&gt;./create_users.sh myfile.txt&lt;/code&gt;, &lt;code&gt;$1&lt;/code&gt; is &lt;code&gt;myfile.txt&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [ $# -ne 1 ]; then
    echo "Use: $0 &amp;lt;filename&amp;gt;"
    exit 1
fi
FILENAME=$1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;CREATE PASSWORD FILE AND PERMISSIONS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a directory &lt;code&gt;/var/secure&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Sets up the path for the password file &lt;code&gt;PASSFILE="/var/secure/user_passwords.txt&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Then create an empty file &lt;code&gt;touch $PASSFILE&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Sets permissions for the password file &lt;code&gt;chmod 600 $PASSFILE&lt;/code&gt; ensuring that only the owner can read and write to the file.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir -p /var/secure
PASSFILE="/var/secure/user_passwords.txt"
touch $PASSFILE
chmod 600 $PASSFILE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;CREATE LOG FILE AND PERMISSIONS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a directory &lt;code&gt;/var/log&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Sets up the path for the password file &lt;code&gt;LOGFILE="/var/log/user_management.LOG&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Then create an empty file &lt;code&gt;touch $LOGFILE&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Give read permission to user and others (Everyone needs to be able to access the log), - - while owner will be given read and write permission. &lt;code&gt;chmod 644 $LOGFILE&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir -p /var/log
LOGFILE="/var/log/user_management.log"
touch $LOGFILE
chmod 644 /var/log/user_management.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;STARTING PROCESS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initializes the &lt;code&gt;$LOGFILE&lt;/code&gt; with a start message, indicating the start time of the user management process.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo "User management started at $(date)" &amp;gt; $LOGFILE

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;READ FILE&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start a loop that reads a line from the &lt;code&gt;$FILENAME&lt;/code&gt;, splits it into two parts based on &lt;code&gt;IFS.&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Assign the first part to username and the remaining parts to groups .&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;xargs&lt;/code&gt; command is use to remove any leading or trailing whitespace from username or groups.
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Checks if &lt;code&gt;$username&lt;/code&gt; is empty and skip to the next line in the file. This is part of error handling.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [ $# -ne 1 ]; then
        continue
    fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;USER&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check if the user already exist.&lt;/li&gt;
&lt;li&gt;If the user exists, it logs that the user already exists.&lt;/li&gt;
&lt;li&gt;If the user does not exist, it creates the user, with the personal group and log it.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if id -u "$username"; then
        echo "User $username already exists" | tee -a $LOGFILE
    else
        useradd -m -s /bin/bash -U "$username"
        echo "User $username created with personal group $username" | tee -a $LOGFILE
    fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;GROUPS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Splits the groups string by , into an array called group_array.&lt;/li&gt;
&lt;li&gt;Check if the group in the array already exist in the database.&lt;/li&gt;
&lt;li&gt;If it does not, create a &lt;code&gt;$group&lt;/code&gt; and log.&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;$username&lt;/code&gt; to the group without removing them from other groups and log.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for group in "${group_array[@]}"; do
        if ! getent group "$group" &amp;gt;/dev/null 2&amp;gt;&amp;amp;1; then
            groupadd "$group"
            echo "Group $group created" | tee -a $LOGFILE
        fi
        usermod -aG "$group" "$username"
        echo "User $username added to group $group" | tee -a $LOGFILE
    done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;PASSWORDS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generate random password for&lt;/li&gt;
&lt;li&gt;Sets the password for &lt;code&gt;$username&lt;/code&gt; to the value stored in $password&lt;/li&gt;
&lt;li&gt;Print message to show password has been set.&lt;/li&gt;
&lt;li&gt;Then store the user and password to the &lt;code&gt;$PASSFILE&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Ends the loop after processing all lines in the file
&lt;/li&gt;
&lt;/ul&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" | chpasswd
    echo "Password set for user $username" | tee -a $LOGFILE
    echo "$username,$password" &amp;gt;&amp;gt; $PASSFILE
done &amp;lt; "$FILENAME"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;END PROCESS&lt;/strong&gt;&lt;br&gt;
'''&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Logs a message indicating the completion of user management, appending it to &lt;code&gt;$LOGFILE&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo "User management completed at $(date)" | tee -a $LOGFILE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Links to HNG Internship&lt;/strong&gt;&lt;br&gt;
The HNG Internship is a great opportunity to learn and grow in the field of technology. For more information, visit:&lt;br&gt;
&lt;a href="https://hng.tech/internship"&gt;HNG Internship&lt;/a&gt;&lt;br&gt;
&lt;a href="https://hng.tech/premium"&gt;HNG Premium&lt;/a&gt;&lt;/p&gt;




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

&lt;p&gt;Let's try running the scripts to see if it works.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a file name &lt;code&gt;test_users.txt&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim test_users.txt

#file content
john;developers,testers
jane;developers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Execute the script &lt;code&gt;create_users.sh&lt;/code&gt; with the file &lt;code&gt;test_users.txt&lt;/code&gt;
&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
sudo ./create_users.sh test_users.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Check the &lt;code&gt;$LOGFILE&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat /var/log/user_management.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Switch to root user and check the &lt;code&gt;$PASSFILE&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su
cat /var/secure/user_passwords.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyqxz0k3g8r079s3d729r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyqxz0k3g8r079s3d729r.png" alt="Image description" width="800" height="589"&gt;&lt;/a&gt;&lt;/p&gt;

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