Skip to content

Instantly share code, notes, and snippets.

@arjenblokzijl
Last active August 16, 2017 06:03
Show Gist options
  • Save arjenblokzijl/65b5ab7ba00c6d7f50e8 to your computer and use it in GitHub Desktop.
Save arjenblokzijl/65b5ab7ba00c6d7f50e8 to your computer and use it in GitHub Desktop.
Setup SSH keys

SSH keys

1. Create

ssh-keygen -t rsa

2. Store the keys (and passphrase)

Once you have entered the Gen Key command, you will get a few more questions:

Enter file in which to save the key (/home/demo/.ssh/id_rsa):

You can press enter here, saving the file to the user home (in this case, my example user is called demo).

Enter passphrase (empty for no passphrase):

It's up to you whether you want to use a passphrase. Entering a passphrase does have its benefits: the security of a key, no matter how encrypted, still depends on the fact that it is not visible to anyone else. Should a passphrase-protected private key fall into an unauthorized users possession, they will be unable to log in to its associated accounts until they figure out the passphrase, buying the hacked user some extra time. The only downside, of course, to having a passphrase, is then having to type it in each time you use the Key Pair.

3. Copy the Public Key with ssh-copy-id

Once the key pair is generated, it's time to place the public key on the virtual server that we want to use.

You can copy the public key into the new machine's authorized_keys file with the ssh-copy-id command. Make sure to replace the example username and IP address below.

ssh-copy-id user@123.45.56.78

Install ssh-copy-id on mac:

curl -L https://raw.githubusercontent.com/beautifulcode/ssh-copy-id-for-OSX/master/install.sh | sh

Or clone from https://github.com/beautifulcode/ssh-copy-id-for-OSX.

4. Create config

Now you can connect to your server without a password and that’s great. But it can be a drag to remember username or port or even domain name so let’s dumb it down a little bit more.

First step is to position yourself to .ssh directory.

cd ~/.ssh

If you were to type in the “ls” command you’d see that directory is populated with generated private and public keys. To move to the next step we have to create a config file. You can do it with vim, nano or something else. There are numerous ways to do it. Use whichever editor you’re most comfortable with.

vim config

Fill the config file with following lines:

Host myServer
    HostName yourserver.com # Or IP
    User yourusername
    IdentityFile ~/.ssh/id_rsa

Done

:)

Todo

Sources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment