<?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: Victor Gentle</title>
    <description>The latest articles on DEV Community by Victor Gentle (@victorgentle).</description>
    <link>https://dev.to/victorgentle</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%2F1444352%2F1ffc2285-0c07-4f1e-80b7-beed1c0b73eb.jpeg</url>
      <title>DEV Community: Victor Gentle</title>
      <link>https://dev.to/victorgentle</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/victorgentle"/>
    <language>en</language>
    <item>
      <title>Linux User Creation Bash Script</title>
      <dc:creator>Victor Gentle</dc:creator>
      <pubDate>Mon, 01 Jul 2024 12:44:33 +0000</pubDate>
      <link>https://dev.to/victorgentle/linux-user-creation-bash-script-4n42</link>
      <guid>https://dev.to/victorgentle/linux-user-creation-bash-script-4n42</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;
Learn more about HNG&lt;/p&gt;
&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&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;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The script must be run with root privileges.&lt;/li&gt;
&lt;li&gt;Ensure the input file with usernames and groups is formatted correctly and exists.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Input File Format
&lt;/h2&gt;

&lt;p&gt;Each line in the input file should be formatted as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;username&lt;span class="p"&gt;;&lt;/span&gt;group1,group2,...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;light&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nb"&gt;sudo&lt;/span&gt;,dev,www-data
idimma&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nb"&gt;sudo
&lt;/span&gt;mayowa&lt;span class="p"&gt;;&lt;/span&gt;dev,www-data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Script Steps
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Check Root Privileges:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;if [[ $EUID -ne 0 ]]&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;The script starts by checking if it is being run as the root user.&lt;/li&gt;
&lt;li&gt;This is necessary because creating users and modifying system files requires root privileges.&lt;/li&gt;
&lt;li&gt;This ensures that the script has the necessary permissions to perform its tasks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Validate Input File:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;if [[ -z "$1" ]]&lt;/code&gt;&lt;/strong&gt;; then
echo "Usage: $0 " &amp;gt;&amp;amp;2&lt;/li&gt;
&lt;li&gt;The script checks if the input file is provided as an argument and whether it exists.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Setup Logging and Password Files:
&lt;/h3&gt;

&lt;p&gt;The script sets up the log file and the password file. It ensures the directories exist and sets appropriate permissions for the password file&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;mkdir -p&lt;/code&gt;&lt;/strong&gt;: Ensures the directories exist.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;&amp;gt; "$LOG_FILE"&lt;/code&gt; and &lt;code&gt;&amp;gt; "$PASSWORD_FILE"&lt;/code&gt;&lt;/strong&gt;: Create or clear the log and password files.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;chmod 600 "$PASSWORD_FILE"&lt;/code&gt;&lt;/strong&gt;: Ensures that only the owner can read the password file, enhancing security.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Generate Passwords:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;generate_password() {
&amp;lt; /dev/urandom tr -dc A-Za-z0-9 | head -c12&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;The script defines a function to generate random passwords for the new users.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Log Messages:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;log_message() {
local message="$1"
echo "$(date '+%Y-%m-%d %H:%M:%S') : $message" &amp;gt;&amp;gt; "$LOG_FILE"&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;The script defines a function to log messages with timestamps.&lt;/li&gt;
&lt;li&gt;This provides a way to track actions performed by the script, useful for auditing and debugging.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Process Each Line:
&lt;/h3&gt;

&lt;p&gt;The script reads and processes each line from the input file, creating users and groups, setting up home directories, generating passwords, and logging actions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;IFS=';' read -r username groups&lt;/code&gt;&lt;/strong&gt;: Reads the username and groups from each line.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;username=$(echo "$username" | xargs)&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;groups=$(echo "$groups" | xargs)&lt;/code&gt;&lt;/strong&gt;: Removes leading/trailing whitespace.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;getent group "$username"&lt;/code&gt;&lt;/strong&gt;: Checks if the user's personal group exists; creates it if it doesn't.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;id "$username"&lt;/code&gt;&lt;/strong&gt;: Checks if the user already exists.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;password=$(generate_password)&lt;/code&gt;&lt;/strong&gt;: Generates a random password for the user.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;useradd -m -g "$username" -s /bin/bash "$username"&lt;/code&gt;&lt;/strong&gt;: Creates the user with the specified home directory and personal group.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;echo "$username:$password" | chpasswd&lt;/code&gt;&lt;/strong&gt;: Sets the user's password.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;IFS=',' read -r -a group_array &amp;lt;&amp;lt;&amp;lt; "$groups"&lt;/code&gt;&lt;/strong&gt;: Splits the groups into an array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;groupadd "$group"&lt;/code&gt;&lt;/strong&gt;: Creates additional groups if they don't exist.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;usermod -aG "$group" "$username"&lt;/code&gt;&lt;/strong&gt;: Adds the user to the additional groups.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Final Message:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;log_message "User creation script completed successfully"
echo "User creation script completed. Check the log file at $LOG_FILE and passwords at $PASSWORD_FILE."&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;The script logs a completion message and prints a final status to the console.&lt;/li&gt;
&lt;li&gt;Notifies the user of the script's completion and provides locations for the log and password files.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;Save the script as create_users.sh and make it executable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chmod&lt;/span&gt; +x create_users.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the script with the user file as an argument:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo&lt;/span&gt; ./create_users.sh &amp;lt;name-of-text-file&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Logs and Password Storage
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Log File: /var/log/user_management.log contains logs of all actions performed.&lt;/li&gt;
&lt;li&gt;Password File: /var/secure/user_passwords.csv stores the generated passwords securely.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Example User File
&lt;/h2&gt;

&lt;p&gt;Create a file named user_list.txt with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;light&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nb"&gt;sudo&lt;/span&gt;,dev,www-data
idimma&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nb"&gt;sudo
&lt;/span&gt;mayowa&lt;span class="p"&gt;;&lt;/span&gt;dev,www-data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Run the script
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo&lt;/span&gt; ./create_users.sh user_list.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This script ensures that users and groups are created as specified, with appropriate permissions and logging.&lt;/p&gt;

&lt;h2&gt;
  
  
  Learn More About HNG Internship
&lt;/h2&gt;

&lt;p&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;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://hng.tech/internship"&gt;Learn more about the HNG Internship program&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://hng.tech/hire"&gt;Explore hiring opportunities through HNG&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://hng.tech/premium"&gt;Check out HNG Premium services&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

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