Add multiples SSH keys on the same host

This is a case if you use the same machine for work/home. You will need to be able to use different keys for different host.

One for git@mywork.com/project.git

One for git@github.com/project.git

Assuming that you already use id_rsa key for your github.com all we need is tell to ssh-agent to use the other key when the host is mywork.com

First, create a key for your work key

Creating new key

$ ssh-keygen -t rsa -C "your@work.email" -f "id_rsa_WORK"
# Your identification has been saved in id_rsa_WORK_rsa.
Your public key has been saved in id_rsa_WORK_rsa.pub.
The key fingerprint is:
XXX
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
+-----------------+

This will generate id_rsa_WORK in your .ssh folder.

Ensure ssh-agent is enabled:

$ eval "$(ssh-agent -s)"
# Agent pid 59566

And we need to tell the agent about our new key

$ ssh-add ~/.ssh/id_rsa_WORK
# Identity added: id_rsa_WORK (id_rsa_WORK)

Configure per host

When you clone/push an git repo from work, you use an uri like git@mywork.com/project.git , we need to tell the agent that when we use this host mywork.com to use a different key.

This is done adding into the .ssh/config file

Host mywork.com
HostName mywork.com
User git
IdentityFile ~/.ssh/id_rsa_OTHERUSER

Setting user

For the last, we need to change the user for the local repo.

So after clone the repository, set your work user

git config user.name "work_username"
git config user.email "me@work.com"

As we don't user global option, so this configuration will be placed in local repository only.

You can test the new key using `ssh -v git@mywork.com`

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.