Skip to content

Instantly share code, notes, and snippets.

@10h30
Created May 13, 2025 05:36
Show Gist options
  • Select an option

  • Save 10h30/a9bf9469b8ec82bb2f12eec7f8713b67 to your computer and use it in GitHub Desktop.

Select an option

Save 10h30/a9bf9469b8ec82bb2f12eec7f8713b67 to your computer and use it in GitHub Desktop.
Quickly setup new user on new Oracle VPS
#!/bin/bash
set -e
# Parse command-line argument
while [[ "$#" -gt 0 ]]; do
case $1 in
--user) USERNAME="$2"; shift ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done
# Check if username was provided
if [[ -z "$USERNAME" ]]; then
echo "Usage: $0 --user <username>"
exit 1
fi
PUBKEY_SRC="/home/ubuntu/.ssh/authorized_keys"
USER_HOME="/home/$USERNAME"
echo "Creating user: $USERNAME"
# Create the user without a password
sudo adduser --disabled-password --gecos "" "$USERNAME"
# Add to sudo group
sudo usermod -aG sudo "$USERNAME"
# Copy SSH authorized keys
sudo mkdir -p "$USER_HOME/.ssh"
sudo cp "$PUBKEY_SRC" "$USER_HOME/.ssh/authorized_keys"
sudo chown -R "$USERNAME:$USERNAME" "$USER_HOME/.ssh"
sudo chmod 700 "$USER_HOME/.ssh"
sudo chmod 600 "$USER_HOME/.ssh/authorized_keys"
# Allow passwordless sudo
echo "$USERNAME ALL=(ALL) NOPASSWD:ALL" | sudo tee "/etc/sudoers.d/$USERNAME"
sudo chmod 440 "/etc/sudoers.d/$USERNAME"
echo "User $USERNAME created and configured successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment