Skip to content

Instantly share code, notes, and snippets.

@Prosen-Ghosh
Forked from jexchan/multiple_ssh_setting.md
Last active September 1, 2020 12:17
Show Gist options
  • Save Prosen-Ghosh/0dd6eb02ed0010d3f7176a24381e1609 to your computer and use it in GitHub Desktop.
Save Prosen-Ghosh/0dd6eb02ed0010d3f7176a24381e1609 to your computer and use it in GitHub Desktop.
Multiple SSH keys for different github accounts

Multiple SSH Keys settings for different github account

create different public key

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

$ ssh-keygen -t rsa -C "your_email@youremail.com"

Please refer to github ssh issues for common problems.

for example, 2 keys created at:

~/.ssh/id_rsa_activehacker
~/.ssh/id_rsa_jexchan

then, add these two keys as following

$ ssh-add ~/.ssh/id_rsa_activehacker
$ ssh-add ~/.ssh/id_rsa_jexchan

you can delete all cached keys before

$ ssh-add -D

finally, you can check your saved keys

$ ssh-add -l

Modify the ssh config

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

Then added

#activehacker account
Host github.com-activehacker
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_activehacker

#jexchan account
Host github.com-jexchan
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_jexchan

Clone you 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/

-------------------

At joining my current work place, I was asked to create a new GitHub account before I can be a member of the company’s GitHub organization and in turn get access to all the private project repositories. It wasn’t until I left the work laptop in my locker but still needed to get something done that I felt the need to manage both of my GitHub accounts on my personal laptop (who needs work-life balance?) To make things more complicated, the project I’m working on references other private repositories in its package.json’s dependencies block and needs to access them at npm install , which defaults to using my personal GitHub account’s ssh-key. After some trials and failures, here’s how I finally got it to work:

  1. Create a new ssh-key and add it to the work GitHub account $ ssh-keygen -t rsa -b 4096 -C "my_work_email@my_company.com" Say the new ssh-key was named “work_rsa”, now copy the content of the newly generated public key file (work_rsa.pub in this example) and paste it to the work GitHub account’s setting page as described in the GitHub help page.
  2. Modify the ssh config file ( ~/.ssh/config) Open the config file in a text editor (create it if there isn’t one in the ~/.ssh folder yet) and add the following to it:

Personal GitHub account

Host github.com HostName github.com User git AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_rsa

Work GitHub account

Host github.com-work HostName github.com User git AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/work_rsa 3. Clone the work project repo (with a slightly different address) To clone the work project repo using the new ssh-key we need to tweak a little bit on the repo’s ssh address. The host url needs to match the Host defined in the ssh config file from last step, namely, where in the address there is github.com, replace it with github.com-work. E.g., with the following private repo ssh address we get from GitHub: git@github.com:[my work GitHub group]/[my project].git We need to tweak its address like this before we can git clone it: git@github.com-work:[my work GitHub group]/[my project].git $ git clone git@github.com-work:[my work GitHub group]/[my project].git 4. Modify the package.json to install the private repo dependencies In the project that cloned locally, find the private repo dependencies in its package.json and do the same modification to their repo address as in step 3 (I wouldn’t commit these modifications since it breaks everything for other people who are also working on this project): "dependencies": { "private-module": "git+ssh://git@github.com-work/[private module name].git ... } *It might be necessary to first do a manual install for one of the private modules in terminal: $ npm install git+ssh://git@github.com-work/[private module name] So that the private repos get recognized in the ssh known_hosts and can be accessed during npm installing the project dependencies.

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