<?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: AnnCodes0001</title>
    <description>The latest articles on DEV Community by AnnCodes0001 (@anncodes0001).</description>
    <link>https://dev.to/anncodes0001</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%2F1400570%2F9390facf-4b05-4f1f-aaf7-5392686a6f2e.png</url>
      <title>DEV Community: AnnCodes0001</title>
      <link>https://dev.to/anncodes0001</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anncodes0001"/>
    <language>en</language>
    <item>
      <title>Linux User creation Bash script</title>
      <dc:creator>AnnCodes0001</dc:creator>
      <pubDate>Mon, 01 Jul 2024 13:01:09 +0000</pubDate>
      <link>https://dev.to/anncodes0001/linux-user-creation-bash-script-3gna</link>
      <guid>https://dev.to/anncodes0001/linux-user-creation-bash-script-3gna</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This blog post explores leveraging Docker containers to automate the user creation with a custom Bash script. This Dockerized approach not only streamlines user creation but also cuts the time needed into two, therefore saving time, improving consistency, and enhancing security.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Scenario 
&lt;/h2&gt;

&lt;p&gt;Your company has employed many new developers. As a SysOps engineer, write a bash script called create_users.sh that reads a text file containing the employee's usernames and group names, where each line is formatted as user;groups.&lt;br&gt;
The script should create users and groups as specified, set up home directories with appropriate permissions and ownership, generate random passwords for the users, and log all actions to /var/log/user_management.log. Additionally, store the generated passwords securely in /var/secure/user_passwords.txt.&lt;/p&gt;
&lt;h2&gt;
  
  
  Implementation steps
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Create the txt file containing the users and their groups: &lt;/strong&gt;&lt;br&gt;
This text file acts as the schema for the user accounts. Each line in the file specifies a user and the groups (e.g., "admin" or "finance") they belong to. the semicolon separates the users from the groups. users.txt follows the format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;light;sudo,dev,www-data
ann;sudo
kosi;dev,www-data
evie;finance,hr
dolapo;marketing,sales
ify;it,security,network
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Create a Dockerfile:&lt;/strong&gt;&lt;br&gt;
For increased consistency and effortless deployment, I containerized the user creation script using a Dockerfile. This Dockerfile defines the steps to prepare the environment and run the script within a containerized environment, ensuring a uniform user creation process.&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Use an official Alpine as a base image
FROM alpine:latest

# Install necessary packages
RUN apk update &amp;amp;&amp;amp; apk add --no-cache \
    sudo \
    openssl \
    bash \
    shadow \
    util-linux \
    vim

# Copy the script and users.txt into the container
COPY create_users.sh /usr/local/bin/create_users.sh
COPY users.txt /usr/local/bin/users.txt

# Make the script executable
RUN chmod +x /usr/local/bin/create_users.sh

# Specify script to run on container start
CMD ["bash", "/usr/local/bin/create_users.sh", "/usr/local/bin/users.txt"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This Dockerfile installs essential tools (sudo, openssl, bash, etc.) via apk and copies create_users.sh and users.txt (file containing the employee usernames and group names) to /usr/local/bin/ (creating the directory if needed). It sets the script create_users.sh to execute upon container startup using CMD.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Create the Script&lt;/strong&gt;&lt;br&gt;
The bash script, create_users.sh, automates the process of creating users, assigning them to appropriate groups (e.g., sudo), setting home directory permissions, and logging all actions to a file for auditing purposes. With security at the forefront, this script avoids hardcoded credentials and potentially prompting for user input during execution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

# Check if the script is run as root (superuser)
if [ "$(id -u)" -ne 0 ]; then
    echo "Please run this script as root or using sudo."
    exit 1
fi

# Check if the input file is provided
if [ -z "$1" ]; then
    echo "Usage: $0 &amp;lt;name-of-text-file&amp;gt;"
    exit 1
fi

INPUT_FILE="$1"
LOG_FILE="/var/log/user_management.log"
PASSWORD_FILE="/var/secure/user_passwords.csv"

# Create log and password files if they do not exist
touch $LOG_FILE
mkdir -p /var/secure
touch $PASSWORD_FILE
chmod 600 $PASSWORD_FILE

# Function to generate random passwords
generate_password() {
    openssl rand -base64 12
}

# Read the input file line by line
while IFS=';' read -r username groups; do
    username=$(echo "$username" | xargs)
    groups=$(echo "$groups" | xargs)

    # Check if user already exists
    if id "$username" &amp;amp;&amp;gt;/dev/null; then
        echo "User $username already exists, skipping..." | tee -a $LOG_FILE
        continue
    fi

    # Create a personal group for the user
    addgroup "$username"

    # Create the user with the personal group and home directory
    adduser -D -G "$username" -s /bin/bash "$username"

    # Add the user to additional groups if specified
    if [ -n "$groups" ]; then
        IFS=',' read -r -a group_array &amp;lt;&amp;lt;&amp;lt; "$groups"
        for group in "${group_array[@]}"; do
            group=$(echo "$group" | xargs)
            if ! getent group "$group" &amp;gt; /dev/null; then
                addgroup "$group"
            fi
            adduser "$username" "$group"
        done
    fi

    # Generate a random password for the user
    password=$(generate_password)

    # Set the user's password
    echo "$username:$password" | chpasswd

    # Log the actions
    echo "Created user $username with groups $groups and home directory" | tee -a $LOG_FILE

    # Store the username and password securely
    echo "$username,$password" &amp;gt;&amp;gt; $PASSWORD_FILE
done &amp;lt; "$INPUT_FILE"

echo "User creation process completed. Check $LOG_FILE for details."

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

&lt;/div&gt;



&lt;p&gt;For better understanding on the rationale behind this script&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- **Check if the executor has root privileges:&lt;/strong&gt; **&lt;br&gt;
By performing a root privilege check at the outset, the script avoids permission-related issues that could arise without proper access because this script requires root privileges (sudo) to create users and groups, manage passwords, and modify system files.&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)" -ne 0 ]; then
    echo "Please run this script as root or using sudo."
    exit 1
fi

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;- **Check that the correct input file is being used:&lt;/strong&gt;** &lt;br&gt;
This ensures that hat the script is provided with an input file (users.txt), which contains usernames and employee groups. If this file isn't used, the script can not proceed and informs the user about the correct usage format (script name followed by the file name).&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 "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;&lt;strong&gt;- File and Directory Setup:&lt;/strong&gt;&lt;br&gt;
 This step creates or verifies the existence of essential files: user_management.log for logging script actions and user_passwords.csv for encrypted password storage The password file has restricted permissions (chmod 600), this ensures that only the root user can access and modify its contents, once again prioritising security.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch $LOG_FILE
mkdir -p /var/secure
touch $PASSWORD_FILE
chmod 600 $PASSWORD_FILE
4. Password Generation Function: This function capitalizes on openssl to generate secure passwords with sufficient length and complexity.
generate_password() {
    openssl rand -base64 12
}

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

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;- User Creation Loop: *&lt;/em&gt;&lt;br&gt;
This reads each line from the input file (users.txt). For each line, it checks if the user already exists. If it doesn't, it proceeds to create the user with their personal group and home directory. If it exists, it adds the user to additional groups listed in users.txt.&lt;br&gt;
&lt;/p&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
    username=$(echo "$username" | xargs)
    groups=$(echo "$groups" | xargs)

    # Check if user already exists
    if id "$username" &amp;amp;&amp;gt;/dev/null; then
        echo "User $username already exists, skipping..." | tee -a $LOG_FILE
        continue
    fi

    # Create a personal group for the user
    addgroup "$username"

    # Create the user with the personal group and home directory
    adduser -D -G "$username" -s /bin/bash "$username"

    # Add the user to additional groups if specified
    if [ -n "$groups" ]; then
        IFS=',' read -r -a group_array &amp;lt;&amp;lt;&amp;lt; "$groups"
        for group in "${group_array[@]}"; do
            group=$(echo "$group" | xargs)
            if ! getent group "$group" &amp;gt; /dev/null; then
                addgroup "$group"
            fi
            adduser "$username" "$group"
        done
    fi

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;- Password Setting and Logging:&lt;/strong&gt; &lt;br&gt;
It first generates a random password using the generate_password function. This ensures that each user's password is unique and strong. The script then sets the user's password with chpasswd. As an important step, it logs the entire user creation process, including the username, associated groups, and home directory information, to the user_management.log file. This comprehensive trail allows for easy tracking and verification of all actions performed by the script for user creation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Generate a random password for the user
    password=$(generate_password)

    # Set the user's password
    echo "$username:$password" | chpasswd

    # Log the actions
    echo "Created user $username with groups $groups and home directory" | tee -a $LOG_FILE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;- Password Storage:&lt;/strong&gt; &lt;br&gt;
Each user's username is stored, and a secure password is generated in user_passwords.csv. This file is essential for securely managing and distributing user credentials while maintaining utmost secrecy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Store the username and password securely
    echo "$username,$password" &amp;gt;&amp;gt; $PASSWORD_FILE
done &amp;lt; "$INPUT_FILE"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;-  Completion Message:&lt;/strong&gt;&lt;br&gt;
Notifies the user upon completion of the user creation process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo "User creation process completed. Check $LOG_FILE for details."

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Build the Docker Image and Run the Docker Container:&lt;/strong&gt; &lt;br&gt;
Using the docker run command to build a Docker image named "user_creation" using docker build -t user_creation ., to encapsulate our entire user creation environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t user_creation .
docker run --rm -it --name user_creation_container user_creation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. Copy Logs from Docker container to local machine:&lt;/strong&gt;&lt;br&gt;
After the script finishes its work, we use docker cp to copy the log file and password file from the container. In ding this we have a copy on our local machine.&lt;br&gt;
&lt;/p&gt;

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

docker cp user_creation_container:/var/secure/user_passwords.csv .
cat user_passwords.csv

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. Final testing&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;sudo bash create_users.sh users.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;With this, we have successfully automated user creation with a Bash script and streamlined the process with Docker. From defining user details in users.txt to containerized execution, we've done it all.&lt;br&gt;
To be part of the program that provided this task scenario, visit their official websites to gain more insights&lt;br&gt;
&lt;a href="https://hng.tech/internship"&gt;https://hng.tech/internship&lt;/a&gt;&lt;br&gt;
&lt;a href="https://hng.tech/hire"&gt;https://hng.tech/hire&lt;/a&gt;&lt;/p&gt;

</description>
      <category>bash</category>
      <category>linux</category>
      <category>devops</category>
      <category>cloud</category>
    </item>
  </channel>
</rss>
