<?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>Forem: Brian Kemboi</title>
    <description>The latest articles on Forem by Brian Kemboi (@brianbravoski).</description>
    <link>https://forem.com/brianbravoski</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%2F212182%2F4b2b8345-321f-41ce-9204-60fe76150f7e.jpg</url>
      <title>Forem: Brian Kemboi</title>
      <link>https://forem.com/brianbravoski</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/brianbravoski"/>
    <language>en</language>
    <item>
      <title>Linux User Administration using Bash Scripts</title>
      <dc:creator>Brian Kemboi</dc:creator>
      <pubDate>Sun, 07 Jul 2024 17:45:00 +0000</pubDate>
      <link>https://forem.com/brianbravoski/linux-user-administration-using-bash-scripts-h9h</link>
      <guid>https://forem.com/brianbravoski/linux-user-administration-using-bash-scripts-h9h</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;As an integral part of DevOps and Sysadmin, creation of users and groups is an important function, thus we ease our work load by creating a bash script that takes in a file containing the users and groups and automatically creates them.&lt;/p&gt;

&lt;p&gt;As a &lt;a href="https://hng.tech/internship" rel="noopener noreferrer"&gt;HNG Intern&lt;/a&gt;, the following article shows the steps that I took to analyze the Stage 1 DevOps task and the required code snippets to accomplish the task.&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;p&gt;This is a step by step guide to accomplish the following tasks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;reads a file which contains a user and group&lt;/li&gt;
&lt;li&gt;Creates the users and groups&lt;/li&gt;
&lt;li&gt;checks whether there is an existing user and skips&lt;/li&gt;
&lt;li&gt;adds a user to the specified group and create a group with the user's name&lt;/li&gt;
&lt;li&gt;Randomly generates passwords for the created users and save them in 

&lt;ul&gt;
&lt;li&gt;/var/log/user_management.log&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;create a log file of all the things that the script performs.

&lt;ul&gt;
&lt;li&gt;/var/secure/user_passwords.csv&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Code
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Run the script with elevated privileges
&lt;/h5&gt;

&lt;p&gt;Since all requirements have been defined. The first section of the script ensures that the script is run as sudo because user and group creation requires sudo.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (("$UID" != 0));
then 
    echo "script requires root priviledge"
    exit 1
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Confirm the existence of the file
&lt;/h5&gt;

&lt;p&gt;The section below checks whether a file is supplied to the script&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [ -z "$1"]; then
    echo "Error: No file was provided"
    exit 1
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Functions
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Read the provided text file
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;read_text_file(){
    local filename="$1"
    while IFS=';' read -r user groups; do
        users+=("$(echo "$user" | xargs)")
        group_list+=("$(echo "$groups" | xargs)")
    done &amp;lt; "$filename"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Create the users and the group
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create_user_and_group(){
    local username="$1"
    if id "$username" &amp;amp;&amp;gt;/dev/null; then
        echo "User $username already exists." | tee -a "$LOG_FILE"
    else
        groupadd "$username"
        useradd -m -g  "$username" -s /bin/bash "$username"
        echo "Created user $username and created group $username." | tee -a "$LOG_FILE"
    fi 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Setting a password for the user
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set_password(){
    local username="$1"
    local password=$(openssl rand -base64 8)
    echo "$username:$password" | chpasswd
    echo "$username:$password" &amp;gt;&amp;gt; "$PASSWORD_FILE"
    echo "password for $username created and stored in $PASSWORD_FILE." | tee -a "$LOG_FILE"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Declare variables
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;We need variables to store all the file paths that are created.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INPUT_FILE="$1"
LOG_FILE="/var/log/user_management.log"
PASSWORD_FILE="/var/secure/user_passwords.txt"
declare -a users
declare -a group_list 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Create the log and password files
&lt;/h5&gt;



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

&lt;/div&gt;



&lt;h5&gt;
  
  
  Execution of the code
&lt;/h5&gt;



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

for ((i = 0; i &amp;lt; ${#users[@]}; i++)); do
  username="${users[i]}"
  user_groups="${group_list[i]}"

  if [[ "$username" == "" ]]; then
    continue  # Skip empty usernames
  fi

  create_user_and_group "$username"
  set_password "$username"
  add_users_groups "$username" "$user_groups"
done

echo "Users created and group assignment completed." | tee -a "$LOG_FILE"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Script execution
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;To run the script pass the text file containing the users and groups to the script as:
&lt;code&gt;sudo bash create_users.sh users.txt&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;This is a small demonstration of how to develop a batch script to automatically create users, generate random passwords and create groups.&lt;/p&gt;

</description>
      <category>bash</category>
      <category>hng11</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
