<?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: Christian ochenehi Peter</title>
    <description>The latest articles on DEV Community by Christian ochenehi Peter (@christian_ochenehipeter_).</description>
    <link>https://dev.to/christian_ochenehipeter_</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%2F1717452%2Fecf0796b-2ebd-4432-afe0-bca849bd9dfa.jpg</url>
      <title>DEV Community: Christian ochenehi Peter</title>
      <link>https://dev.to/christian_ochenehipeter_</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/christian_ochenehipeter_"/>
    <language>en</language>
    <item>
      <title>Using a Bash script to Automate the Creation of Users and Groups</title>
      <dc:creator>Christian ochenehi Peter</dc:creator>
      <pubDate>Tue, 02 Jul 2024 22:15:53 +0000</pubDate>
      <link>https://dev.to/christian_ochenehipeter_/using-a-bash-script-to-automate-the-creation-of-users-and-groups-2i8e</link>
      <guid>https://dev.to/christian_ochenehipeter_/using-a-bash-script-to-automate-the-creation-of-users-and-groups-2i8e</guid>
      <description>&lt;p&gt;is a typical responsibility for a DevOps engineer to add, modify, and remove users and groups. Time can be saved and mistakes can be decreased by automating this procedure, particularly when onboarding numerous new developers. This tutorial will guide you through the building of a Bash script that can be used to automatically create users and their groups, create home directories, generate random passwords, and log all activity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Goals&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make users and the groups they belong to&lt;/li&gt;
&lt;li&gt;Users can be added to designated groups. Create home directories and grant the necessary access. Create random passwords and save them safely. &lt;/li&gt;
&lt;li&gt;Keep track of every action for auditing needs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Requirements&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;• Input File: A text file with the format username;group1,group2 that contains usernames and groups. &lt;br&gt;
• Log File: A file used to keep track of every action. &lt;br&gt;
• Password File: A file where generated passwords are safely kept.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now let's get started.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;&lt;strong&gt;"shebang"&lt;/strong&gt;&lt;/em&gt; is the first line that we write at the beginning of every shell script we write; it sounds catchy, doesn't it?&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

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

&lt;/div&gt;



&lt;p&gt;An executable file is indicated by the existence of a &lt;em&gt;&lt;strong&gt;shebang&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;We can now discuss the juicy specifics now that it is out of the way.&lt;/p&gt;

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

&lt;p&gt;The script begins by specifying the locations of the password and log files (you are free to give them any names you choose):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LOGFILE="/var/log/user_management.log"
PASSWORD_FILE="/var/secure/user_passwords.csv"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Check the File Input&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The script determines whether an input file has been supplied as an argument:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [ -z "$1" ]; then
  echo "Usage: $0 &amp;lt;name-of-text-file&amp;gt;"
  exit 1
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The script exits with a usage message instructing our users on how to utilize our script if no file is supplied.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Make Password and Log Files&lt;/strong&gt;&lt;br&gt;
This little piece of code creates the required files and directories and sets the right permissions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir -p /var/secure
touch $LOGFILE $PASSWORD_FILE
chmod 600 $PASSWORD_FILE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We make a directory called "/var/secure/" which will keep our passwords. After that we create the two files that were defined above for logging and saving passwords. Modifying our $PASSWORD_FILE with chmod 600 will ensure that only the user with appropriate permissions can view it(which happens to be the current user we are logged in as).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function to Generate Random Passwords&lt;/strong&gt;&lt;br&gt;
We will create a function to provide our various users with secure, random passwords.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;generate_random_password() {
    local length=${1:-10} # Default length is 10 if no argument is provided
    tr -dc 'A-Za-z0-9!?%+=' &amp;lt; /dev/urandom | head -c $length
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function accepts an argument specifying the desired password length; if none is supplied, it defaults to 10.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;tr -dc 'A-Za-z0-9!?%+=': This command is used to translate and delete characters that are not in the regex A-Za-z0-9!?%+=&lt;/li&gt;
&lt;li&gt;&amp;lt; /dev/urandom: uses the Linux kernel's random number generator and passes the result to the command above.&lt;/li&gt;
&lt;li&gt;| head -c $length: outputs the length of the random string specified.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Function of Logging&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;log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" &amp;gt;&amp;gt; $LOGFILE
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;logs the message's date and time in the $LOGFILE after receiving an argument.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Make a User Function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The function create_user() manages the process of creating users and their groups. Username and groups are the two arguments it requires.&lt;br&gt;
&lt;/p&gt;

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

  if getent passwd "$username" &amp;gt; /dev/null; then
    log_message "User $username already exists"
  else
    useradd -m $username
    log_message "Created user $username"
  fi

  # Add user to specified groupsgroup
  groups_array=($(echo $groups | tr "," "\n"))

  for group in "${groups_array[@]}"; do
    if ! getent group "$group" &amp;gt;/dev/null; then
      groupadd "$group"
      log_message "Created group $group"     
    fi
    usermod -aG "$group" "$username"
    log_message "Added user $username to group $group"
  done

  # Set up home directory permissions
  chmod 700 /home/$username
  chown $username:$username /home/$username
  log_message "Set up home directory for user $username" 

  # Generate a random password
  password=$(generate_random_password 12) 
  echo "$username:$password" | chpasswd
  echo "$username,$password" &amp;gt;&amp;gt; $PASSWORD_FILE
  log_message "Set password for user $username"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Let's dissect it together.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Verifying the Existence of the User
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if id "$username" &amp;gt; /dev/null; then
  log_message "User $username already exists"
else
  useradd -m $username
  log_message "Created user $username"
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our script logs users if they already exist and then continues; if not, a new user is generated and signed in.&lt;/p&gt;

&lt;p&gt;NOTE: In order to prevent it from interfering with our logs, /dev/null is forwarding the response to null.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;NOTE: Unix handles the creation of personal groups for us when we establish a new user, so we won't be creating them manually for each user.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User Addition to Identified Groups
After that, we continue by adding the users to their respective groups.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;groups_array=($(echo $groups | tr "," "\n"))
for group in "${groups_array[@]}"; do
  if ! getent group "$group" &amp;gt; /dev/null; then
    groupadd "$group"
    log_message "Created group $group"   
  fi
  usermod -aG "$group" "$username"
  log_message "Added user $username to group $group"
done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using commas, the code sample divides the groups string and stores the pieces in a groups_array variable. Next, it iterates over each group, adding the user and making sure the group is created if it doesn't already exist:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating Permissions for the Home Directory
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chmod 700 /home/$username
chown $username:$username /home/$username
log_message "Set up home directory for user $username" 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;chmod 700 /home/$username:: Sets the home directory permissions so only the user has full access (read, write, execute).&lt;/p&gt;

&lt;p&gt;chown $username:$username /home/$username: Changes the ownership of the home directory to the specified user and their group.&lt;/p&gt;

&lt;p&gt;echo "Set up home directory for user $username" | tee -a $LOGFILE: Logs a message indicating the home directory setup to a specified log file.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Giving Every User A Random Password
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;password=$(generate_random_password 12) 
echo "$username:$password" | chpasswd
echo "$username,$password" &amp;gt;&amp;gt; $PASSWORD_FILE
log_message "Set password for user $username"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This little code generates a new password using the function we previously built, updates the user's password to the produced password, saves the username and password to the $PASSWORD_FILE, and then logs a message confirming the change was successful.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examining the Input File&lt;/strong&gt;&lt;br&gt;
The script reads the input file line by line, providing the arguments $username and $groups to the create_user() function on each line:&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
  create_user "$username" "$groups"
done &amp;lt; "$1"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Now, Compile Everything...&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 location
LOGFILE="/var/log/user_management.log"
PASSWORD_FILE="/var/secure/user_passwords.csv"

# Check if the input file is provided
if [ -z "$1" ]; then
  echo "Error: No file was provided"
  echo "Usage: $0 &amp;lt;name-of-text-file&amp;gt;"
  exit 1
fi

# Create log and password files
mkdir -p /var/secure
touch $LOGFILE $PASSWORD_FILE
chmod 600 $PASSWORD_FILE

generate_random_password() {
    local length=${1:-10} # Default length is 10 if no argument is provided
    LC_ALL=C tr -dc 'A-Za-z0-9!?%+=' &amp;lt; /dev/urandom | head -c $length
}

# Function to create a user
create_user() {
  local username=$1
  local groups=$2

  if getent passwd "$username" &amp;gt; /dev/null; then
    echo "User $username already exists" | tee -a $LOGFILE
  else
    useradd -m $username
    echo "Created user $username" | tee -a $LOGFILE
  fi

  # Add user to specified groupsgroup
  groups_array=($(echo $groups | tr "," "\n"))

  for group in "${groups_array[@]}"; do
    if ! getent group "$group" &amp;gt;/dev/null; then
      groupadd "$group"
      echo "Created group $group" | tee -a $LOGFILE      
    fi
    usermod -aG "$group" "$username"
    echo "Added user $username to group $group" | tee -a $LOGFILE
  done

  # Set up home directory permissions
  chmod 700 /home/$username
  chown $username:$username /home/$username
  echo "Set up home directory for user $username" | tee -a $LOGFILE

  # Generate a random password
  password=$(generate_random_password 12) 
  echo "$username:$password" | chpasswd
  echo "$username,$password" &amp;gt;&amp;gt; $PASSWORD_FILE
  echo "Set password for user $username" | tee -a $LOGFILE
}

# Read the input file and create users
while IFS=';' read -r username groups; do
  create_user "$username" "$groups"
done &amp;lt; "$1"

echo "User creation process completed." | tee -a $LOGFILE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In summary&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This script offers a streamlined method for managing the creation of users and groups while making sure that all required actions are taken securely and recorded for audit purposes. SysOps engineers can save time and lower the possibility of mistakes during user onboarding by automating these procedures.&lt;/p&gt;

&lt;p&gt;For additional information and to begin your programming career, go to &lt;a href="https://hng.tech/internship"&gt;https://hng.tech/internship&lt;/a&gt; or &lt;a href="https://hng.tech/premium"&gt;https://hng.tech/premium&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Please get in touch if you have any queries or ideas for enhancements. Cheers to automation!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Automating User and Group Management on Linux with Bash</title>
      <dc:creator>Christian ochenehi Peter</dc:creator>
      <pubDate>Tue, 02 Jul 2024 16:12:22 +0000</pubDate>
      <link>https://dev.to/christian_ochenehipeter_/automating-user-and-group-management-on-linux-with-bash-10cb</link>
      <guid>https://dev.to/christian_ochenehipeter_/automating-user-and-group-management-on-linux-with-bash-10cb</guid>
      <description>&lt;p&gt;Managing user accounts and groups is a fundamental aspect of system administration. In dynamic environments, such as those in software development companies, the ability to automate this process can save time and reduce errors. This article explains a Bash script designed to automate the creation of user accounts and groups based on a predefined list, ensuring each user has a secure, randomly generated password.&lt;br&gt;
Understanding the Script&lt;/p&gt;

&lt;p&gt;The script create_users.sh takes a text file as input, where each line specifies a username and associated groups, separated by a semicolon. It performs the following actions:&lt;br&gt;
User and Group Creation: For each line in the input file, the script creates a user and a personal group with the same name. It also adds the user to specified groups, creating those groups if they don't already exist.&lt;/p&gt;

&lt;p&gt;Password Management: It generates a secure, random password for each user, sets it, and stores it in a secure file, ensuring that only the root user can access it.&lt;/p&gt;

&lt;p&gt;Logging: All actions are logged to /var/log/user_management.log, providing a clear audit trail.&lt;/p&gt;

&lt;p&gt;Why This Approach?&lt;/p&gt;

&lt;p&gt;This script emphasizes security and accountability. By generating random passwords and securing the password file, it ensures that user accounts are protected from the outset. Logging actions allow system administrators to track changes and troubleshoot issues.&lt;/p&gt;

&lt;p&gt;Deployment and Usage&lt;/p&gt;

&lt;p&gt;To use the script, simply run it as root and pass the path to your input file as an argument:&lt;br&gt;
sudo bash create_users.sh usernames.txt&lt;br&gt;
Ensure that your input file follows the format username;group1,group2. &lt;/p&gt;

&lt;p&gt;Learn More&lt;/p&gt;

&lt;p&gt;For those interested in further automating and managing Linux systems, the &lt;a href="https://hng.tech/internship"&gt;https://hng.tech/internship&lt;/a&gt; offers valuable resources and opportunities. Whether you're looking to hire tech talent or enhance your skills, &lt;a href="https://hng.tech/hire"&gt;https://hng.tech/hire&lt;/a&gt; platform and premium courses provide a wealth of information and support.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;Automating user and group management not only streamlines system administration tasks but also enhances security and efficiency. By leveraging simple yet powerful Bash scripts, sysadmins can ensure their systems are well-organized and secure.&lt;/p&gt;

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