Home
Categories
EXPLORE
True Crime
Comedy
Society & Culture
Business
News
Sports
TV & Film
About Us
Contact Us
Copyright
© 2024 PodJoint
Podjoint Logo
US
00:00 / 00:00
Sign in

or

Don't have an account?
Sign up
Forgot password
https://is1-ssl.mzstatic.com/image/thumb/Podcasts123/v4/c6/69/cc/c669cc1e-30b6-2ee8-57a2-1a356338f475/mza_5498275658329189582.jpg/600x600bb.jpg
Learn As I Learn - Technology, Product and Cybersecurity
Akanksha Pathak
57 episodes
4 days ago
No time to learn? Take out few minutes from your life and learn new things every day! Please subscribe to start learning for FREE now!
Show more...
Education
RSS
All content for Learn As I Learn - Technology, Product and Cybersecurity is the property of Akanksha Pathak and is served directly from their servers with no modification, redirects, or rehosting. The podcast is not affiliated with or endorsed by Podjoint in any way.
No time to learn? Take out few minutes from your life and learn new things every day! Please subscribe to start learning for FREE now!
Show more...
Education
https://d3t3ozftmdmh3i.cloudfront.net/production/podcast_uploaded/4732411/4732411-1587053849816-c6c798440b069.jpg
Series 4: Ep 6: Bash Scripting Essentials
Learn As I Learn - Technology, Product and Cybersecurity
12 minutes 26 seconds
1 week ago
Series 4: Ep 6: Bash Scripting Essentials

Master automation in Linux with Bash scripts! Discover how to create and debug scripts for user setup, including creating new users, setting passwords, adding them to groups, configuring SSH key-based login, and setting password expiry. We’ll also cover testing and verification.

Script:

#!/bin/bash
#Variables
USERNAME="Jason" # User account name
PASSWORD="P@ssw0rd" # User password
GROUP="developers" # Custom group name
SSH_DIR="/home/$USERNAME/.ssh"
PUB_KEY="ssh-rsa AAAAB3...your-public-key... user@kali" # Replace with your actual public key

#Step 1: Check if user already exists
if id "$USERNAME" &>/dev/null; then
  echo "Error: User '$USERNAME' already exists!"
  exit 1
fi

#Step 2: Create user and set password
echo "Creating user '$USERNAME'..."
useradd -m -s /bin/bash "$USERNAME"
if [ $? -ne 0 ]; then
  echo "Error: Failed to create user '$USERNAME'"
  exit 1
fi
echo "$USERNAME:$PASSWORD" | chpasswd
echo "Password set for user '$USERNAME'."

#Step 3: Add user to sudoers
echo "Granting sudo access to '$USERNAME'..."
usermod -aG sudo "$USERNAME"
if [ $? -ne 0 ]; then
  echo "Error: Failed to add '$USERNAME' to sudoers"
  exit 1
fi

#Step 4: Create custom group and add user
echo "Creating group '$GROUP' and adding user..."
groupadd "$GROUP" 2>/dev/null
usermod -aG "$GROUP" "$USERNAME"
if [ $? -ne 0 ]; then
  echo "Error: Failed to add '$USERNAME' to group '$GROUP'"
  exit 1
fi

#Step 5: Setup SSH key-based authentication
echo "Setting up SSH key-based authentication..."
mkdir -p "$SSH_DIR"
echo "$PUB_KEY" > "$SSH_DIR/authorized_keys"
chmod 600 "$SSH_DIR/authorized_keys"
chmod 700 "$SSH_DIR"
chown -R "$USERNAME:$USERNAME" "$SSH_DIR"
if [ $? -ne 0 ]; then
  echo "Error: Failed to set up SSH keys"
  exit 1
fi
echo "SSH keys configured for '$USERNAME'."

#Step 6: Set password expiry to 30 days
echo "Setting password expiry policy for '$USERNAME'..."
chage -M 30 "$USERNAME"
if [ $? -ne 0 ]; then
  echo "Error: Failed to set password expiry"
  exit 1
fi

#Step 7: Log activity to /var/log/user_setup.log
LOG_FILE="/var/log/user_setup.log"
echo "$(date) - User '$USERNAME' created and configured" >> "$LOG_FILE"
if [ $? -ne 0 ]; then
  echo "Error: Failed to write log to $LOG_FILE"
  exit 1
fi

#Step 8: Confirmation Message
echo "User '$USERNAME' created and configured successfully!"

Learn As I Learn - Technology, Product and Cybersecurity
No time to learn? Take out few minutes from your life and learn new things every day! Please subscribe to start learning for FREE now!