The other answers here are great, assuming that the destination server already has the .pem
file's public key. In my case, I created a .pem
file to access an existing server, which was not configured for my newly generate key. So here is how I managed to set up my server for access using my newly created .pem
key:
Create the public key from the .pem
file: ssh-keygen -y -f my-new-key.pem > my-new-key.pub
Transfer the key to my server: scp -i ~/.ssh/my-old-key.pem /path/to/my-new-key.pub user@123.456.789:~/.ssh/
SSH into the server and attached the pem file as follows: ssh -i ~/.ssh/my-old.key.pem user@123.456.789
and run cat ~/.ssh/my-new-key.pub >> ~/.ssh/authorized_keys
Now, you can ssh into your server using your newly generated .pem
key! ie ssh -i ~/.ssh/my-new-key.pem user@123.456.789
.