Skip to content

Instantly share code, notes, and snippets.

@davidmroth
Created September 27, 2018 03:47
Show Gist options
  • Save davidmroth/7bd2f1b6b1738fcaeff868cc5a9077a2 to your computer and use it in GitHub Desktop.
Save davidmroth/7bd2f1b6b1738fcaeff868cc5a9077a2 to your computer and use it in GitHub Desktop.
Use this script to regain access to a Lightsail server that has a lost private key
#!/bin/bash
# Lost your Lightsail private key (*.pem) file? Locked out of your server? Never fear, this script will fix that!!
# Step 1: Log into your Lighsail account
# Step 2: Create another in Lightsail
# Step 3: Generate a new public key from the private key you just downloaded `ssh-keygen -y -f .keys/newprivatekey.pem`
# Step 4: Copy the public key to the PRV_KEY bash variable below
# Step 5: Create a snapshot of your current server
# Step 6: Create an instance from the snapshot you just created
# Step 7: Copy this entire script to the 'Launch script' box
# Step 8: Create your instance
# Step 9: After your server is started, ssh using the user backdoor@whateveryouripaddressis
# Step 10: Prosper!
#Add public key here
PRV_KEY="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCd9ubQ+BI8k4nN7CJUT/VskscMs5vSR+Gfblahblahblahblahblab..."
useradd backdoor -s /bin/bash -m -G sudo
chpasswd << 'END'
backdoor:abc1234
END
mkdir -p /home/backdoor/.ssh/
echo $KEY | tee /home/backdoor/.ssh/authorized_keys
chmod 700 /home/backdoor/.ssh
chmod 600 /home/backdoor/.ssh/authorized_keys
chown backdoor.backdoor /home/backdoor/.ssh -R
#Remove backdoor user when done!
#sudo deluser backdoor --remove-all-files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment