Skip to content

Instantly share code, notes, and snippets.

@RomuloOliveira
Forked from jexchan/multiple_ssh_setting.md
Last active November 1, 2015 19:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RomuloOliveira/d668b01e7eac40638571 to your computer and use it in GitHub Desktop.
Save RomuloOliveira/d668b01e7eac40638571 to your computer and use it in GitHub Desktop.
Configure multiple git accounts on same computer using ssh

Multiple SSH Keys settings for different github account

Create different public key

Create different ssh key according the article Generating SSH keys.

Example:

$ ssh-keygen -t rsa -C "personal@personalemail.com" -f id_rsa_personal

Or for your work account:

$ ssh-keygen -t rsa -C "work@workemail.com" -f id_rsa_work

For example, 2 keys created at:

~/.ssh/id_rsa_work
~/.ssh/id_rsa_personal

Then, add these two keys as following:

$ ssh-add ~/.ssh/id_rsa_work
$ ssh-add ~/.ssh/id_rsa_personal

You can delete all cached keys before:

$ ssh-add -D

Finally, check your saved keys:

$ ssh-add -l

Remember to add created keys to your Github account. This guide will help you.

Modify the ssh config

Open ~/.ssh/config file:

$ cd ~/.ssh/
$ touch config
$ vim config # please use your favorite editor

Then add something like:

# work account
Host github.com-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_work

# personal account
Host github.com-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_personal

Finally, test your added hosts:

$ ssh -T git@github.com-work
Hi WorkUser! You've successfully authenticated, but GitHub does not provide shell access.

Or:

$ ssh -T git@github.com-personal
Hi PersonalUser! You've successfully authenticated, but GitHub does not provide shell access.

Clone you repo and modify your Git config

Clone your repo using a configured host (in this case, "github.com-personal"):

    git clone git@github.com-personal:repoowner/repo.git

Enter repository and modify git config:

$ git config user.name "personal"
$ git config user.email "personal@personalemail.com" 

Done!

@pecota-luizalabs
Copy link

👍
Very helpful.
Just remember to add the key on your github account before execute ssh -T git@github.com-personal.

@agrava-luizalabs
Copy link

👍

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