<?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: Mayowa Sodipo</title>
    <description>The latest articles on DEV Community by Mayowa Sodipo (@mayowa_sodipo_794f2d64923).</description>
    <link>https://dev.to/mayowa_sodipo_794f2d64923</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%2F1725943%2Fb6019c98-b170-466d-a34f-826653f1324f.jpg</url>
      <title>DEV Community: Mayowa Sodipo</title>
      <link>https://dev.to/mayowa_sodipo_794f2d64923</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mayowa_sodipo_794f2d64923"/>
    <language>en</language>
    <item>
      <title>Creating Linux Users using bash Script</title>
      <dc:creator>Mayowa Sodipo</dc:creator>
      <pubDate>Wed, 03 Jul 2024 20:54:18 +0000</pubDate>
      <link>https://dev.to/mayowa_sodipo_794f2d64923/creating-linux-users-using-bash-script-5hm6</link>
      <guid>https://dev.to/mayowa_sodipo_794f2d64923/creating-linux-users-using-bash-script-5hm6</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In an Unix operating system, managing users and groups can be a laborious operation, particularly when handling several users. We can automate the creation of users and groups, configure home directories, generate random passwords, and log all activities with a Bash script, which will streamline the process. You may follow along with a detailed Bash script that completes these tasks by reading this blog article.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;br&gt;
Before we dive into the code, ensure you have a basic understanding of the Bash shell and the permission requirements for user creation on your Linux system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Bash Script&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;#!/bin/bash

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

# Ensure /var/secure exists and has the correct permissions
mkdir -p /var/secure
chmod 700 /var/secure
touch "$PASSWORD_FILE"
chmod 600 "$PASSWORD_FILE"

# Function to log messages
log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}

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

# Function to add users, groups and set up home directories
setup_user() {
    local username=$1
    local groups=$2

    # Create the user
    if ! id -u "$username" &amp;amp;&amp;gt;/dev/null; then
        password=$(generate_password)
        useradd -m -s /bin/bash "$username"
        echo "$username:$password" | chpasswd
        log_message "User $username created."

        # Store the username and password
        echo "$username,$password" &amp;gt;&amp;gt; "$PASSWORD_FILE"
        log_message "Password for $username stored."
    else
        log_message "User $username already exists."
    fi
    if ! getent group "$username" &amp;amp;&amp;gt;/dev/null; then
            groupadd "$username"
            log_message "Group $username created."
        fi
        usermod -aG "$group" "$username"
        log_message "Added $username to $group."
    # Create groups and add user to groups
    IFS=',' read -ra group_array &amp;lt;&amp;lt;&amp;lt; "$groups"
    for group in "${group_array[@]}"; do
        if ! getent group "$group" &amp;amp;&amp;gt;/dev/null; then
            groupadd "$group"
            log_message "Group $group created."
        fi
        usermod -aG "$group" "$username"
        log_message "Added $username to $group."
    done

    # Set up the home directory
    local home_dir="/home/$username"
    chown "$username":"$username" "$home_dir"
    chmod 700 "$home_dir"
    log_message "Home directory set up for $username  with appropriate permissions."
}


if [ $# -eq 0 ]; then
    log_message "Usage: $0 &amp;lt;input_file&amp;gt;"
    exit 1
fi

input_file=$1
log_message "Starting users and groups script."

# Read the input file and process each line
while IFS=';' read -r username groups; do
setup_user "$username" "$groups"
done &amp;lt; "$input_file"

log_message "Users created with password and set to groups script completed."

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Understanding the Script&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;#!/bin/bash&lt;/code&gt;&lt;br&gt;
The line #!/bin/bash at the beginning of a script is called a shebang (or hashbang). It specifies the path to the interpreter that should be used to run the script. In this case, it indicates that the script should be executed using the Bash shell located at /bin/bash.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Check if script is running with sudo
if [ "$(id -u)" -ne 0 ]; then
    echo "This script must be run with sudo."
    exit 1
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;if [ "$(id -u)" -ne 0 ]; then:&lt;/code&gt; Checks if the effective user ID ($(id -u)) is not equal (-ne) to 0, which is the user ID of the root user (typically indicating sudo privileges).&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.csv"
mkdir -p /var/secure
chmod 700 /var/secure
touch "$PASSWORD_FILE"
chmod 600 "$PASSWORD_FILE"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This script makes sure that a file and directory are set up securely to store user passwords. It first determines the directories for the password and log files, and if the /var/secure directory doesn't already exist, it creates it and sets its rights so that only the owner may access it. Subsequently, it generates the password file and modifies its permissions to restrict access to only the owner. This guarantees that private password data is kept safe.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;log_message&lt;/code&gt; function logs messages to the &lt;code&gt;$LOGFILE&lt;/code&gt; path with date stamps&lt;/p&gt;

&lt;p&gt;&lt;code&gt;generate_password&lt;/code&gt; function creates a 12 character long random password&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setup_user() {
    local username=$1
    local groups=$2

    # Create the user
    if ! id -u "$username" &amp;amp;&amp;gt;/dev/null; then
        password=$(generate_password)
        useradd -m -s /bin/bash "$username"
        echo "$username:$password" | chpasswd
        log_message "User $username created."

        # Store the username and password
        echo "$username,$password" &amp;gt;&amp;gt; "$PASSWORD_FILE"
        log_message "Password for $username stored."
    else
        log_message "User $username already exists."
    fi
    if ! getent group "$username" &amp;amp;&amp;gt;/dev/null; then
            groupadd "$username"
            log_message "Group $username created."
        fi
        usermod -aG "$group" "$username"
        log_message "Added $username to $group."
    # Create groups and add user to groups
    IFS=',' read -ra group_array &amp;lt;&amp;lt;&amp;lt; "$groups"
    for group in "${group_array[@]}"; do
        if ! getent group "$group" &amp;amp;&amp;gt;/dev/null; then
            groupadd "$group"
            log_message "Group $group created."
        fi
        usermod -aG "$group" "$username"
        log_message "Added $username to $group."
    done

    # Set up the home directory
    local home_dir="/home/$username"
    chown "$username":"$username" "$home_dir"
    chmod 700 "$home_dir"
    log_message "Home directory set up for $username  with appropriate permissions."
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This script defines a function setup_user that creates a new user with specified groups. It checks if the user already exists, and if not, generates a password, creates the user, and stores the username and password in a secure file. It then creates any specified groups that do not already exist and adds the user to those groups. Finally, it sets up the user's home directory with the correct ownership and permissions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [ $# -eq 0 ]; then
    log_message "Usage: $0 &amp;lt;input_file&amp;gt;"
    exit 1
fi

input_file=$1
log_message "Starting users and groups script."

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

&lt;/div&gt;



&lt;p&gt;This piece of code determines whether any command-line arguments are supplied ($# determines the number of arguments). It reports an error message showing the right usage and quits with a status of 1, signalling an error, if none are given ($# -eq 0). It logs a message signalling the beginning of a script for managing users and groups if an input file argument is given.&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 username groups; do
setup_user "$username" "$groups"
done &amp;lt; "$input_file"

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

&lt;/div&gt;



&lt;p&gt;This script reads a file line by line, expecting each line to have a group and a username separated by a semicolon (;). It invokes the setup_user method for each line, passing the groups and username as parameters. Presumably, the setup_user function adds the user to the selected groups and creates them. Until every line in the input file has been processed, this loop keeps going.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Running the script&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To run the script, execute it with superuser privileges (as user creation requires root access):&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo bash create_users.sh users.txt&lt;/code&gt;&lt;br&gt;
Upon execution this script will create multiple users, multiple groups and set up their home directory&lt;/p&gt;

&lt;p&gt;To learn more and push your programming journey forward you can visit:&lt;br&gt;
&lt;a href="https://hng.tech/internship"&gt;https://hng.tech/internship&lt;/a&gt; or &lt;a href="https://hng.tech/hire"&gt;https://hng.tech/hire&lt;/a&gt;&lt;/p&gt;

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