<?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: Olatunji</title>
    <description>The latest articles on DEV Community by Olatunji (@hollyphat).</description>
    <link>https://dev.to/hollyphat</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%2F545183%2Ffb938443-b76f-4185-89ce-84067ad201d7.jpeg</url>
      <title>DEV Community: Olatunji</title>
      <link>https://dev.to/hollyphat</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hollyphat"/>
    <language>en</language>
    <item>
      <title>Automating User Account Management in Linux with a Bash Script</title>
      <dc:creator>Olatunji</dc:creator>
      <pubDate>Tue, 02 Jul 2024 14:12:40 +0000</pubDate>
      <link>https://dev.to/hollyphat/automating-user-account-management-in-linux-with-a-bash-script-248n</link>
      <guid>https://dev.to/hollyphat/automating-user-account-management-in-linux-with-a-bash-script-248n</guid>
      <description>&lt;p&gt;User Management is an integral part of a Sys Ops Engineer, as this is useful in day-to-day activity. This is usually required when onboarding new members of staff. In this piece, we will go through the process of creating and assigning new users.&lt;/p&gt;

&lt;p&gt;This is part of HNG Internship requirements. You can learn more about HNG by clicking on the link below.&lt;br&gt;
&lt;a href="https://hng.tech/internship"&gt;HNG Internship&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Premise
&lt;/h2&gt;

&lt;p&gt;Manually handling user accounts can be tedious and often leads to mistakes. To make things easier and more reliable, we should automate this process. We'll create a script called "create_users.sh" that will read a list of usernames and groups from a given text file, create the users and groups, set up their home directories, generate random passwords, and log everything to a management.log file. This will save time, reduce errors, and keep things consistent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;The following are the requirements needed to create and execute the script&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic Knowledge of Linux command&lt;/li&gt;
&lt;li&gt;Admin privilege&lt;/li&gt;
&lt;li&gt;Text editor e.g Vim, Nano, TextEdit, etc&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;The script is expected to perform the following tasks&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reads a list of users and groups from any given text file.&lt;/li&gt;
&lt;li&gt;Creates users and assigns them to specified groups.&lt;/li&gt;
&lt;li&gt;Set up home directories with appropriate permissions.&lt;/li&gt;
&lt;li&gt;Generates random passwords for the users.&lt;/li&gt;
&lt;li&gt;Logs all actions to &lt;code&gt;/var/log/user_management.log.&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Stores the generated passwords securely in &lt;code&gt;/var/secure/user_passwords.csv&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Procedure
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Define the variable to accept the input file, log file and 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;INPUT_FILE="$1"
USER_INPUT_FILE="/var/log/user_management.log"
PASSWORD_FILE="/var/secure/user_passwords.csv"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create functions to perform tasks
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Function to log messages
log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | sudo tee -a $USER_INPUT_FILE &amp;gt; /dev/null
}

# Function to generate random password
random_password() {    
    &amp;lt; /dev/urandom tr -dc 'A-Za-z0-9' | head -c 12
}

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create directories, and files and give permissions
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Create neccessary directories if they do not exist
sudo mkdir -p /var/log
sudo mkdir -p /var/secure

# create log file if it does not exist, and set the neccessary permission
sudo touch $USER_INPUT_FILE
sudo chmod 600 $USER_INPUT_FILE

# create password file if it does not exist, and set the neccessary permission
sudo touch $PASSWORD_FILE
sudo chmod 600 $PASSWORD_FILE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The code below read the file line by line, create the user, add to group and set password
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Read the input file line by line
while IFS=';' read -r username groups; do
    # Remove whitespace from username and group
    username=$(echo $username | xargs)
    groups=$(echo $groups | xargs)

    # Create the new user 
    if id -u "$username" &amp;gt;/dev/null 2&amp;gt;&amp;amp;1; then
        log_message "User $username already exists. Creation skipped."
    else
        sudo useradd -m -s /bin/bash "$username"
        if [ $? -eq 0 ]; then
            log_message "New user: $username created successfully."
        else
            log_message "Unable to create user: $username."
            continue
        fi
    fi

    # Create the new user personal group
    if ! getent group "$username" &amp;gt;/dev/null 2&amp;gt;&amp;amp;1; then
        sudo groupadd "$username"
        log_message "Personal group $username created successfully"
    fi

    # Add user to group
    sudo usermod -aG "$username" "$username"

    # Add the user to other groups
    IFS=',' read -ra group_array &amp;lt;&amp;lt;&amp;lt; "$groups"
    for group in "${group_array[@]}"; do
        group=$(echo $group | xargs) # Remove whitespace
        if ! getent group "$group" &amp;gt;/dev/null 2&amp;gt;&amp;amp;1; then
            sudo groupadd "$group"
            log_message "Group $group created."
        fi
        sudo usermod -aG "$group" "$username"
        log_message "User $username added to group: $group."
    done

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

    log_message "Password set for user $username."
done &amp;lt; "$INPUT_FILE"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Log message to show the status after execution
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;log_message "User creation script completed."
echo "User creation process is complete. Check $USER_INPUT_FILE for details"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Using a bash script to automate user account management can greatly simplify the onboarding process for new employees, users, or accounts. By following the steps outlined in this article, you can create an effective script that ensures users are created, added to groups, and provided with secure passwords, all while logging actions for transparency and audit purposes.&lt;/p&gt;

&lt;p&gt;This tutorial is made possible by &lt;a href="https://hng.tech/hire"&gt;HNG&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can find the bash code &lt;a href="https://github.com/hollyphat/Hng11-Stage-1"&gt;https://github.com/hollyphat/Hng11-Stage-1&lt;/a&gt;.&lt;/p&gt;

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