Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Multiple SSH keys for different github accounts

Multiple SSH Keys settings for different git account

Create different public keys

Create different ssh keys for each git account

$ ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to" -f ~/.ssh/tarranjones@github.com_id_rsa
$ ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to" -f ~/.ssh/otherusername@github.com_id_rsa
$ ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to" -f ~/.ssh/tarranjones@bitbucket.org_id_rsa
$ ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to" -f ~/.ssh/otherusername@bitbucket.org_id_rsa

If you would like to use a passphrase then remove -N "" from the command, you will then be prompted for you pass-phrase.

This will create the following files


Please refer to github ssh issues for common problems.

You can add your keys to memory (give to ssh-agent) using the ssh-add command,

# run agent if it is'nt already 
$ eval `ssh-agent -s`

$ ssh-add ~/.ssh/tarranjones@github.com_id_rsa
$ ssh-add ~/.ssh/tarranjones@bitbucket.org_id_rsa
$ ssh-add ~/.ssh/otherusername@github.com_id_rsa
$ ssh-add ~/.ssh/otherusername@bitbucket.org_id_rsa

or modify ~/.ssh/config to specify that keys should be automatically added to a running ssh-agent

To list your added keys

$ ssh-add -l

to delete all cached keys with

$ ssh-add -D

Modify the ssh config

Make sure ~/.ssh exists and is writable

$ mkdir -p $HOME/.ssh
$ chmod 0700 $HOME/.ssh

Make sure ~/.ssh/config exists and open in your text editor (subl)

$ cd ~/.ssh/
$ touch config
$ subl -a config

Then add the following

#Set Git User Domains
Host * * *
  User git

Host tarranjones-*
  IdentityFile ~/.ssh/tarranjones@%h_id_rsa

Host otherusername-*
  IdentityFile ~/.ssh/otherusername@%h_id_rsa

Host *

Host * *

Host *
  Protocol 2
  UseKeychain yes
  AddKeysToAgent yes
  IdentitiesOnly yes

Modify your Git config

I recommend setting your global config to match your most frequently used account details

$ git config --global "tarranjones"
$ git config --global ""

Having the same username and email address for all your primary accounts across different git hosting providers is something worth thinking about. Your user config would have to be changed a lot less.

Clone you repo and modify your Git config

clone your repo

git clone

...or for an existing working directory

git remote set-url origin

you should set your user config for repositories where details differ from your global config details.

cd repo and modify git config

$ git config "otherusername"
$ git config ""

then use normal flow to push your code

$ git add .
$ git commit -m "your comments"
$ git push

Heres a little helper function

usage: ssh [user@]host]
function ssh_keygen(){
    ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to $1" -f ~/.ssh/"$1_id_rsa"

$ ssh_keygen

further reading
alias to switch

Another related article in Chinese

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment