Files
bootstrap-scripts/create-user.sh
2025-07-03 16:49:46 +00:00

44 lines
1.1 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 "🔐 Ansible User Setup Script"
echo "-----------------------------"
# Username prompt
read -p "👤 Enter username to create (default: ansible): " USERNAME
USERNAME=${USERNAME:-ansible}
# SSH key prompt
read -p "🔑 Paste the SSH public key: " PUBKEY
# Group prompt
read -p "👥 Enter comma-separated groups to add (default: sudo, e.g. sudo,docker): " GROUPS
GROUPS=${GROUPS:-sudo}
# Convert group list into array
IFS=',' read -ra GROUP_ARRAY <<< "$GROUPS"
# Create user
echo ""
echo "📋 Creating user: $USERNAME"
useradd -m -s /bin/bash "$USERNAME"
# Add user to each group
for group in "${GROUP_ARRAY[@]}"; do
echo " Adding $USERNAME to group: $group"
usermod -aG "$group" "$USERNAME"
done
# 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."
echo "📦 Groups added: ${GROUP_ARRAY[*]}"
echo "🎉 SSH key installed and permissions secured."