Skip to content

Instantly share code, notes, and snippets.

@pnsinha
Created March 4, 2025 05:44
Show Gist options
  • Save pnsinha/8274b6047b9238db2385c87ba7379637 to your computer and use it in GitHub Desktop.
Save pnsinha/8274b6047b9238db2385c87ba7379637 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Variables
USERNAME="pnsinha"
SSH_KEY="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9p/5ob6qOD55c+DdYTVDLpanxoCULNJAaNZIr1rNv9bdw7yaq1Ow3TbYrLmcV0ZVKnCQM/ZRa//tM/ybx7zbFQkh8aKwZog0f84+ImeqNoO5NfodoiGbQMEv+OKxXo4UaXO3lJIVIe1z5brBatxSOuTHM0oIeWowUKPURX5oCYmeekivseWFHJ+DGB+cmlmtntR8G1pXrMFv7+T+j3H8F+PI6RREZvftUj9eZiGKstFIGuSRzkCkqwWBXzwBrl8FZgZ/a0eA2V59PHt0C3XOMYGfBXBrKinV79Poc9Sddn8sodnoO8t46CjROR58+ADGSZRXjtH+84Hk3oXYzbXCZ parma@DESKTOP-UCGIS"
# Create a new user
echo "Creating user $USERNAME..."
sudo useradd -m -s /bin/bash "$USERNAME"
# Set a password for the new user (optional, interactive)
echo "Setting password for $USERNAME..."
sudo passwd "$USERNAME"
# Create .ssh directory and authorized_keys file for the new user
echo "Setting up SSH key for $USERNAME..."
sudo mkdir -p /home/"$USERNAME"/.ssh
echo "$SSH_KEY" | sudo tee /home/"$USERNAME"/.ssh/authorized_keys > /dev/null
# Set correct ownership and permissions
sudo chown -R "$USERNAME":"$USERNAME" /home/"$USERNAME"/.ssh
sudo chmod 700 /home/"$USERNAME"/.ssh
sudo chmod 600 /home/"$USERNAME"/.ssh/authorized_keys
# Add the user to the sudo (or wheel) group
echo "Granting sudo privileges to $USERNAME..."
if grep -q "^sudo:" /etc/group; then
sudo usermod -aG sudo "$USERNAME"
else
sudo usermod -aG wheel "$USERNAME"
fi
# Add the SSH key to root's authorized_keys
echo "Adding SSH key to root's authorized_keys..."
sudo mkdir -p /root/.ssh
echo "$SSH_KEY" | sudo tee -a /root/.ssh/authorized_keys > /dev/null
sudo chmod 700 /root/.ssh
sudo chmod 600 /root/.ssh/authorized_keys
# Completion message
echo "User $USERNAME created, SSH key added, and sudo privileges granted."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment