<?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: Udealor Ngozika</title>
    <description>The latest articles on DEV Community by Udealor Ngozika (@udealor_ngozika_d9ec8be50).</description>
    <link>https://dev.to/udealor_ngozika_d9ec8be50</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%2F1608838%2Fe2eedc84-bca3-438d-b69f-e1ac1077be27.png</url>
      <title>DEV Community: Udealor Ngozika</title>
      <link>https://dev.to/udealor_ngozika_d9ec8be50</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/udealor_ngozika_d9ec8be50"/>
    <language>en</language>
    <item>
      <title>Creating Users and Groups with Bash Script: A Comprehensive Guide</title>
      <dc:creator>Udealor Ngozika</dc:creator>
      <pubDate>Sat, 06 Jul 2024 01:26:38 +0000</pubDate>
      <link>https://dev.to/udealor_ngozika_d9ec8be50/creating-users-and-groups-with-bash-script-a-comprehensive-guide-1n8e</link>
      <guid>https://dev.to/udealor_ngozika_d9ec8be50/creating-users-and-groups-with-bash-script-a-comprehensive-guide-1n8e</guid>
      <description>&lt;p&gt;The ng_users.sh Script&lt;br&gt;
Below is the detailed explanation of each section in the ng_users.sh script:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h1&gt;
  
  
  !/bin/bash
&lt;/h1&gt;

&lt;h1&gt;
  
  
  Log file and secure passwords file
&lt;/h1&gt;

&lt;p&gt;LOGFILE="/var/log/user_management.log"&lt;br&gt;
PASSWORD_FILE="/var/secure/user_passwords.txt"&lt;/p&gt;

&lt;h1&gt;
  
  
  Ensure the secure passwords file exists and set the correct permissions
&lt;/h1&gt;

&lt;p&gt;sudo mkdir -p /var/secure&lt;br&gt;
sudo touch $PASSWORD_FILE&lt;br&gt;
sudo chmod 600 $PASSWORD_FILE&lt;/p&gt;

&lt;h1&gt;
  
  
  Function to generate a random password
&lt;/h1&gt;

&lt;p&gt;generate_password() {&lt;br&gt;
    openssl rand -base64 12&lt;br&gt;
}&lt;/p&gt;

&lt;h1&gt;
  
  
  Check if openssl is installed
&lt;/h1&gt;

&lt;p&gt;if ! command -v openssl &amp;amp;&amp;gt; /dev/null; then&lt;br&gt;
    echo "openssl is required but not installed. Please install it and try again." &amp;gt;&amp;amp;2&lt;br&gt;
    exit 1&lt;br&gt;
fi&lt;/p&gt;

&lt;h1&gt;
  
  
  Read the input file line by line
&lt;/h1&gt;

&lt;p&gt;while IFS=';' read -r username groups; do&lt;br&gt;
    # Remove any leading or trailing whitespace&lt;br&gt;
    username=$(echo "$username" | xargs)&lt;br&gt;
    groups=$(echo "$groups" | xargs)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Create a personal group with the same name as the username
if ! getent group "$username" &amp;gt; /dev/null 2&amp;gt;&amp;amp;1; then
    if sudo groupadd "$username"; then
        echo "$(date '+%Y-%m-%d %H:%M:%S') - Group '$username' created." &amp;gt;&amp;gt; "$LOGFILE"
    else
        echo "$(date '+%Y-%m-%d %H:%M:%S') - Error creating group '$username'." &amp;gt;&amp;gt; "$LOGFILE"
        continue
    fi
else
    echo "$(date '+%Y-%m-%d %H:%M:%S') - Group '$username' already exists." &amp;gt;&amp;gt; "$LOGFILE"
fi

# Create the user if it does not exist
if ! id -u "$username" &amp;gt; /dev/null 2&amp;gt;&amp;amp;1; then
    if sudo useradd -m -s /bin/bash -g "$username" "$username"; then
        echo "$(date '+%Y-%m-%d %H:%M:%S') - User '$username' created." &amp;gt;&amp;gt; "$LOGFILE"

        # Generate a random password for the user
        password=$(generate_password)
        echo "$username:$password" | sudo chpasswd
        echo "$username:$password" | sudo tee -a "$PASSWORD_FILE" &amp;gt; /dev/null

        # Set ownership and permissions for the user's home directory
        sudo chown "$username":"$username" "/home/$username"
        sudo chmod 700 "/home/$username"

        echo "$(date '+%Y-%m-%d %H:%M:%S') - Password for '$username' set and stored securely." &amp;gt;&amp;gt; "$LOGFILE"
    else
        echo "$(date '+%Y-%m-%d %H:%M:%S') - Error creating user '$username'." &amp;gt;&amp;gt; "$LOGFILE"
        continue
    fi
else
    echo "$(date '+%Y-%m-%d %H:%M:%S') - User '$username' already exists." &amp;gt;&amp;gt; "$LOGFILE"
fi

# Add user to additional groups
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;gt; /dev/null 2&amp;gt;&amp;amp;1; then
        if sudo groupadd "$group"; then
            echo "$(date '+%Y-%m-%d %H:%M:%S') - Group '$group' created." &amp;gt;&amp;gt; "$LOGFILE"
        else
            echo "$(date '+%Y-%m-%d %H:%M:%S') - Error creating group '$group'." &amp;gt;&amp;gt; "$LOGFILE"
            continue
        fi
    fi
    if sudo usermod -aG "$group" "$username"; then
        echo "$(date '+%Y-%m-%d %H:%M:%S') - User '$username' added to group '$group'." &amp;gt;&amp;gt; "$LOGFILE"
    else
        echo "$(date '+%Y-%m-%d %H:%M:%S') - Error adding user '$username' to group '$group'." &amp;gt;&amp;gt; "$LOGFILE"
    fi
done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

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

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Initializing Variables&lt;br&gt;
We define the log file path (LOGFILE) and the secure passwords file path (PASSWORD_FILE). These files will store logs and securely store passwords, respectively.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Generating Random Passwords&lt;br&gt;
We create a function called generate_password() that uses openssl to generate a random 12-character password. This function will be used later to set passwords for users.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Checking Dependencies&lt;br&gt;
We check if openssl is installed. If not, we exit the script with an error message.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reading Input File&lt;br&gt;
We read the input file line by line, splitting each line into username and groups. We remove any leading or trailing whitespace.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Creating Personal Groups&lt;br&gt;
For each user, we create a personal group with the same name as the username (if it doesn’t exist). We log the action in the LOGFILE.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Creating Users&lt;br&gt;
If the user doesn’t exist, we create the user, set a random password, and securely store it. We also set ownership and permissions for the user’s home directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Adding Users to Additional Groups&lt;br&gt;
We read the comma-separated groups and add the user to each group (if the group doesn’t exist). We log these actions as well.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Conclusion&lt;br&gt;
The script ensures that all requirements are met, including logging and secure password storage.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Technical Article&lt;br&gt;
I’ve written a detailed technical article explaining the script step by step. You can find it on the HNG website: Creating Users and Groups with Bash Script. &lt;a href="https://hng.tech/premium" rel="noopener noreferrer"&gt;https://hng.tech/premium&lt;/a&gt; ,&lt;a href="https://hng.tech/hire" rel="noopener noreferrer"&gt;https://hng.tech/hire&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Creating Users and Groups with Bash Script: A Comprehensive Guide</title>
      <dc:creator>Udealor Ngozika</dc:creator>
      <pubDate>Sat, 06 Jul 2024 01:26:38 +0000</pubDate>
      <link>https://dev.to/udealor_ngozika_d9ec8be50/creating-users-and-groups-with-bash-script-a-comprehensive-guide-396g</link>
      <guid>https://dev.to/udealor_ngozika_d9ec8be50/creating-users-and-groups-with-bash-script-a-comprehensive-guide-396g</guid>
      <description>&lt;p&gt;The ng_users.sh Script&lt;br&gt;
Below is the detailed explanation of each section in the ng_users.sh script:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h1&gt;
  
  
  !/bin/bash
&lt;/h1&gt;

&lt;h1&gt;
  
  
  Log file and secure passwords file
&lt;/h1&gt;

&lt;p&gt;LOGFILE="/var/log/user_management.log"&lt;br&gt;
PASSWORD_FILE="/var/secure/user_passwords.txt"&lt;/p&gt;
&lt;h1&gt;
  
  
  Ensure the secure passwords file exists and set the correct permissions
&lt;/h1&gt;

&lt;p&gt;sudo mkdir -p /var/secure&lt;br&gt;
sudo touch $PASSWORD_FILE&lt;br&gt;
sudo chmod 600 $PASSWORD_FILE&lt;/p&gt;
&lt;h1&gt;
  
  
  Function to generate a random password
&lt;/h1&gt;

&lt;p&gt;generate_password() {&lt;br&gt;
    openssl rand -base64 12&lt;br&gt;
}&lt;/p&gt;
&lt;h1&gt;
  
  
  Check if openssl is installed
&lt;/h1&gt;

&lt;p&gt;if ! command -v openssl &amp;amp;&amp;gt; /dev/null; then&lt;br&gt;
    echo "openssl is required but not installed. Please install it and try again." &amp;gt;&amp;amp;2&lt;br&gt;
    exit 1&lt;br&gt;
fi&lt;/p&gt;
&lt;h1&gt;
  
  
  Read the input file line by line
&lt;/h1&gt;

&lt;p&gt;while IFS=';' read -r username groups; do&lt;br&gt;
    # Remove any leading or trailing whitespace&lt;br&gt;
    username=$(echo "$username" | xargs)&lt;br&gt;
    groups=$(echo "$groups" | xargs)&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Create a personal group with the same name as the username
if ! getent group "$username" &amp;gt; /dev/null 2&amp;gt;&amp;amp;1; then
    if sudo groupadd "$username"; then
        echo "$(date '+%Y-%m-%d %H:%M:%S') - Group '$username' created." &amp;gt;&amp;gt; "$LOGFILE"
    else
        echo "$(date '+%Y-%m-%d %H:%M:%S') - Error creating group '$username'." &amp;gt;&amp;gt; "$LOGFILE"
        continue
    fi
else
    echo "$(date '+%Y-%m-%d %H:%M:%S') - Group '$username' already exists." &amp;gt;&amp;gt; "$LOGFILE"
fi

# Create the user if it does not exist
if ! id -u "$username" &amp;gt; /dev/null 2&amp;gt;&amp;amp;1; then
    if sudo useradd -m -s /bin/bash -g "$username" "$username"; then
        echo "$(date '+%Y-%m-%d %H:%M:%S') - User '$username' created." &amp;gt;&amp;gt; "$LOGFILE"

        # Generate a random password for the user
        password=$(generate_password)
        echo "$username:$password" | sudo chpasswd
        echo "$username:$password" | sudo tee -a "$PASSWORD_FILE" &amp;gt; /dev/null

        # Set ownership and permissions for the user's home directory
        sudo chown "$username":"$username" "/home/$username"
        sudo chmod 700 "/home/$username"

        echo "$(date '+%Y-%m-%d %H:%M:%S') - Password for '$username' set and stored securely." &amp;gt;&amp;gt; "$LOGFILE"
    else
        echo "$(date '+%Y-%m-%d %H:%M:%S') - Error creating user '$username'." &amp;gt;&amp;gt; "$LOGFILE"
        continue
    fi
else
    echo "$(date '+%Y-%m-%d %H:%M:%S') - User '$username' already exists." &amp;gt;&amp;gt; "$LOGFILE"
fi

# Add user to additional groups
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;gt; /dev/null 2&amp;gt;&amp;amp;1; then
        if sudo groupadd "$group"; then
            echo "$(date '+%Y-%m-%d %H:%M:%S') - Group '$group' created." &amp;gt;&amp;gt; "$LOGFILE"
        else
            echo "$(date '+%Y-%m-%d %H:%M:%S') - Error creating group '$group'." &amp;gt;&amp;gt; "$LOGFILE"
            continue
        fi
    fi
    if sudo usermod -aG "$group" "$username"; then
        echo "$(date '+%Y-%m-%d %H:%M:%S') - User '$username' added to group '$group'." &amp;gt;&amp;gt; "$LOGFILE"
    else
        echo "$(date '+%Y-%m-%d %H:%M:%S') - Error adding user '$username' to group '$group'." &amp;gt;&amp;gt; "$LOGFILE"
    fi
done
&lt;/code&gt;&lt;/pre&gt;

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

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Initializing Variables&lt;br&gt;
We define the log file path (LOGFILE) and the secure passwords file path (PASSWORD_FILE). These files will store logs and securely store passwords, respectively.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Generating Random Passwords&lt;br&gt;
We create a function called generate_password() that uses openssl to generate a random 12-character password. This function will be used later to set passwords for users.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Checking Dependencies&lt;br&gt;
We check if openssl is installed. If not, we exit the script with an error message.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reading Input File&lt;br&gt;
We read the input file line by line, splitting each line into username and groups. We remove any leading or trailing whitespace.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Creating Personal Groups&lt;br&gt;
For each user, we create a personal group with the same name as the username (if it doesn’t exist). We log the action in the LOGFILE.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Creating Users&lt;br&gt;
If the user doesn’t exist, we create the user, set a random password, and securely store it. We also set ownership and permissions for the user’s home directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Adding Users to Additional Groups&lt;br&gt;
We read the comma-separated groups and add the user to each group (if the group doesn’t exist). We log these actions as well.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Conclusion&lt;br&gt;
The script ensures that all requirements are met, including logging and secure password storage.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Technical Article&lt;br&gt;
I’ve written a detailed technical article explaining the script step by step. You can find it on the HNG website: Creating Users and Groups with Bash Script.&lt;/p&gt;

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