#!/bin/bash # =============================== # Safe User Creation Script # =============================== # Ask for username read -p "Enter the username to create: " NEW_USER # Check if the user exists if id "$NEW_USER" &>/dev/null; then echo "User '$NEW_USER' already exists." read -p "Do you want to reset the password for this user? (y/n): " RESET_PASS if [ "$RESET_PASS" != "y" ]; then echo "Skipping password reset. Continuing to ensure admin privileges..." fi else # Create user if it does not exist sudo useradd -m -s /bin/bash "$NEW_USER" echo "User '$NEW_USER' created successfully." fi # Ask for password (hidden) read -s -p "Enter the password for $NEW_USER: " NEW_PASS echo read -s -p "Confirm the password: " CONFIRM_PASS echo if [ "$NEW_PASS" != "$CONFIRM_PASS" ]; then echo "Passwords do not match. Exiting." exit 1 fi # Set or reset password echo "$NEW_USER:$NEW_PASS" | sudo chpasswd echo "Password set successfully." # Add user to admin group if [ -f /etc/debian_version ]; then sudo usermod -aG sudo "$NEW_USER" elif [ -f /etc/redhat-release ]; then sudo usermod -aG wheel "$NEW_USER" else echo "Unknown Linux distro. Add $NEW_USER to admin group manually." fi # Confirm echo "---------------------------------------" echo "User '$NEW_USER' is ready!" echo "Home directory: /home/$NEW_USER" echo "Admin privileges granted." echo "Switch to user: su - $NEW_USER" echo "---------------------------------------"