<?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: njambibetty</title>
    <description>The latest articles on DEV Community by njambibetty (@njambibetty).</description>
    <link>https://dev.to/njambibetty</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%2F457168%2F2b824407-4337-4b13-975e-723bbf5286c2.jpg</url>
      <title>DEV Community: njambibetty</title>
      <link>https://dev.to/njambibetty</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/njambibetty"/>
    <language>en</language>
    <item>
      <title>Automating User Management with Bash: A Streamlined Approach</title>
      <dc:creator>njambibetty</dc:creator>
      <pubDate>Thu, 04 Jul 2024 13:24:03 +0000</pubDate>
      <link>https://dev.to/njambibetty/automating-user-management-with-bash-a-streamlined-approach-2p9f</link>
      <guid>https://dev.to/njambibetty/automating-user-management-with-bash-a-streamlined-approach-2p9f</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
As a SysOps engineer, efficiently managing user accounts is crucial, especially when onboarding new developers. Automating this process saves time and reduces errors. In this article, I'll explain a Bash script, &lt;code&gt;create_users.sh&lt;/code&gt; , designed to automate user creation, group assignment, secure password management, and comprehensive logging for troubleshooting and verification. This task was part of the DevOps track for the &lt;em&gt;HNG11 internship&lt;/em&gt;.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Script Overview&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;create_users.sh&lt;/code&gt; script reads a text file(&lt;code&gt;users.txt&lt;/code&gt;) passed as an argument, containing usernames and group names. It creates users and groups, sets up home directories, generates random passwords, and logs all actions to &lt;code&gt;/var/log/user_management.log&lt;/code&gt;.The generated passwords are securely stored in &lt;code&gt;/var/secure/user_passwords.txt&lt;/code&gt;.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Challenges and Solutions&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Root Privileges&lt;/strong&gt;:Ensuring the script runs with root privileges is essential for creating users and modifying system files.&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution&lt;/strong&gt;:The script checks for root privileges at the start and exits if not run as root.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.&lt;strong&gt;Handling Input Files&lt;/strong&gt;:Verifying that an input file is provided is crucial to prevent errors.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution&lt;/strong&gt;:The script checks if an input file is provided and exits with an error message if not.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.&lt;strong&gt;Secure Password Management&lt;/strong&gt;:Generating and storing passwords securely while ensuring only root can access them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution&lt;/strong&gt;:The script uses &lt;code&gt;dev/urandom&lt;/code&gt; to generate random passwords and stores them in a file with strict permissions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.&lt;strong&gt;Logging&lt;/strong&gt;:Maintaining a log of all actions for transparency and troubleshooting.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution&lt;/strong&gt;:The script logs all to &lt;code&gt;/var/log/user_management.log&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;strong&gt;Script Breakdown&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Check Administrative Privilege&lt;/strong&gt;
The script starts by verifying the user has root privileges, ensuring it can perform all necessary actions without encountering permission issues.
&lt;/li&gt;
&lt;/ol&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 accessibility"
    exit 1
fi

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

&lt;/div&gt;


&lt;p&gt;2.&lt;strong&gt;Check Input File&lt;/strong&gt;&lt;br&gt;
It verifies that an input file is provided and exits with an error message if not.&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"
  echo "Usage: $0 &amp;lt;name-of-text-file&amp;gt;"
  exit 1
fi

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

&lt;/div&gt;



&lt;p&gt;3.&lt;strong&gt;Setup Log and Secure Password Files&lt;/strong&gt;&lt;br&gt;
Ensures the log and password files exist, creating them if necessary, and sets appropriate permissions.&lt;br&gt;
&lt;/p&gt;

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

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

&lt;/div&gt;



&lt;p&gt;4.&lt;strong&gt;Generate Random Passwords&lt;/strong&gt;&lt;br&gt;
Defines a function to generate random passwords for the users.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;generate_random_password() {
    local length="${1:-12}"
    tr -dc 'A-Za-z0-9!?%+=' &amp;lt; /dev/urandom | head -c "$length"
}

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

&lt;/div&gt;



&lt;p&gt;5.&lt;strong&gt;Process Each Line in the Input File&lt;/strong&gt;&lt;br&gt;
Reads the input file, processes each line, and creates users and groups as specified. It also logs every action for transparency and troubleshooting.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" &amp;gt;&amp;gt; $LOG_FILE
}

create_user() {
    local username=$1
    local groups=$2

    if getent passwd "$username" &amp;gt; /dev/null; then
        log_message "User $username already exists"
    else
        useradd -m "$username"
        log_message "Created user $username"
    fi

    # Add user to specified groups
    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; then
            groupadd "$group"
            log_message "Created group $group"
        fi
        usermod -aG "$group" "$username"
        log_message "Added user $username to group $group"
    done

    # Set up home directory permissions
    chmod 700 /home/"$username"
    chown "$username:$username" /home/"$username"
    log_message "Set up home directory for user $username"

    # Generate a random password
    password=$(generate_random_password 12)
    echo "$username:$password" | chpasswd
    echo "$username,$password" &amp;gt;&amp;gt; $PASSWORD_FILE
    log_message "Set password for user $username"
}

while IFS=';' read -r username groups; do
    create_user "$username" "$groups"
done &amp;lt; "$1"

log_message "User creation process completed."

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;create_users.sh&lt;/code&gt; script automates the process of user creation, group assignment, and secure password management, ensuring efficiency and reducing potential errors. This approach is essential for SysOps engineers managing large teams.&lt;br&gt;
This project is part of the &lt;em&gt;HNG Internship program&lt;/em&gt;, which aims to train and nurture aspiring tech professionals. To learn more about the &lt;em&gt;HNG Internship&lt;/em&gt;, visit &lt;a href="https://hng.tech/internship"&gt;https://hng.tech/internship&lt;/a&gt; and &lt;a href="https://hng.tech/hire"&gt;https://hng.tech/hire&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

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