<?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: Ian alex</title>
    <description>The latest articles on DEV Community by Ian alex (@ik_again).</description>
    <link>https://dev.to/ik_again</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%2F758309%2Fc9c69cf6-5597-4947-9e34-ecd68a5f2cdd.jpg</url>
      <title>DEV Community: Ian alex</title>
      <link>https://dev.to/ik_again</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ik_again"/>
    <language>en</language>
    <item>
      <title>My First Task In HNG Internship</title>
      <dc:creator>Ian alex</dc:creator>
      <pubDate>Wed, 03 Jul 2024 00:40:39 +0000</pubDate>
      <link>https://dev.to/ik_again/my-first-task-in-hng-internship-12f6</link>
      <guid>https://dev.to/ik_again/my-first-task-in-hng-internship-12f6</guid>
      <description>&lt;p&gt;I signup for an internship program named HNG. It is expected that the intern should have an intermediate to advance experience for any track they wish to participate in. For more information regarding the internship, your can follow this link &lt;a href="https://hng.tech/internship"&gt;https://hng.tech/internship&lt;/a&gt; and applying for a job at HNG you can also checkout this link &lt;a href="https://hng.tech/hire"&gt;https://hng.tech/hire&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Task 1: We were tasked to write a script named create_user.sh for creating a user and adding the user to a group via reading from an input file.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
# Log file and password file
PASSWORD_FILE="/var/secure/user_passwords.txt"
LOG_FILE="/var/log/user_management.log"
# ensure to check if the number of argument provided is 1
# if !true exit running the entire codebase
if [ $# -ne 1 ]; then
    echo "Usage: $0 &amp;lt;input_textfile&amp;gt;" | sudo tee -a $LOG_FILE
    exit 1
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Considering the above code block;&lt;br&gt;
&lt;code&gt;#!/bin/bash&lt;/code&gt; the shebang declaration specifying that this file is a bash script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PASSWORD_FILE="/var/secure/user_passwords.txt"
LOG_FILE="/var/log/user_management.log"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the above block of code assigns the path &lt;code&gt;/var/secure/user_passwords.txt&lt;/code&gt; to variable &lt;code&gt;PASSWORD_FILE&lt;/code&gt; and path &lt;code&gt;/var/log/user_management.log&lt;/code&gt; to variable &lt;code&gt;LOG_FILE&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [ $# -ne 1 ]; then
    echo "This is how to run the script: $0 &amp;lt;input_textfile&amp;gt;" | sudo tee -a $LOG_FILE
    exit 1
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above block of code checks if only argument is passed to the script.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$# -ne 1&lt;/code&gt; checks if the number of argument passed is not equal to one and prints the output to the terminal and also log the data.&lt;/li&gt;
&lt;li&gt;else if the condition doesn't hold true it exits the block of the code.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [ ! -f "$input_textfile" ]; then
    echo "Error: The file $input_textfile does not exists" | sudo tee -a $LOG_FILE
    exit 1
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;! -f "$input_textfile&lt;/code&gt; this checks if an input file is not passed to the script it exits
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo chown root:root $PASSWORD_FILE
sudo mkdir -p /var/secure
sudo touch $PASSWORD_FILE
sudo chmod 600 $PASSWORD_FILE
sudo touch $LOG_FILE
sudo chmod 640 $LOG_FILE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This Create necessary directories such as $LOG_FILE $PASSWORD_FILE and set permissions such as making the $PASSWORD_FILE have root administrative privilege and setting the permission to read and write privilege. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sudo chmod 640 $LOG_FILE&lt;/code&gt; this ensure that the user has a read and write privilege and the group has only read privilege.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;generate_password() {
    &amp;lt; /dev/urandom tr -dc 'A-Za-z0-9!@#$%&amp;amp;*' | head -c 12
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This function is responsible for generating random password&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Read File&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while IFS=';' read -r user groups; do
    if [ -z "$user" ] || [ -z "$groups" ]; then
        echo "Skipping invalid line: $user;$groups" | sudo tee -a $LOG_FILE
        continue
    fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Start a loop that reads a line from the $FILENAME, splits it into two parts separated by &lt;code&gt;;&lt;/code&gt; based on IFS.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;read -r user groups&lt;/code&gt; Assign the first part to username and the remaining parts to groups.&lt;/li&gt;
&lt;li&gt;-z "$user" -z "$groups" checks to see if the user and group name is empty.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Creating users&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if id -u "$user" &amp;gt;/dev/null 2&amp;gt;&amp;amp;1; then
        echo "This particular User $user exists" | sudo tee -a $LOG_FILE
    else
        sudo useradd -m "$user"
        if [ $? -eq 0 ]; then
            echo "User $user created" | sudo tee -a $LOG_FILE

            # Generating the random password for each user 
            password=$(generate_password)
            echo "$user,$password" | sudo tee -a $PASSWORD_FILE &amp;gt;/dev/null
            echo "$user:$password" | sudo chpasswd
            echo "User $user password is set" | sudo tee -a $LOG_FILE

            # Set appropriate permissions for the home directory
            sudo chmod 700 /home/$user
            sudo chown $user:$user /home/$user
            echo "Home directory for user $user set up with appropriate permissions" | sudo tee -a $LOG_FILE
        else
            echo "Failed to create user $user" | sudo tee -a $LOG_FILE
            continue
        fi
    fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;id -u "$user" &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;/code&gt; this looks for the user id and suppress the standard output and error to &lt;code&gt;/dev/null&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;sudo useradd -m "$user"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useradd&lt;/code&gt;: This is the command used to add a new user&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-m&lt;/code&gt;: This option tells useradd to create a home directory for the new user if it does not already exist. The home directory will be created in the /home/ directory and named after the user.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"$user"&lt;/code&gt;: This is the username of the new user being created. The $user variable should contain the name of the user&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[ $? -eq 0 ]&lt;/code&gt; this checks if the previous command successfully executed and 0 indicates success.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;password=$(generate_password)&lt;/code&gt; calls the generate_password function and assigns the result to the password variable.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;echo "$user,$password" | sudo tee -a $PASSWORD_FILE &amp;gt;/dev/null&lt;/code&gt; this suppresses the output due to the /dev/null&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;echo "$user:$password" | sudo chpasswd&lt;/code&gt; this allows the user to change password.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;echo "User $user password is set" | sudo tee -a $LOG_FILE&lt;/code&gt; displays the output to the terminal.
&lt;code&gt;sudo chmod 700 /home/$user&lt;/code&gt; gives the user a full privileged.
&lt;code&gt;sudo chown $user:$user /home/$user&lt;/code&gt; gives the owner of the directory to the user.
&lt;code&gt;else&lt;/code&gt; if the condition doesn't hold true it print the output of failed user creation to the terminal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Adding Users to Group&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
            sudo usermod -aG "$group" "$user"
            echo "User $user added to existing group $group" | sudo tee -a $LOG_FILE
        else
            sudo groupadd "$group"
            sudo usermod -aG "$group" "$user"
            echo "Group $group created and user $user added to it" | sudo tee -a $LOG_FILE
        fi
    done
done &amp;lt; "$input_textfile"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-&lt;code&gt;IFS=',' read -r -a group_array &amp;lt;&amp;lt;&amp;lt; "$groups"&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;IFS=','&lt;/code&gt;: Sets the Internal Field Separator to a comma. This means the read command will split the input string based on commas.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;read -r -a group_array &amp;lt;&amp;lt;&amp;lt; "$groups"&lt;/code&gt; Reads the group variable, splits it by comma and stores the value to the group_array.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;group=$(echo "$group" | xargs)&lt;/code&gt; this removes any leading whitespace in the group.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;for group in "${group_array[@]}"&lt;/code&gt; this loops through the group_array array and stores each iteration to group.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;if getent group "$group" &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;/code&gt; if the group exists in the system; also suppress the standard output and error.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sudo usermod -aG "$group" "$user"&lt;/code&gt; adds users to the existing group&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sudo groupadd "$group"&lt;/code&gt; this creates a new group.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sudo usermod -aG "$group" "$user"&lt;/code&gt; this adds user to the group&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;echo "Group $group created and user $user added to it" | sudo tee -a $LOG_FILE&lt;/code&gt; prints the output and log it into the log file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;done&lt;/code&gt; ends the for loop&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;done &amp;lt; "$input_textfile"&lt;/code&gt; ends the while loop that reads from the input file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;echo "User creation and group assignment created." | sudo tee -a $LOG_FILE&lt;/code&gt; outputting the finished the creation of users and group.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Running The Script&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;created the file named called &lt;code&gt;name-of-text-file.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;nano name-of-text-file.txt

#file content of the file
kachi; security, crypto, signals
dika; werey, genuis, smartkid
diamond; werey, soc, faith
chimummy; boss, theboss, smartguy
david; psycho, funny, jovial
faith; babe, babygirl, fine
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;execute the script &lt;code&gt;create_userss.sh&lt;/code&gt; with the text file &lt;code&gt;name-of-text-file.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;# making the script file to be executable
chmod +x create_userss.sh
# running the script
./create_userss.sh name-of-text-file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;checking the LOG_FILE
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The display output for the log file&lt;/li&gt;
&lt;/ul&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%2Fct92terz4sznyh4v6qlh.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%2Fct92terz4sznyh4v6qlh.png" alt="Image description" width="437" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;checking the password file PASSWORD_FILE
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This displayed output for the password file&lt;/li&gt;
&lt;/ul&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%2Fj9q9nldvvx57ro7x6i1a.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%2Fj9q9nldvvx57ro7x6i1a.png" alt="Image description" width="170" height="145"&gt;&lt;/a&gt;&lt;/p&gt;

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