3

I am using AWS EC2 with Ubuntu. I accidentally deleted the contents of ~/.ssh but still have my session open and still have my .pem and public keys backed up on my PC. Is there a way to recover my ~/.ssh content? I tried copying my public key, pasting it into a file with the same name on my instance and entering.

cat my-key.pkk >> authorized_keys 

But I cannot open another session with PuTTY.

1 Answer 1

7

You cannot recover it.

But as long as you have your session open, you can re-create the authorized_keys the same way, you would create it for the very first time on a normal *nix system.

  • create ~/.ssh folder, and set its permissions to 700:

    mkdir ~/.ssh
    chmod 700 ~/.ssh 
    
  • run PuTTYgen on your local Windows machine, load your key pair (.pem or .ppk) into it, copy the contents of Public key for pasting into OpenSSH authorized_keys file box to the clipboard;

  • type cat > ~/.ssh/authorized_keys in your PuTTY terminal;
  • right click the terminal window to paste the line copied from PuTTYgen;
  • press Enter and Ctrl+C;
  • set permissions of ~/.ssh/authorized_keys to 600:

    chmod 600 ~/.ssh/authorized_keys
    

Now you should be able to authenticate a new session using your private key.

See also my guide for Set up SSH public key authentication.

1
  • 1
    It worked! Thank you times 100. Commented Feb 17, 2018 at 21:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .