Skip to content

Instantly share code, notes, and snippets.

@eswarijayakumar
Created July 1, 2023 16:42
Show Gist options
  • Save eswarijayakumar/2dc66ddef4aec95ee4e5961b9b251fce to your computer and use it in GitHub Desktop.
Save eswarijayakumar/2dc66ddef4aec95ee4e5961b9b251fce to your computer and use it in GitHub Desktop.
Script to automate Ubuntu User creation with SSH Key access
#!/bin/sh
public_key="<provide your public key here>"
sudo adduser --disabled-password --gecos "" testuser
sudo usermod -aG sudo testuser
sudo mkdir -p /home/testuser/.ssh && sudo touch /home/testuser/.ssh/authorized_keys
sudo chmod 700 /home/testuser/.ssh && sudo chmod 600 /home/testuser/.ssh/authorized_keys
sudo chown -R testuser /home/testuser/.ssh
sudo sh -c "echo $public_key > /home/testuser/.ssh/authorized_keys"
sudo sh -c "echo 'testuser ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/testuser-user"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment