Setup Public/Private Key

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.

> 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:

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

> scp ~/.ssh/ myUsername@remoteServer:

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:

> ssh myUsername@remoteServer

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

> mkdir ~/.ssh
> mv ~/ ~/.ssh/authorized_keys

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

> cd ~/.ssh
> chmod 700 .
> chmod 600

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

> 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!:

> ssh myUsername@remoteServer