How to recover ssh private key after hard disk crash and MAC OS X update
and how to get access to server without id_rsa file or password after hard disk crash cloning old disk
After I spent quite a while without access to my server because my hard disk crashed I now finally regained access and control over my server.
The issue was that I had set the CENTOS server to not accept any password authentication and to only accept ssh-login from one passphrase protected RSA key file on my machine.
My hard disk crashed and had some physical damage – a few sections were not readable anymore which caused the system to be slower and slower and finally made it impossible for my MAC to reboot. DISK repair set that the disk is not reparable.
I then got a new hard disk, got all my files copied from my backup and continued as usual. That’S what I thought at least. Everything worked fine except for my SSH connection with my server. For some reason it didn’t work even though I had the key files on my disk and they were in the correct MAC specific location ~/.ssh
The error message I got was
I had a look at this post about possible solutions to the PEM_read_PrivateKey failed dilemma
but it didn’t help me any further.
i checked ownership and permissions — all seemed to be OK with 600 on id_rsa and 700 on id_rsa.pub
The key was listed in authorized_keys — all seemed to be perfect. Then I read that apparently some newer MAC OS versions seem to have issues with keys created on older versions. As I had a newer MAC OS on my new hard disk and did not clone right away from a backup this could have been it …
I was clueless and thought – ok there is no solution to get access to my server again or to recover my RSA private key. Restoring the private key from my public key obviously was not possible either.
Then I thought I might as well have a look at my old supposedly broken hard disk again and see if I can boot from it as an external USB drive holding down the Options key when booting the MAC. It didn’t work. The hdd would just crash.
Finally I decided to clone my broken hard disk — just to give it a shot and see if I might be able to boot from the cloned disk and login to my server with SSH.
It took about 6 hours and I got several error messages saying the disk was having errors and not reparable. Almost all my hope vanished.
Anyway I gave it a try once it was cloned – and it worked!
i got onto my old system having cloned a broken hard disk – recovered access to my server and enabled Password Login in
Then got onto my normal HDD – connected with password, created a new set of key files with keygen via Terminal and got the whole thing working. Once it all was ok I turned off password authentication again.
Conclusion: If you use private keys to access your server and have no password authentication activated and your hard disk crashes — first gain access to the server from a 1-1 cloned copy backup and activate password access before updating your OS on a new hard disk.