Setting up SSH and Private Keys

A simple article today that hopefully is helpful and that I can also use as a reference in the future! We use SSH and git every day, but we don't set it up every day. This will get you up and running with SSH on Linux or a Unix-like OS in no time.

Setting up SSH and Private Keys

Copy your existing SSH key to ~/.ssh/id_rsa. If you need to generate a new one, the code for this is below.

FVFZJ1A1L414:~ user.name$ ssh-keygen -t rsa -b 4096 -C "user.name@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/you/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.
The key fingerprint is:
SHA256:NO/282vY1+y+tUZ76aZeMPaXThvTHfSHJgR1axtkZGE user.name@example.com
The key's randomart image is:
+---[RSA 4096]----+
|          ....E. |
|           . * . |
|        o   . +. |
|       . o . ..+.|
|        S . .++.o|
|         .  .o+.*|
|          o  o.OO|
|         . .o *=%|
|            o*B@=|
+----[SHA256]-----+

Once your private key is in place, let's update the permissions so SSH won't complain (also, so it's secure). SSH will not load a key that is stored unsafely.

chmod 600 ~/.ssh/id_rsa

Let's move onto the ~/.ssh/config file. We need to add a few fields so that our SSH Agent knows what to do with our keys. There is also an extra line you will need to add if you are on macOS.

Host *
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_rsa

  # If you're using a Mac, add the following line
  UseKeychain yes

Finally, we need to start the SSH Agent and then add our key to the agent. We can do this with a nice one-liner:

eval "$(ssh-agent -s)" && ssh-add
Identity added: /home/you/.ssh/id_rsa (user.name@example.com)

That's it, you're good to go!