<?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: Iheme chidera favour</title>
    <description>The latest articles on DEV Community by Iheme chidera favour (@devdera).</description>
    <link>https://dev.to/devdera</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%2F1735684%2Fb14aaa2e-d139-4bef-bbd2-7d024f565c60.png</url>
      <title>DEV Community: Iheme chidera favour</title>
      <link>https://dev.to/devdera</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devdera"/>
    <language>en</language>
    <item>
      <title>Linux User Creation Bash Script</title>
      <dc:creator>Iheme chidera favour</dc:creator>
      <pubDate>Fri, 05 Jul 2024 16:38:47 +0000</pubDate>
      <link>https://dev.to/devdera/linux-user-creation-bash-script-1hfm</link>
      <guid>https://dev.to/devdera/linux-user-creation-bash-script-1hfm</guid>
      <description>&lt;p&gt;[As part of the HNG Internship program, we were tasked with creating a bash script named create_users.sh to automate the creation of new users and groups on a Linux system.&lt;br&gt;
Checkout (&lt;a href="https://hng.tech/internship"&gt;https://hng.tech/internship&lt;/a&gt;) and (&lt;a href="https://hng.tech/premium"&gt;https://hng.tech/premium&lt;/a&gt;) for more information]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;br&gt;
This script, create_users.sh, automates the creation of users and their associated groups, sets up their home directories, generates random passwords, and logs all actions. The script reads from a specified text file containing usernames and group names.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;br&gt;
The script must be run with root privileges.&lt;br&gt;
Ensure the input file with usernames and groups is formatted correctly and exists.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Script steps&lt;/strong&gt;&lt;br&gt;
I created a file called Create_Users.sh&lt;/p&gt;

&lt;p&gt;Using vim editor, I created a log file , password.txt file .&lt;br&gt;
Ensure my script is run as root and set up specific instructions and permissions.&lt;br&gt;
Below is the content of the script.&lt;/p&gt;

&lt;h2&gt;
  
  
  !/bin/bash
&lt;/h2&gt;

&lt;h1&gt;
  
  
  Create log file and secure password file with proper permissions
&lt;/h1&gt;

&lt;p&gt;LOG_FILE="/var/secure/user_management.log"&lt;br&gt;
PASSWORD_FILE="/var/secure/user_passwords.txt"&lt;/p&gt;

&lt;h1&gt;
  
  
  Ensure the script is run as root
&lt;/h1&gt;

&lt;p&gt;if [[ "$(id -u)" -ne 0 ]]; then&lt;br&gt;
    echo "This script must be run as root."&lt;br&gt;
    exit 1&lt;br&gt;
fi&lt;/p&gt;

&lt;h1&gt;
  
  
  Ensure the log file exists
&lt;/h1&gt;

&lt;p&gt;touch "$LOG_FILE"&lt;/p&gt;

&lt;h1&gt;
  
  
  Setup password file
&lt;/h1&gt;

&lt;p&gt;if [[ ! -d "/var/secure" ]]; then&lt;br&gt;
    mkdir /var/secure&lt;br&gt;
fi&lt;br&gt;
if [[ ! -f "$PASSWORD_FILE" ]]; then&lt;br&gt;
    touch "$PASSWORD_FILE"&lt;br&gt;
    chmod 600 "$PASSWORD_FILE"&lt;br&gt;
fi&lt;/p&gt;

&lt;h1&gt;
  
  
  Check if the input file is provided
&lt;/h1&gt;

&lt;p&gt;if [[ -z "$1" ]]; then&lt;br&gt;
    echo "Usage: bash create_users.sh "&lt;br&gt;
    echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: No input file provided." &amp;gt;&amp;gt; "$LOG_FILE"&lt;br&gt;
    exit 1&lt;br&gt;
fi&lt;/p&gt;

&lt;h1&gt;
  
  
  Read the input file line by line
&lt;/h1&gt;

&lt;p&gt;while IFS=';' read -r username groups; do&lt;br&gt;
    # Skip empty lines&lt;br&gt;
    [[ -z "$username" ]] &amp;amp;&amp;amp; continue&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Remove whitespace
username=$(echo "$username" | xargs)
groups=$(echo "$groups" | xargs)

# Create user if not exists
if ! id "$username" &amp;amp;&amp;gt;/dev/null; then
    # Create the user with a home directory
    useradd -m -s /bin/bash "$username"
    if [[ $? -ne 0 ]]; then
        echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: Failed to create user $username." &amp;gt;&amp;gt; "$LOG_FILE"
        continue
    fi
    echo "$(date '+%Y-%m-%d %H:%M:%S') - INFO: User $username created." &amp;gt;&amp;gt; "$LOG_FILE"

    # Generate a random password for the user
    password=$(openssl rand -base64 12)
    echo "$username:$password" | chpasswd

    # Save the password to the secure password file
    echo "$username,$password" &amp;gt;&amp;gt; "$PASSWORD_FILE"
    echo "$(date '+%Y-%m-%d %H:%M:%S') - INFO: Password for user $username generated and stored." &amp;gt;&amp;gt; "$LOG_FILE"
else
    echo "$(date '+%Y-%m-%d %H:%M:%S') - INFO: User $username already exists." &amp;gt;&amp;gt; "$LOG_FILE"
fi

# Create groups and add user to them
IFS=',' read -ra group_list &amp;lt;&amp;lt;&amp;lt; "$groups"
for group in "${group_list[@]}"; do
    group=$(echo "$group" | xargs)
    # Create group if not exists
    if ! getent group "$group" &amp;gt;/dev/null; then
        groupadd "$group"
        echo "$(date '+%Y-%m-%d %H:%M:%S') - INFO: Group $group created." &amp;gt;&amp;gt; "$LOG_FILE"
    fi
    # Add user to the group
    usermod -a -G "$group" "$username"
    echo "$(date '+%Y-%m-%d %H:%M:%S') - INFO: User $username added to group $group." &amp;gt;&amp;gt; "$LOG_FILE"
done

# Set ownership and permissions for the home directory
chown -R "$username:$username" "/home/$username"
chmod 700 "/home/$username"
echo "$(date '+%Y-%m-%d %H:%M:%S') - INFO: Home directory for user $username set up with appropriate permissions." &amp;gt;&amp;gt; "$LOG_FILE"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;done &amp;lt; "$1"&lt;/p&gt;

&lt;p&gt;echo "$(date '+%Y-%m-%d %H:%M:%S') - INFO: User creation script completed." &amp;gt;&amp;gt; "$LOG_FILE"&lt;/p&gt;

&lt;p&gt;exit 0# &lt;/p&gt;

&lt;p&gt;Next, I created an employees.txt file for the usernames and groups.&lt;/p&gt;

&lt;p&gt;Granted permission to the Create_Users.sh file using chmod +x /home/kali/Desktop/HNG/Create_Users.sh (this is the file path) and sudo /home/kali/Desktop/HNG/Create_Users.sh /home/kali/Desktop/HNG/employees.txt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verify Execution&lt;/strong&gt;&lt;br&gt;
Input the following to verify execution &lt;/p&gt;

&lt;p&gt;id John for user creation verification &lt;/p&gt;

&lt;p&gt;Groups John to verify the groups John is in.&lt;/p&gt;

&lt;p&gt;Cat /var/log/user_management.log to print log details.&lt;/p&gt;

&lt;p&gt;Cat /car/secure/user_passwords.txt to print passwords.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Learn More About HNG Internship&lt;/strong&gt;&lt;br&gt;
The HNG Internship is a remote internship program designed to find and develop the most talented software developers. It offers a stimulating environment for interns to improve their skills and showcase their abilities through real-world tasks.&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;&lt;/a&gt;&lt;/p&gt;

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