<aside> 💡 Original post by: fredrik.anderzon.se

</aside>

14 MARCH 2017

Every time I switch computers I go through this dance of setting up new SSH keys for my different code repository accounts.

Coincidentally, every time I switch computers I've completely forgotten how to set up different SSH keys for my different accounts, leading to me Googling and looking through my old .ssh folder a lot trying to figure it out.

So this post is for future me, and probably present you, since you are here. 🤔

Generating keys

If you are setting up a new machine, first generate a key to the default ~/.ssh/id_rsa by running:

ssh-keygen -t rsa  
ssh-add ~/.ssh/id_rsa  

The created key is a private key you should never share with anyone. A public key ~/.ssh/id_rsa.pub will also be created.

You will be prompted for the location to save the keys in, hit enter to accept the default. You will also be asked for a passphrase to protect the key, enter anything you want here.

The idea is that you'll use this key for your personal accounts.

Next, for each company account you have, run this command (substituting companyName to something more meaningful), which tags the key with companyName and saves it to ~/.ssh/companyName.

ssh-keygen -t rsa -C "companyName" -f "companyName"  
ssh-add ~/.ssh/companyName  

That takes care of generating the keys, next up you have some SSH config to write.

Config

Open up the SSH config file:

vim ~/.ssh/config

Don't worry if this file doesn't already exists, it will be created later when you save your changes.

What you will do is add rules for different hosts.

Host bitbucket.org  
  HostName bitbucket.org
  IdentityFile ~/.ssh/id_rsa

Host companyname.bitbucket.org  
  HostName bitbucket.org
  IdentityFile ~/.ssh/companyName

Host github.com  
  HostName github.com
  IdentityFile ~/.ssh/id_rsa

Host companyname.github.com  
  HostName github.com
  IdentityFile ~/.ssh/companyName