Skip to content

Instantly share code, notes, and snippets.

@jfschaff
Forked from jexchan/multiple_ssh_setting.md
Last active December 11, 2018 10:29
Show Gist options
  • Save jfschaff/097fdba00f29dd94cb476742f0662751 to your computer and use it in GitHub Desktop.
Save jfschaff/097fdba00f29dd94cb476742f0662751 to your computer and use it in GitHub Desktop.
Multiple SSH keys for multiple GitHub accounts

Multiple SSH keys for multiple GitHub accounts

This is assuming that you already have a main git identity configured and a corresponding ssh key pair in ~/.ssh/ (on linux)

$ cd ~/.ssh/
$ ls
$ config  id_rsa  id_rsa.pub  known_hosts

If you are not there yet, refer to this GitHub howto.

We also assume that you have second GitHub account with username seconduser and email address seconduser@example.com.

Create a second ssh key pair

Create a new ssh key pair

$ cd ~/.ssh/
$ ls
$ config  id_rsa  id_rsa.pub  known_hosts
$ ssh-keygen -t rsa -C "seconduser@example.com" -f id_rsa_seconduser

You now have a second key pair

$ ls
config  id_rsa  id_rsa_seconduser  id_rsa_seconduser.pub  id_rsa.pub  known_hosts

Then, add the new private key to your ssh authentication agent

$ ssh-add ~/.ssh/id_rsa_seconduser

You can delete all cached keys

$ ssh-add -D

Finally, you can check your saved keys

$ ssh-add -l

Modify the ssh config

Edit file ~/.ssh/config and add

# first (default) account
Host github.com
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa

# seconduser account
Host github.com-seconduser
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_seconduser

Configure a repo to use your second identity

$ cd some-repo
$ git config user.name "seconduser"
$ git config user.email "seconduser@example.com"

And edit the repo git config file .git/config to change the remote server

  ...
  [remote "origin"]
- 	url = git@github.com:seconduser/some-repo.git
+ 	url = git@github.com-seconduser:parolepol/some-repo.git
  ...

Clone your repo and modify your Git config

clone your repo git clone git@github.com:activehacker/gfs.git gfs_jexchan

cd gfs_jexchan and modify git config

$ git config user.name "jexchan"
$ git config user.email "jexchan@gmail.com" 

$ git config user.name "activehacker"
$ git config user.email "jexlab@gmail.com" 

or you can have global git config $ git config --global user.name "jexchan" $ git config --global user.email "jexchan@gmail.com"

then use normal flow to push your code

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

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