I have accidentally lost my ~/.ssh/known_hosts file. on my server, I had disabled ssh with a password. I still able to plug a screen on my server and log in with the keyboard. How can I remove last ssh key (from my server) since it is not useful anymore? How can I regenerate new ssh private key transfer into my laptop (Linux)?

Thank you in advance

ssh -V = OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g ...
  • let me get you right you want to remove known_host keys, if so just delete that folder. The second part I don't really follow... May 19, 2017 at 18:45
  • @George I want to delete my ssh public key from the server (since I lost private), and recreate new public (for my server) and private key (for my laptop) to be able to connect again via ssh from my laptop to my server.
    – dmx
    May 19, 2017 at 18:52
  • @George I have disabled password connection ... Do I have to enable it or is there another way?
    – dmx
    May 19, 2017 at 19:12
  • @George this is the wrong thing to do.
    – guntbert
    May 19, 2017 at 19:16
  • 1
    @guntbert, yes you are right I totally forgot about the authorized_keys. dmx please use the answer below... May 19, 2017 at 19:18

1 Answer 1


The key will be in ~/.ssh/authorized_keys

You can delete the line for the old key with the editor of your choice.

You can generate a new private key by running the following command on a client machine.


Depending on which algorithm was used add the contents of the ~/.ssh/id*.pub file to the ~/.authorized_keys file on the server. It will be a single line

It is probably called:


And the key will look like

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAIZ5CYnbANePH8y1rKvFIpWNWrr3kSuelNP61W/yPiPtf11DZgdWsP5eaBQQqEZpXnw57pu5SuPHe5FPn+L39c/xtPJdvn1ZSVo1OTWMPkKGX+5WuL5ypaHN5J2E4qkZD9vzQ4OcUGGdODZ676TPV5cAD7oYHvBWKdCxPUztUAAAADAQABAAIZ5CYnbANePH8y1rKvFIpWNWrr3kSuelNP6 user@foo

Note that it is a single line. If you delete the authorized_keys file on the server you will need to make sure the permissions are correct.

chmod 0644 ~/.ssh/authorized_keys
  • Is each public key on the server in the authorized_keys file separated by a new line? Jan 7, 2020 at 22:28

You must log in to answer this question.

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