<?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: Charles Gobina</title>
    <description>The latest articles on DEV Community by Charles Gobina (@charlesgobina).</description>
    <link>https://dev.to/charlesgobina</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%2F1708596%2F20e01eca-3b79-469b-bf4d-16449bce5021.jpg</url>
      <title>DEV Community: Charles Gobina</title>
      <link>https://dev.to/charlesgobina</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/charlesgobina"/>
    <language>en</language>
    <item>
      <title>Automating Linux user and group creation using a bash script.</title>
      <dc:creator>Charles Gobina</dc:creator>
      <pubDate>Sun, 07 Jul 2024 19:43:19 +0000</pubDate>
      <link>https://dev.to/charlesgobina/automating-linux-user-and-group-creation-using-a-bash-script-2jmj</link>
      <guid>https://dev.to/charlesgobina/automating-linux-user-and-group-creation-using-a-bash-script-2jmj</guid>
      <description>&lt;p&gt;Sysadmins often find themselves caught in an endless loop of user management. With each new hire, they're tasked with the tedious chore of setting up yet another account. The process can quickly become monotonous, so I devised a bash script to automate the user creation process by simply providing a file with the user data as an argument to the script.&lt;/p&gt;

&lt;p&gt;Let's begin by creating the name of the script and making it executable&lt;br&gt;
&lt;code&gt;$ touch create_user.sh &amp;amp;&amp;amp; chmod u+x create_user.sh&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;With that out of the way, we can proceed to writing the script. Remember! Each bash script begins with &lt;code&gt;#!/bin/bash&lt;/code&gt; which should be at the top of the file (the first line). It tells our shell to use bash as the interpreter for this script.&lt;/p&gt;
&lt;h3&gt;
  
  
  ⚠️ Note
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;We assume that the content format of the file passed as an argument looks like the example below.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user1; group1,group2,group3
user2; group1
user3; group3,group2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Variable Declaration
&lt;/h3&gt;

&lt;p&gt;Let's go on to define some variables&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;logfile="/var/log/user_management.log"
passwordfile="/var/secure/user_passwords.csv"
filename=$1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;logfile&lt;/code&gt; holds the path to the file where we'll be storing our logs (No logs no SysAdmin 😅&lt;br&gt;
&lt;code&gt;passwordfile&lt;/code&gt; holds the path to the file where we'll be storing the user-password pair&lt;br&gt;
&lt;code&gt;filename&lt;/code&gt; holds the content of the first argument passed to our script at the level of the terminal. In our case, it is going to be a file containing different users and their respective groups. We'll be reading the contents of this file to create the different users&lt;/p&gt;
&lt;h3&gt;
  
  
  Verify that our argument is passed to the script
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [ $# -eq 0 ]; then
    echo "Please provide a filename as an argument $(date)" | tee -a $logfile
    exit 1
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The code snippet above checks if the number of command-line arguments passed to the script is equal to 0. The &lt;code&gt;$#&lt;/code&gt; variable represents the number of command-line arguments. If it is equal to 0, it means no arguments where passed, and the script exits.&lt;/p&gt;

&lt;p&gt;Once an argument (the file) is passed to our script at the level of the terminal, the script then proceeds to do the following;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reads the file line by line.&lt;/li&gt;
&lt;li&gt;Extracts the users and groups.&lt;/li&gt;
&lt;li&gt;Generates a random hashed password and creates the user.&lt;/li&gt;
&lt;li&gt;Modify the created user to include their respective groups.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I. &lt;strong&gt;Read file line by line&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 line; do
    ...
done &amp;lt; "$filename"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The code snippet reads the next line from the input file and assigns it to the &lt;code&gt;line&lt;/code&gt; variable.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;IFS=&lt;/code&gt; sets the Internal Field Separator (IFS) to nothing, which ensures leading and trailing whitespace characters are preserved in each line.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;-r&lt;/code&gt; flag ensures every &lt;code&gt;\&lt;/code&gt; in the input file is treated literally.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;II. &lt;strong&gt;Extracts the users and groups&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;user=$(echo "$line" | cut -d';' -f1)
groups=$(echo "$line" | cut -d';' -f2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The code snippet above uses the &lt;code&gt;cut&lt;/code&gt; command and the &lt;code&gt;-d&lt;/code&gt; flag to set a delimiter for appropriately extracting a user and a group. The extracted user is stored in the &lt;strong&gt;user&lt;/strong&gt; variable while the group(s) are store in the &lt;strong&gt;group&lt;/strong&gt; variable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;III. &lt;strong&gt;Generate a random hashed password and create the user&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 "$user" &amp;amp;&amp;gt;/dev/null; then
        echo "The user $user already exists $(date)" | tee -a $logfile
    else
        # Create user
        password=$(openssl rand -base64 12)
        sudo useradd -m "$user" -p $password
        # echo "$user:$password" | sudo chpasswd
        echo "$user:$password" &amp;gt;&amp;gt; $passwordfile
        # user has been created successfully
        echo "User $user has been created successfully $(date)" | tee -a $logfile

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This code snippet begins by checking if the user we are about to create already exists. If the user exists, we'll get the error &lt;em&gt;The user [user] already exists&lt;/em&gt; together with the date today (for better debugging in case  something happens). This error is also written in our log file thanks to &lt;code&gt;tee -a $logfile&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the user does not exist, the script generates a random fixed hashed password for that user using &lt;code&gt;openssl rand -base64 12&lt;/code&gt; then stores it in the password variable.&lt;/li&gt;
&lt;li&gt;The password variable is then used in assigning that password to the user once that user is created using the &lt;code&gt;-p&lt;/code&gt; flag in the &lt;code&gt;useradd&lt;/code&gt; command&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;sudo useradd -m "$user" -p $password&lt;/code&gt; creates a user using the &lt;code&gt;user&lt;/code&gt; variable that was earlier extracted in step II. The &lt;code&gt;-m&lt;/code&gt; flag created a &lt;em&gt;home directory&lt;/em&gt; for that user in &lt;code&gt;/home&lt;/code&gt; and at the same time, that user is being assigned to a group with the same name as the user's name. &lt;/li&gt;
&lt;li&gt;Once the operation is successful, the action is logged into our log file using &lt;code&gt;echo "User $user has been created successfully $(date)" | tee -a $logfile&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;IV. &lt;strong&gt;Modify the created user to include their respective groups&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;# split the groups separated by comma and add them to an array
        IFS=',' read -r -a group_array &amp;lt;&amp;lt;&amp;lt; "$groups"

        for group in "${group_array[@]}"; do
            # remove leading and trailing whitespaces
            group=$(echo "$group" | xargs)
            # check if the group exists before adding the user to it
            if ! getent group "$group" &amp;amp;&amp;gt;/dev/null; then
                echo "Creating group $group $(date)" | tee -a $logfile
                sudo groupadd "$group"
            fi
            echo "Modifying user $user to add them to group $group $(date)" | tee -a $logfile
            sudo usermod -a -G "$group" "$user"
            echo "User modifcation complete, successfully added to group $group $(date)" | tee -a $logfile
        done

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This is the last step, where we modify the created user to include all the groups associated with that user. From the &lt;strong&gt;note&lt;/strong&gt; above, we can see that some users belong to more than one group, so we have to make sure that is enforced.&lt;/li&gt;
&lt;li&gt;The snippet begins by turning the &lt;code&gt;group&lt;/code&gt; into an array for easy manipulation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;IFS=','&lt;/code&gt; sets the Internal Field Separator (IFS) to a comma. This tells the read command to split the input string into elements based on the comma separator.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;read&lt;/code&gt; reads input into variables.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;r&lt;/code&gt; prevents backslashes from being interpreted as escape characters. This ensures that backslashes are treated as literal characters.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;a group_array&lt;/code&gt; reads the input into an array named group_array.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;&amp;lt;&amp;lt; "$groups"&lt;/code&gt; is a here-string that provides the value of the variable $groups as input to the read command. $groups is expected to be a string containing group names separated by commas.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;for group in "${group_array[@]}"; do:&lt;/code&gt;is a loop that iterates over each element in the &lt;code&gt;group_array&lt;/code&gt; array.&lt;code&gt;${group_array[@]}&lt;/code&gt; expands to all elements in the group_array, allowing the for loop to iterate over each group name.&lt;/li&gt;
&lt;li&gt;The individual groups in the array, are then created using the &lt;code&gt;sudo groupadd "$group"&lt;/code&gt; command. Of course we don't want to create a group that exists already, so &lt;code&gt;getent group "$group"&lt;/code&gt; checks if the group we are currently looping through exists already. If the group does not exist, it is created, and again the action is logged into our log file&lt;/li&gt;
&lt;li&gt;After creating the different groups, now comes the time to assign these groups to their respective users, so we use the &lt;code&gt;sudo usermod -a -G "$group" "$user"&lt;/code&gt; command to perform the modification.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;-a&lt;/code&gt; flag stands for append and it is used to add the user to the supplementary group(s) specified by the &lt;code&gt;-G&lt;/code&gt; option without removing them from other groups they may already be members of.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;-G&lt;/code&gt; flag accepts the group or groups we want to add, in our case the group(s) are stored in the &lt;code&gt;$group&lt;/code&gt; variable.&lt;/li&gt;
&lt;li&gt;After this operation is performed, the action is logged in our log file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can find the entire source code to this script &lt;a href="https://github.com/charlesgobina/linux-user-creation?tab=readme-ov-file" rel="noopener noreferrer"&gt;here&lt;/a&gt;. The script was task given by HNG Internship for stage one DevOps participants. HNG Internship is a fast-paced bootcamp for learning digital skills. Signing up is &lt;a href="https://hng.tech/internship" rel="noopener noreferrer"&gt;free&lt;/a&gt;. If you're looking for something extra ⭐, then consider signing up for &lt;a href="https://hng.tech/premium" rel="noopener noreferrer"&gt;premium&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>bash</category>
      <category>automation</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
