public
Created

Move local SSH keys to production machine

  • Download Gist
ssh-key-transfer.bash
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/bin/bash
# sends your local dev machines keys to the production machine for easy deployment + ssh
 
if [[ ! -e ~/.ssh/id_rsa.pub ]]; then
echo "Local keys do not exist, creating them..."
ssh-keygen -t rsa
fi
 
echo "username"
read username
echo "domain"
read domain
 
if [[ ! -e ~/.ssh/id_rsa.pub ]]; then
echo "You do not have a SSH key generated locally"
exit 1
fi
 
scp ~/.ssh/id_rsa.pub "$username"@"$domain":~/tmp_key
ssh "$username"@"$domain" 'if [[ ! -e ~/.ssh/ ]]; then mkdir -m 700 ~/.ssh; fi; if [[ ! -e ~/.ssh/authorized_keys ]]; then touch ~/.ssh/authorized_keys; fi; chmod 600 ~/.ssh/authorized_keys; cat ~/tmp_key >> ~/.ssh/authorized_keys; rm ~/tmp_key'
 
exit 0

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.