13

I am new to set up SSH Keys on Ubuntu 16.04. I was looking for an authorized key to remove named anne but I think I deleted the whole authorized_keys file.

mike@mike-thinks:~$ rm /home/mike/.ssh/
authorized_keys  id_rsa           id_rsa.pub       known_hosts
mike@mike-thinks:~$ rm /home/mike/.ssh/authorized_keys 

Is it serious mistake? If yes how to regenerate them? Meanwhile I did that :

mike@mike-thinks:~/.ssh$ touch ~/.ssh/authorized_keys
mike@mike-thinks:~/.ssh$ ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
mike@mike-thinks:~/.ssh$ chmod 600 ~/.ssh/authorized_keys
  • How did the first rm command end up listing the contents of the directory? Is that a copying error that should be ls /home/mike/.ssh/? – Barmar Jun 18 '18 at 19:50
  • 3
    @Barmar, that's what it looks like in Bash when you hit tab to autocomplete the path, and there's multiple choices. – ilkkachu Jun 18 '18 at 20:51
  • 2
    Now is the best time to think about backup and restore. – Martin Schröder Jun 19 '18 at 9:17
20

The ~/.ssh/authorized_keys file contains a list of public keys. It allows everyone who owns a private key matching one of those to connect to this machine and get remote access under this user account (the tilde ~ at the front of the path signifies that this file is under the current user's home directory).

On the server (the machine you want to connect to):

If you deleted this file, nobody will be able to authenticate under this user account using their private keys. Password authentication would still work, unless it was disabled.

You'd have to regenerate the file and add all the public keys to which you want to grant access using the public key authentication method again.

On the client (the machine you want to connect from, i.e. your local machine):

Here you do not need an authorized_keys file, because you do probably not want to authorize any keys and grant them remote access to your local computer. It should be empty or deleted.

On the client, you only need the private key files which are authorized on the server (i.e. which have their corresponding public keys in the authorized_keys file on the server), and optionally an appropriate config and known_hosts file.

  • Thanks ! Hmm, I'm really new to public keys, private ones and remote access. This is my personal machine. I think nobody had a remote access to my machine, I think not. – ThePassenger Jun 18 '18 at 12:55
  • If you don't want anyone having access to the machine, just remove the file or leave it empty and you're good to go. – RoVo Jun 18 '18 at 12:57
  • 2
    Okay, then I think you misread it. You don't need the authorized_keys file on the local machine, but on the server. – RoVo Jun 18 '18 at 13:00
  • 3
    ssh-keygen creates id_rsa and id_rsa.pub, not authorized_keys. And that for sure. – RoVo Jun 18 '18 at 13:07
  • 1
    Note that it's quite common to want to ssh from another computer to your desktop computer, and using keys to authenticate in that case is more secure. – Karl Bielefeldt Jun 18 '18 at 18:32
3

Yes, you removed the whole file. To recreate it all users have to copy their ssh-keys on the server agein. You can use

ssh-copy-id user@hostname.example.com

If you don't want to remove all keys, edit the file and remove only the lines next time.

  • Thanks, this is my personal machine. I'm not sure it served as a server yet – ThePassenger Jun 18 '18 at 13:01
  • 2
    On your client you need the private key-file and known_hosts and on the server authorized_keys with pubkeys – trietend Jun 18 '18 at 13:04

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.