Skip to content

Instantly share code, notes, and snippets.

@tarranjones
Forked from jexchan/multiple_ssh_setting.md
Last active October 3, 2019 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarranjones/24738c3df5755754785df32c2c6b75a6 to your computer and use it in GitHub Desktop.
Save tarranjones/24738c3df5755754785df32c2c6b75a6 to your computer and use it in GitHub Desktop.
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 tarranjones@github.com" -f ~/.ssh/tarranjones@github.com_id_rsa
$ ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to otherusername@github.com" -f ~/.ssh/otherusername@github.com_id_rsa
$ ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to tarranjones@bitbucket.org" -f ~/.ssh/tarranjones@bitbucket.org_id_rsa
$ ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to otherusername@bitbucket.org" -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

~/.ssh/tarranjones@github.com_id_rsa
~/.ssh/otherusername@github.com_id_rsa
~/.ssh/tarranjones@bitbucket.org_id_rsa
~/.ssh/otherusername@bitbucket.org_id_rsa

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 *-github.com *-bitbucket.org *-bitbucket.com
  User git

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

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

#Hostnames
Host *-github.com
  Hostname github.com

Host *-bitbucket.com *-bitbucket.org
  Hostname bitbucket.org

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 user.name "tarranjones"
$ git config --global user.email "tarranjones@gmail.com"

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 tarranjones-github.com:username/repo.git

...or for an existing working directory

git remote set-url origin tarranjones-github.com:username/repo.git

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

cd repo and modify git config

$ git config user.name "otherusername"
$ git config user.email "otheremail@gmail.com"

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 tarranjones@github.com



further reading
alias to switch user.email http://stackoverflow.com/a/33079036/2273611

Another related article in Chinese

  1. http://4simple.github.com/docs/multipleSSHkeys/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment