<?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: Rufus Chibuike</title>
    <description>The latest articles on DEV Community by Rufus Chibuike (@rufus_chibuike_1652bc4801).</description>
    <link>https://dev.to/rufus_chibuike_1652bc4801</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%2F1713346%2Ff6d49f57-a99e-4604-b7d9-8c80669f31c8.png</url>
      <title>DEV Community: Rufus Chibuike</title>
      <link>https://dev.to/rufus_chibuike_1652bc4801</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rufus_chibuike_1652bc4801"/>
    <language>en</language>
    <item>
      <title>Technical Article</title>
      <dc:creator>Rufus Chibuike</dc:creator>
      <pubDate>Tue, 02 Jul 2024 12:29:11 +0000</pubDate>
      <link>https://dev.to/rufus_chibuike_1652bc4801/technical-article-46hb</link>
      <guid>https://dev.to/rufus_chibuike_1652bc4801/technical-article-46hb</guid>
      <description>&lt;p&gt;Introduction&lt;br&gt;
SysOps engineers must automate user and group management in today's fast-paced technology landscape. This article examines create_users.sh, a comprehensive Bash script that automates user creation, group assignment, and password management on Linux systems.&lt;/p&gt;

&lt;p&gt;Script Overview&lt;br&gt;
The create_users.sh script reads a text file containing usernames and group names, creates users with home directories, assigns groups, generates random passwords, and records all actions. The credentials are securely kept, allowing only the root user to access them.&lt;/p&gt;

&lt;p&gt;Script Details&lt;br&gt;
Shebang and Root Check&lt;/p&gt;

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

&lt;p&gt;if [ "$(id -u)" -ne 0 ]; then&lt;br&gt;
    echo "This script must be run as root" &amp;gt;&amp;amp;2&lt;br&gt;
    exit 1&lt;br&gt;
fi&lt;br&gt;
The script begins with the shebang line, which specifies the script interpreter. It determines if the script is executed as root to ensure adequate permissions for user and group administration.&lt;/p&gt;

&lt;p&gt;Log and Password File Initialization&lt;br&gt;
LOG_FILE="/var/log/user_management.log"&lt;br&gt;
PASSWORD_FILE="/var/secure/user_passwords.txt"&lt;/p&gt;

&lt;p&gt;mkdir -p /var/secure&lt;br&gt;
chmod 700 /var/secure&lt;br&gt;
echo "User creation script started at $(date)" &amp;gt; $LOG_FILE&lt;br&gt;
Log files and folders are initialized, verifying that the secure password directory exists and has the required permissions.&lt;/p&gt;

&lt;p&gt;Generate Random Passwords&lt;br&gt;
generate_password() {&lt;br&gt;
    tr -dc A-Za-z0-9 &amp;lt;/dev/urandom | head -c 12&lt;br&gt;
}&lt;br&gt;
For secure password generation, a function is provided that generates random passwords with tr and /dev/urandom.&lt;/p&gt;

&lt;p&gt;Reading and Processing the Input File&lt;br&gt;
while IFS=";" read -r username groups; do&lt;br&gt;
    username=$(echo "$username" | xargs)&lt;br&gt;
    groups=$(echo "$groups" | xargs)&lt;br&gt;
The script reads the input file line by line, separating usernames and groups and eliminating whitespace. &lt;/p&gt;

&lt;p&gt;User and Group Management&lt;br&gt;
if id "$username" &amp;amp;&amp;gt;/dev/null; then&lt;br&gt;
    echo "User $username already exists, skipping." &amp;gt;&amp;gt; $LOG_FILE&lt;br&gt;
    continue&lt;br&gt;
fi&lt;/p&gt;

&lt;p&gt;groupadd "$username" &amp;amp;&amp;gt;&amp;gt; $LOG_FILE&lt;br&gt;
useradd -m -g "$username" -s /bin/bash "$username" &amp;amp;&amp;gt;&amp;gt; $LOG_FILE&lt;/p&gt;

&lt;p&gt;Users are created only if they do not already exist. Each user is given a personal group.&lt;/p&gt;

&lt;p&gt;Assigning Additional Groups and Setting Passwords&lt;br&gt;
IFS=',' read -ra ADDR &amp;lt;&amp;lt;&amp;lt; "$groups"&lt;br&gt;
for group in "${ADDR[@]}"; do&lt;br&gt;
    group=$(echo "$group" | xargs)&lt;br&gt;
    if ! getent group "$group" &amp;amp;&amp;gt;/dev/null; then&lt;br&gt;
        groupadd "$group" &amp;amp;&amp;gt;&amp;gt; $LOG_FILE&lt;br&gt;
    fi&lt;br&gt;
    usermod -aG "$group" "$username" &amp;amp;&amp;gt;&amp;gt; $LOG_FILE&lt;br&gt;
done&lt;/p&gt;

&lt;p&gt;password=$(generate_password)&lt;br&gt;
echo "$username:$password" | chpasswd &amp;amp;&amp;gt;&amp;gt; $LOG_FILE&lt;br&gt;
echo "$username,$password" &amp;gt;&amp;gt; $PASSWORD_FILE&lt;/p&gt;

&lt;p&gt;Users are added to specified groups, and random passwords are generated and set. Passwords are logged securely.&lt;/p&gt;

&lt;p&gt;Finalizing Permissions&lt;br&gt;
chmod 600 $PASSWORD_FILE&lt;br&gt;
echo "User creation script completed at $(date)" &amp;gt;&amp;gt; $LOG_FILE&lt;/p&gt;

&lt;p&gt;The script ensures that the password file is only readable by the root user.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
This script simplifies user management, ensuring that new employees can be onboarded quickly and securely. Proper logging and secure password storage are vital for maintaining system integrity and security.&lt;/p&gt;

&lt;p&gt;For more information about such scripts and internships, explore the HNG Internship Program and learn how to &lt;a href="https://hng.tech/internship"&gt;https://hng.tech/internship&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Summary&lt;br&gt;
The create_users.sh script automates the process of establishing users, allocating groups, and password management in a secure and fast manner. SysOps engineers can maintain strong and scalable systems by understanding the script's individual components.&lt;/p&gt;

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