EPISODE · Oct 24, 2025 · 12 MIN
Series 4: Ep 6: Bash Scripting Essentials
from Learn As I Learn · host Akanksha Pathak
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#VariablesUSERNAME="Jason" # User account namePASSWORD="P@ssw0rd" # User passwordGROUP="developers" # Custom group nameSSH_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 existsif id "$USERNAME" &>/dev/null; then echo "Error: User '$USERNAME' already exists!" exit 1fi#Step 2: Create user and set passwordecho "Creating user '$USERNAME'..."useradd -m -s /bin/bash "$USERNAME" if [ $? -ne 0 ]; then echo "Error: Failed to create user '$USERNAME'" exit 1fiecho "$USERNAME:$PASSWORD" | chpasswd echo "Password set for user '$USERNAME'."#Step 3: Add user to sudoersecho "Granting sudo access to '$USERNAME'..."usermod -aG sudo "$USERNAME" if [ $? -ne 0 ]; then echo "Error: Failed to add '$USERNAME' to sudoers" exit 1fi#Step 4: Create custom group and add userecho "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 1fi#Step 5: Setup SSH key-based authenticationecho "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 1fiecho "SSH keys configured for '$USERNAME'."#Step 6: Set password expiry to 30 daysecho "Setting password expiry policy for '$USERNAME'..."chage -M 30 "$USERNAME" if [ $? -ne 0 ]; then echo "Error: Failed to set password expiry" exit 1fi#Step 7: Log activity to /var/log/user_setup.logLOG_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 1fi#Step 8: Confirmation Messageecho "User '$USERNAME' created and configured successfully!"
What this episode covers
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#VariablesUSERNAME="Jason" # User account namePASSWORD="P@ssw0rd" # User passwordGROUP="developers" # Custom group nameSSH_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 existsif id "$USERNAME" &>/dev/null; then echo "Error: User '$USERNAME' already exists!" exit 1fi#Step 2: Create user and set passwordecho "Creating user '$USERNAME'..."useradd -m -s /bin/bash "$USERNAME" if [ $? -ne 0 ]; then echo "Error: Failed to create user '$USERNAME'" exit 1fiecho "$USERNAME:$PASSWORD" | chpasswd echo "Password set for user '$USERNAME'."#Step 3: Add user to sudoersecho "Granting sudo access to '$USERNAME'..."usermod -aG sudo "$USERNAME" if [ $? -ne 0 ]; then echo "Error: Failed to add '$USERNAME' to sudoers" exit 1fi#Step 4: Create custom group and add userecho "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 1fi#Step 5: Setup SSH key-based authenticationecho "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 1fiecho "SSH keys configured for '$USERNAME'."#Step 6: Set password expiry to 30 daysecho "Setting password expiry policy for '$USERNAME'..."chage -M 30 "$USERNAME" if [ $? -ne 0 ]; then echo "Error: Failed to set password expiry" exit 1fi#Step 7: Log activity to /var/log/user_setup.logLOG_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 1fi#Step 8: Confirmation Messageecho "User '$USERNAME' created and configured successfully!"
NOW PLAYING
Series 4: Ep 6: Bash Scripting Essentials
No transcript for this episode yet
Similar Episodes
Mar 26, 2026 ·1m
Mar 19, 2026 ·34m
Mar 3, 2026 ·44m
Feb 21, 2026 ·30m
Feb 18, 2026 ·11m
Feb 11, 2026 ·45m