I regularly connect to my father's Ubuntu PC using ssh to help or to transfer files through scp. I have been using ssh keys to make these logins password-less and I thought the method needs a blog post 🙂
Generally a user can have two keys – a private key and a public key which we generate using the following process and then copy the public key to the remote machine. Remember, don't give anyone your private key.
This process has to be followed on the machine which will be used to login to the remote machine. If both ways is desired, repeat same steps from the other way (i.e. on remote).
I describe the process,
- Use ssh-keygen to generate your RSA key (giving a passphrase is optional). The key will be generated and you'll be prompted for the location, the default being ~/.ssh/id_rsa
- Use ssh-copy-id remoteuser@remotehost to copy your public key ~/.ssh/id_rsa.pub to the remote's authorized_keys. You'll be asked to enter credentials for the remote machine.
- Now, whenever you login to the remote machine, it checks if your public key (which is offered by your client machine when connecting) exists in its authorized_keys file. If yes, you're allowed access.