Setup Public/Private Key

| Comments

When you connect to a remote server, you’re connecting over ssh or scp or a similar protocol. In each case, you may have to provide some authentication credentials to prove you are who you say you are. This can come in the form of a user/password combo, but if you’re connecting a lot or if you’re trying to setup a non-interactive connection, this can become either really monotonous or really problematic. Public/private keys will come to our rescue, and we’ll never need to enter our password again.

1
> ssh-keygen

You can, but don’t need to protect your private key with a passphrase. It is private, after all. The default names are:

  • Private: id_rsa
  • Public: id_rsa.pub

Now you need to share your public key with the remote server:

1
> scp ~/.ssh/id_rsa.pub [email protected]:

The trailing colon will separate your hostname from the destination path and drop it in your default/home directory on the server. Now, get into the remote server for the last time using that antiquated password of yours:

1
> ssh [email protected]

Once here, we need to put the public key in a file that will be used for incoming connections:

1
2
> mkdir ~/.ssh
> mv ~/id_rsa.pub ~/.ssh/authorized_keys

And make sure that the permissions are locked down for security reasons:

1
2
3
> cd ~/.ssh
> chmod 700 .
> chmod 600 id_rsa.pub

Jump back to your local box and prep your private key for your next ssh/scp connection:

1
2
> eval `ssh-agent`
> ssh-add ~/.ssh/id_rsa

Note that we’re adding our private key only. If you had a passphrase on your private key, you’ll be prompted for it at this point. Voila! You’re next connection to that remote server should not require a password. Try it!:

1
> ssh [email protected]

Comments