<?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: Anthony Obotidem</title>
    <description>The latest articles on DEV Community by Anthony Obotidem (@anthony_obotidem_4e5d7748).</description>
    <link>https://dev.to/anthony_obotidem_4e5d7748</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%2F1734999%2Fdc2709f1-d540-4375-9d62-c0fbb9dad618.jpg</url>
      <title>DEV Community: Anthony Obotidem</title>
      <link>https://dev.to/anthony_obotidem_4e5d7748</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anthony_obotidem_4e5d7748"/>
    <language>en</language>
    <item>
      <title>Automating Linux User Creation with Bash Script</title>
      <dc:creator>Anthony Obotidem</dc:creator>
      <pubDate>Fri, 05 Jul 2024 22:55:28 +0000</pubDate>
      <link>https://dev.to/anthony_obotidem_4e5d7748/automating-linux-user-creation-with-bash-script-3lgb</link>
      <guid>https://dev.to/anthony_obotidem_4e5d7748/automating-linux-user-creation-with-bash-script-3lgb</guid>
      <description>&lt;p&gt;In today's fast-paced technology environment, efficiency and automation are key. &lt;br&gt;
Automating tasks with a Bash&lt;br&gt;
script can save a significant amount of time and reduce errors. In this technical report, we will walk through the process of creating a Bash script to automate user and group creation, setting up home directories, and managing permissions and passwords.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project Overview&lt;/strong&gt;&lt;br&gt;
Your company has recently hired several new developers, and you need to create user accounts and groups for them. To streamline this process, we will write a Bash script called create_users.sh.&lt;br&gt;
This script will;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Read a text file containing usernames and group names,&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create users and groups as specified,&lt;/li&gt;
&lt;li&gt;Set up home directories,
Generate random passwords, and&lt;/li&gt;
&lt;li&gt;Log all actions to /var/log/user_management.log and store the generated passwords securely in /var/secure/user_passwords.txt. 4. We can create the Bash script called "create_users.sh" with this command; Bash script
Implementation steps
Let's walk through the script step-by-step to understand its functionality.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Checking root privileges;&lt;br&gt;
This line specifies that the script should be executed with the Bash shell.&lt;br&gt;
shabang&lt;br&gt;
The script checks if it is being run as root. If not, it prompts the user to run the script with root privileges and exits.&lt;br&gt;
Root privileges&lt;br&gt;
Image description&lt;/p&gt;

&lt;p&gt;Checking for User Data File;&lt;br&gt;
The script checks if the filename (user-data-file) is provided as an argument. If not, it displays the correct usage and exits.&lt;br&gt;
user data file&lt;br&gt;
Image description&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Initializing Variables and Creating Directories;&lt;/strong&gt;&lt;br&gt;
The script creates the necessary directories and sets appropriate permissions to ensure security.&lt;br&gt;
Here, The 'user_data_file' stores the filename provided as an argument. Additionally 'log_file' and 'password_file' store the paths for logging actions and storing passwords.&lt;br&gt;
Initialize variables&lt;br&gt;
Image description&lt;/p&gt;

&lt;p&gt;Generating Random Passwords:&lt;br&gt;
A function to generate random passwords using openssl.&lt;br&gt;
Random password&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Image description&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Reading User Data File and Creating Users;&lt;br&gt;
The script reads the user data file line by line. For each line, it:&lt;br&gt;
. Trims any leading or trailing whitespaces from the username and groups.&lt;br&gt;
. Checks if the user already exists. If so, it logs the information and moves to the next user.&lt;br&gt;
. Creates the user and assigns them a personal group.&lt;br&gt;
creating users&lt;br&gt;
Image description&lt;/p&gt;

&lt;p&gt;Adding Users to Additional Groups;&lt;br&gt;
If additional groups are specified, the script adds the user to these groups, creating the groups if they do not exist.&lt;br&gt;
Adding users&lt;br&gt;
Image description&lt;/p&gt;

&lt;p&gt;Setting Home Directory Permissions;&lt;br&gt;
The script sets appropriate permissions for the user's home directory.&lt;br&gt;
Directory permission&lt;/p&gt;

&lt;p&gt;Image description&lt;/p&gt;

&lt;p&gt;Generating and Storing Passwords; It generates a random password, sets it for the user, and stores it in the password file. Store passwords&lt;br&gt;
Image description&lt;/p&gt;

&lt;p&gt;Logging Actions; Finally, the script logs all actions and completes the user creation process. Logging actions Image description&lt;br&gt;
Running the script;&lt;/p&gt;

&lt;p&gt;Create the txt file containing the users and the groups;&lt;br&gt;
The user accounts' structure is contained in this text file. Save and close the file.&lt;br&gt;
txt file.&lt;br&gt;
Image description&lt;br&gt;
Every line in the file identifies a user along with the groups (such "admin" or "finance") to which they are assigned. The semicolon divides the groups and users. users.txt has the structure:&lt;br&gt;
user datafile.&lt;br&gt;
Image description&lt;/p&gt;

&lt;p&gt;Ensure the script is executable;&lt;br&gt;
Execute script&lt;/p&gt;

&lt;p&gt;Image description&lt;/p&gt;

&lt;p&gt;Run script; Run script&lt;br&gt;
Image description&lt;/p&gt;

&lt;p&gt;Verify the results&lt;/p&gt;

&lt;p&gt;Check the log file for actions performed;&lt;br&gt;
Image description&lt;/p&gt;

&lt;p&gt;Verify the user passwords file;&lt;br&gt;
Image description&lt;/p&gt;

&lt;p&gt;3.Ensure the new users and groups are created correctly;&lt;/p&gt;

&lt;p&gt;Image description&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
This script automates the creation of users and groups, ensuring a streamlined onboarding process. This article is a stage two task in the DevOps of HNG internship. For more information about the HNG Internship and how it can benefit your organization, visit &lt;a href="//http//hng.tech/internship"&gt;HNG Internship&lt;/a&gt; and &lt;a href="//http//hng.tech/hire.com"&gt;HNG Hire&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;By using this tutorial, you can make your organization's user management procedure more efficient and ensure that new developers are onboarded promptly&lt;/p&gt;

&lt;p&gt;Wishing you the best as you continue your Tech journey &lt;/p&gt;

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