Skip to content

Instantly share code, notes, and snippets.

@JuniYadi
Created September 18, 2024 08:12
Show Gist options
  • Save JuniYadi/1c78bb65c10cd6ba676500000a549cb5 to your computer and use it in GitHub Desktop.
Save JuniYadi/1c78bb65c10cd6ba676500000a549cb5 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Function to generate a random password
generate_random_password() {
tr -dc A-Za-z0-9 </dev/urandom | head -c 12
}
# Prompt for username
read -p "Enter username: " USERNAME
# Ask if the user wants to generate a random password
read -p "Do you want to generate a random password? (y/n): " GEN_PASS
if [[ "$GEN_PASS" =~ ^([yY][eE][sS]|[yY])$ ]]; then
PASSWORD=$(generate_random_password)
echo "Generated password: $PASSWORD"
else
# Prompt for password
read -s -p "Enter password: " PASSWORD
echo
fi
# Prompt for admin status
read -p "Should the user be an admin? (y/n): " ADMIN
# Create the user with the provided username
useradd $USERNAME -U -m -s /bin/bash
# Set the provided password for the user
echo "$USERNAME:$PASSWORD" | chpasswd
# If the user should be an admin, add to sudo group and configure sudoers
if [[ "$ADMIN" =~ ^([yY][eE][sS]|[yY])$ ]]; then
usermod -aG sudo $USERNAME
echo "$USERNAME ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/$USERNAME
chmod 0440 /etc/sudoers.d/$USERNAME
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment