Setting up multiple SSH keys on one computer
Setting up two separate SSH keys took me entirely too long because all of the information that I needed was spread out across multiple sources. I am going to consolidate them here for others to find, so hopefully this will save you some time! If you don’t know what an SSH key is or why it is used, you can check out this article to learn more about them.
My company uses a private Bitbucket account, and for my personal use, I have a public Github account. I am going to be travelling for the next month, so I wanted to be able to access my personal repos on my work laptop instead of hauling both around with me. Here are the steps that I took to set it up.
For the sake of this article, personal = Github
and work = Bitbucket
, but obviously do what you need to do.
Step 1: Navigate to the right location
All of your SSH keys need to be stored in ~/.ssh
, so navigate there using:
cd ~/.ssh
The ~
means the root of your user directory, which should make your SSH keys accessible no matter where you navigate to in your file structure, so long as you are logged into the correct user.
Step 2: Create the SSH keys
Next you will need to generate the keys. Start by creating your personal key:
ssh-keygen -t rsa -C "name@personal_email.com"
Hit enter.
It will prompt you for a file name, you should name it something like: id_rsa_personal
This will print out a big chuck of text for your fingerprint. You can ignore that, we’ll grab what we need from the key after.
Now create your work key:
ssh-keygen -t rsa -C "name@work_email.com"
Hit enter.
This time, when it prompts you for a file name, use id_rsa_work
These each will create two files (so four in total), one with the name you entered, and a second with .pub
at the end of it.
Step 3: Create a config file
Still in the same terminal, try using ls
to see if a file name config
exists. If it does, use code config
to open it up in your code editor. If it doesn’t exist, you can create it by using touch config
, then open it with code config
. Copy and paste the following in your config:
# Work account
Host bitbucket.org
HostName bitbucket.org
IdentityFile ~/.ssh/id_rsa_work
User git
IdentitiesOnly yes# Personal account
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa_personal
User git
IdentitiesOnly yes
Step 4: Add keys to your accounts
Next you will need to grab the actual key to add to either account. To do this, you can type the command cat id_rsa_personal.pub
and then copy the key that appears in your terminal. You can then navigate to the SSH/GPG keys section of Github/Bitbucket and paste your new key in! Repeat with cat id_rsa_work.pub
and you will be golden!
Try to pull or clone a repo from either account and see if it works. You might have to reload your config file (from ~/.ssh type in source config
), but if you do happen to get an error while trying, use the next step. If you don’t get an error, then you are done setup and can jump to step 6.
Step 5: “Too Open” Error
Permissions for '/Users/username/.ssh/id_rsa' are too open.
It is recommended that your private key files are NOT accessible by others.
This private key will be ignored.
If your private key is not-so-private, you can easily fix that. Just use chmod 600 ~/.ssh/id_rsa_work
(and/or personal) to make it only read-writeable by you. If you just want to make it readable by you, then you can use `chmod 400 ~/.ssh/id_rsa
. I recommend using the 600 over the 400 in this case.
Step 6: Clone your repo with the correct host
In step 3, we created a field called Host
for each of our SSH keys. In my example, I named them after the website used for source control. If you decided to name yours something else, make sure you reference it when you clone a new repo. Let’s say you decided to do Host personal
instead of Host github.com
, that means to clone into your github account you would need to use:
git clone git@personal:username/repo-name.git
But if you used the same naming convention as I did (which is the default for when you copy/paste from github and bitbucket, it should be:
git clone git@github.com:username/repo-name.git
Hopefully that helps!