ssh Passwordlessly

ssh Passwordlessly

You’re used to ssh-ing into machine B from machine A by typing:

ssh username@B.com
password

Now you wish to ssh from machine A into machine B without having to enter your password. Just follow these steps.

  1. Create a public/private RSA key pair by issuing the unix command ssh-keygen. Do not enter a passphrase.

  2. Put the private key on machine A (the machine you wish to ssh from). Usually the best place to put it is in ~/.ssh.

  3. On machine B, create the file ~/.ssh/authorized_keys. Add the public key as a line to this new file. You can do this with the command cat public_key >> authorized_keys or by copy/pasting.

  4. On machine A, create the file ~/.ssh/config. Add the following to this file. Host anyname
    HostName B.com
    User username
    IdentityFile ~/.ssh/private_key

That’s it. You can now passwordlessly ssh into machine B from machine A just by typing:

ssh anyname

And as a bonus, you can now scp to and from machine B passwordlessly as well.

There’s loads more that you can do with an ssh config file. So have a Google, and see what you can see.


I’m posting this online because it’s something I wish I had known sooner. Hopefully you can benefit from it.

This post isn’t the whole story though! Using an ssh key without a passphrase (a la step 1) is dangerous; if anyone gains access to your private key then they’ll be able to ssh into machine B as you. The solution is to (back in step 1) create a passphrase for your private key, and then ssh-agent will remember your decrypted private key so that you don’t need to enter your password too often.

Discussion 💬

Related