# Series 4: Ep 6: Bash Scripting Essentials

> Episode of [Learn As I Learn - Technology, Product and Cybersecurity](https://podistic.com/podcast/learn-as-i-learn-technology-product-and-cybersecurity.md) · Published October 24, 2025 · Duration 12:26 · Episode 7, season 4

- **Listen on Podistic:** https://podistic.com/episode/series-4-ep-6-bash-scripting-essentials
- **Audio:** https://anchor.fm/s/1ccdac8c/podcast/play/108864439/https%3A%2F%2Fd3ctxlq1ktw2nl.cloudfront.net%2Fstaging%2F2025-8-26%2F408210961-44100-2-6d6f3ced33f59.m4a (audio/x-m4a)

## About this episode

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!"
