Multiple SSH Keys settings for different github account

Create different public key


create different ssh key according the article Mac Set-Up Git

cd ~/.ssh
ssh-keygen -t rsa -C "bustroker@email.com" -f "github_bustroker"
ssh-keygen -t rsa -C "bkr@email.com" -f "github_bkr"

The -C option is a comment to help identify the key. The -f option specifies the file name for the key pair.

for example, 2 private/public key pairs are created at:

~/.ssh/github-bustroker
~/.ssh/github-bustroker.pub
~/.ssh/github-bkr
~/.ssh/github-bkr.pub

Modify the ssh config


$ cd ~/.ssh/
$ touch config
$ nano config

Then add private keys files as IdentityFile

#bustroker account
Host github.com-bustroker
    HostName github.com
    User git
    IdentityFile ~/.ssh/github_bustroker

#bkr account
Host github.com-bkr
    HostName github.com
    User git
    IdentityFile ~/.ssh/github_bkr

The Host is an alias for the connection info. It can be use in git@[alias]:[user_name]/[repo_name].git

Test

ssh -T git@[alias]

Add public key to github

The corresponding public keys ~/.ssh/github_bustroker.pub and ~/.ssh/github_bkr.pub should be added to GitHub account under SSH and GPG keys.

Clone or add origin to your repo

Use alias in repos urls as: git@[alias]:[user_name]/[repo_name].git

To clone the repos

git clone git@github.com-bustroker:bustroker/[repo_name]
git clone git@github.com-bkf:bkr/[repo_name]

Or to add remote. If copying the command from github, make sure to replace @github.com with the right alias, e.g. @github.com-bkr

git remote add origin git@github.com-bustroker:bustroker/[repo_name]

Might want to add ssh keys to ssh agent:

Enable and start SSH service (in Windows) if not already

Enable OpenSSH Authentication Agent in windows services. Start the service

Start-Service ssh-agent

Check existing keys

$ ssh-add -l

then, add these two keys as following

$ ssh-add ~/.ssh/github_bustroker
$ ssh-add ~/.ssh/github_bkr

you can delete all cached keys before

$ ssh-add -D

finally, you can check your saved keys

$ ssh-add -l