Suppose I have created ssh keys.The server has private key and I use a ubuntu machine which has public key to connect to server. Now my public key gets stolen and the hacker know the IP of server which has private key. Then can hacker be able to connect to server using the stolen public key using ssh command?
If no, then why?
My understanding is since the same public key is used by hacker, the server will never know from which machine the ssh request came from. So the server should will validate the public key and allow to login.
Please correct if I am wrong.
2
Answers
As written, the answer to your question is ‘no, the hacker cannot connect to the server using the public key you have obtained from the server’. They would also need the user’s private key or password.
For more completeness, in case the terminology has been confused:
There can be two sets of public+private keys used when you SSH to a server from your workstation.
known_hosts
in your~/.ssh
directory). If the hacker gets the server private key, they can pretend to be the server. If the hacker gets the server public key, they can only verify the identity of the server to themselves.~/.ssh/id_rsa
or similar. The public key will likely be the same filename but with.pub
on the end. The contents of the public key will be also be stored on the server in the file~/.ssh/authorized_keys
for the user on the server that the key authenticates. If the hacker gets this private key, they can pretend to be you. If the hacker gets this public key, they can only verify that a connection comes from you. It is possible to do ssh without this workstation-side public+private key, but you would instead be using a password to authenticate to the server rather than a key.So, the first question you would ask yourself is whether you use a password or a key on the workstation to authenticate to the ssh server. Then whether the hacker has stolen the public key or the private key from either server or workstation.
If the hacker has either public key then this is not generally considered a problem (hence the name ‘public’).
However, if the hacker has either private key then it is a problem, and you should change that key (on either the server or your workstation). Depending on the key that they’ve potentially stolen, you will want to remove the server’s public key from your workstation
~/.ssh/known_hosts
file on your workstation, or your public key from the server (~/.ssh/authorized_keys
).Remember that if the hacker has the server private key, then anyone who has the associated public key in their
known_hosts
file will still trust the old private key (i.e. a server the hacker creates) even once you’ve changed the key on the real server, so you will want to make sure theknown_hosts
files are fixed everywhere. And if they’ve stolen your private key from the workstation then any server that has the associated public key in it’sauthorized_keys
file will still trust the old key even once you’ve changed it on your workstation, so you will want to replace the public key on all servers that you use that private key on.By definition, private keys are private (i.e. secret) and public key are public (i.e. not secret).
A hacker doesn’t need to steal a machine’s public key, they can simply ask for it. For example: