Files
bootstrap-scripts/create-user.sh
2025-07-03 18:11:12 +00:00

53 lines
1.4 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
echo "🔐 Flexible User Creation Script"
echo "-------------------------------"
# Prompt for username
read -p "👤 Enter username to create (required): " USERNAME
if [[ -z "$USERNAME" ]]; then
echo "❌ Error: Username is required."
exit 1
fi
# Prompt for public key
read -p "🔑 Paste the SSH public key: " PUBKEY
if [[ -z "$PUBKEY" ]]; then
echo "❌ Error: Public key is required."
exit 1
fi
# Prompt for optional groups
read -p "👥 Enter comma-separated groups to add (e.g. sudo,docker): " GROUPS
IFS=',' read -ra GROUP_ARRAY <<< "$GROUPS"
# Create user
echo ""
echo "📋 Creating user: $USERNAME"
useradd -m -s /bin/bash "$USERNAME"
# Add user to groups if any provided
if [[ -n "$GROUPS" ]]; then
for group in "${GROUP_ARRAY[@]}"; do
group_trimmed=$(echo "$group" | xargs) # trim spaces
if [[ -n "$group_trimmed" ]]; then
echo " Adding $USERNAME to group: $group_trimmed"
usermod -aG "$group_trimmed" "$USERNAME"
fi
done
fi
# Set up SSH
echo "🔒 Setting up SSH access..."
mkdir -p /home/$USERNAME/.ssh
echo "$PUBKEY" > /home/$USERNAME/.ssh/authorized_keys
chmod 700 /home/$USERNAME/.ssh
chmod 600 /home/$USERNAME/.ssh/authorized_keys
chown -R $USERNAME:$USERNAME /home/$USERNAME/.ssh
# Done
echo ""
echo "✅ User '$USERNAME' created."
[[ -n "$GROUPS" ]] && echo "📦 Groups added: ${GROUP_ARRAY[*]}"
echo "🎉 SSH key installed and permissions secured."