locked out of my server Permission denied (publickey)

I was connected to my server. i edited the /root/.ssh/authorized_keys and added
at the start of the public key and
at the end of the public key. I mistakenly lost connection to the host and now despite having id_rsa file, i am not able to connect to the host.
ssh -i /home/me/id_rsa root@ip
It is showing me:
Enter passphrase for key '/home/me/id_rsa':
root@ip: Permission denied (publickey).
How do I fix this? How do I connect the the server?

Asked By: Huzaifa Malik


/root/.ssh/authorized_keys and added

autorized_keys is very clearly for the public key fingerprints that should have access. You added your private key, and that doesn’t fit the sense nor the format of that file at all. This has two consequences:

  1. You broke the file, making it unparseable to SSH
  2. You copied your private key, which should never leave your control, to a server somewhere else. We call this state compromised, and if you used it (or rather, its matching public key) elsewhere already, you now need to go there and remove access to them.

How do I fix this? How do I connect the the server?

You simply can’t via SSH anymore, unless there’s a password set and you know it. This is good: you locked the front door to your house. Now it’s closed.

Whether there’s another way to connect to your server is something that we can’t tell you. For example, you might not be the only admin, and another admin might still have access through their user account (seeing you broke SSH login for root).

If this is a cloud server, the usual course of action here would be to your management console, download a backup image of your server, use that as template / reference to provision a new VM, with which you replace the inaccessible one.

Answered By: Marcus Müller
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.