2

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 ...
dmx
  • 1,977
  • 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... – George Udosen May 19 '17 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 '17 at 18:52
  • @George I have disabled password connection ... Do I have to enable it or is there another way? – dmx May 19 '17 at 19:12
  • @George this is the wrong thing to do. – guntbert May 19 '17 at 19:16
  • 1
    @guntbert, yes you are right I totally forgot about the authorized_keys. dmx please use the answer below... – George Udosen May 19 '17 at 19:18
  • @dmx I believe your first sentence is wrong - your known-hosts-file never contains your keys. I suppose you lost the complete~/.ssh directory. – guntbert May 19 '17 at 20:13

1 Answers1

7

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.

ssh-keygen

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:

id_rsa.pub

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
gdahlm
  • 459