For most developers, there may be a need to run multiple GitHub accounts on one computer. For instance, you can run an Organization's GitHub account and another one for your personal projects all on the same computer. In this article, you will learn how to use multiple SSH keys for different GitHub accounts.
All personal accounts can own an unlimited number of public and private repositories, with an unlimited number of collaborators on those repositories. If you use GitHub Free, private repositories owned by your personal account have a limited feature set.
The simple answer is NO. You can't create more than 1 ID in GitHub by the same email, but you can do it by using multiple email IDs. If you try to do it, GitHub will show an error - "email invalid or already taken", so delete the present account or make it with a new email id .
GitHub Desktop does not support working with multiple GitHub user accounts at this time.
Andy Lester's response is accurate but I found an important extra step I needed to make to get this to work. In trying to get two profiles set up, one for personal and one for work, my ~/.ssh/config
was roughly as follows:
Host me.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/me_rsa
Host work.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/work_rsa
My work profile didn't take until I did a ssh-add ~/.ssh/work_rsa
. After that connections to github used the correct profile. Previously they defaulted to the first public key.
For Could not open a connection to your authentication agent when using ssh-add
,
check:
https://stackoverflow.com/a/17695338/1760313
I recently had to do this and had to sift through all these answers and their comments to eventually piece the information together, so I'll put it all here, in one post, for your convenience:
Step 1: ssh keys
Create any keypairs you'll need. In this example I've named me default/original 'id_rsa' (which is the default) and my new one 'id_rsa-work':
ssh-keygen -t rsa -C "[email protected]"
Step 2: ssh config
Set up multiple ssh profiles by creating/modifying ~/.ssh/config. Note the slightly differing 'Host' values:
# Default GitHub
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
# Work GitHub
Host work.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_work
Step 3: ssh-add
You may or may not have to do this. To check, list identity fingerprints by running:
$ ssh-add -l
2048 1f:1a:b8:69:cd:e3:ee:68:e1:c4:da:d8:96:7c:d0:6f stefano (RSA)
2048 6d:65:b9:3b:ff:9c:5a:54:1c:2f:6a:f7:44:03:84:3f [email protected] (RSA)
If your entries aren't there then run:
ssh-add ~/.ssh/id_rsa_work
Step 4: test
To test you've done this all correctly, I suggest the following quick check:
$ ssh -T [email protected]
Hi stefano! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T [email protected]
Hi stefano! You've successfully authenticated, but GitHub does not provide shell access.
Note that you'll have to change the hostname (github / work.github) depending on what key/identity you'd like to use. But now you should be good to go! :)
Let's say alice
is a github.com user, with 2 or more private repositories repoN
.
For this example we'll work with just two repositories named repo1
and repo2
https://github.com/alice/repo1
https://github.com/alice/repo2
You need to be to pull from these repositories without entering a passwords probably on a server, or on multiple servers.
You want to perform git pull origin master
for example, and you want this to happen without asking for a password.
You don't like dealing with ssh-agent, you have discovered (or you're discovering now) about ~/.ssh/config
a file that let's your ssh client know what private key to use depending on Hostname and username, with a simple configuration entry that looks like this:
Host github.com
HostName github.com
User git
IdentityFile /home/alice/.ssh/alice_github.id_rsa
IdentitiesOnly yes
So you went ahead and created your (alice_github.id_rsa, alice_github.id_rsa.pub)
keypair, you then also went to your repository's .git/config
file and you modified the url of your remote origin
to be something like this:
[remote "origin"]
url = "ssh://[email protected]/alice/repo1.git"
And finally you went to the repository Settings > Deploy keys
section and added the contents of alice_github.id_rsa.pub
At this point you could do your git pull origin master
without entering a password without issue.
So your instinct will be to grab that key and add it to repo2
's Deploy keys, but github.com will error out and tell you that the key is already being used.
Now you go and generate another key (using ssh-keygen -t rsa -C "[email protected]"
without passwords of course), and so that this doesn't become a mess, you will now name your keys like this:
repo1
keypair: (repo1.alice_github.id_rsa, repo1.alice_github.id_rsa.pub)
repo2
keypair: (repo2.alice_github.id_rsa, repo2.alice_github.id_rsa.pub)
You will now put the new public key on repo2
's Deploy keys configuration at github.com, but now you have an ssh problem to deal with.
github.com
domain?Your .ssh/config
file points to github.com
and it doesn't know which key to use when it's time to do the pull.
So I found a trick with github.com. You can tell your ssh client that each repository lives in a different github.com subdomain, in these cases, they will be repo1.github.com
and repo2.github.com
So first thing is editing the .git/config
files on your repo clones, so they look like this instead:
For repo1
[remote "origin"]
url = "ssh://[email protected]/alice/repo1.git"
For repo2
[remote "origin"]
url = "ssh://[email protected]/alice/repo2.git"
And then, on your .ssh/config
file, now you will be able to enter a configuration for each subdomain :)
Host repo1.github.com
HostName github.com
User git
IdentityFile /home/alice/.ssh/repo1.alice_github.id_rsa
IdentitiesOnly yes
Host repo2.github.com
HostName github.com
User git
IdentityFile /home/alice/.ssh/repo2.alice_github.id_rsa
IdentitiesOnly yes
Now you are able to git pull origin master
without entering any passwords from both repositories.
If you have multiple machines, you could copy the keys to each of the machines and reuse them, but I'd advise doing the leg work to generate 1 key per machine and repo. You will have a lot more keys to handle, but you will be less vulnerable if one gets compromised.
I have 2 accounts on github, and here is what I did (on linux
) to make it work.
ssh-keygen
, name them properly, so that make life easier.ssh-add path_to_private_key
~/.ssh/config
Host github-kc
Hostname github.com
User git
IdentityFile ~/.ssh/github_rsa_kc.pub
# LogLevel DEBUG3
Host github-abc
Hostname github.com
User git
IdentityFile ~/.ssh/github_rsa_abc.pub
# LogLevel DEBUG3
Set remote url for repo:
For repo in Host github-kc
:
git remote set-url origin git@github-kc:kuchaguangjie/pygtrans.git
For repo in Host github-abc
:
git remote set-url origin git@github-abc:abcdefg/yyy.git
Options in ~/.ssh/config
:
Host
github-<identify_specific_user>
Host could be any value that could identify a host plus an account,
it don't need to be a real host,
e.g
github-kc
identify one of my account on github for my local
laptop,
When set remote url for a git repo, this is the value to put after git@
, that's how a repo maps to a Host, e.g git remote set-url origin git@github-kc:kuchaguangjie/pygtrans.git
Host
]Hostname
github.com
for github,User
git git
for github,IdentityFile
LogLevel
DEBUG3
gives the most detailed info.Use the IdentityFile
parameter in your ~/.ssh/config
:
Host github.com
HostName github.com
IdentityFile ~/.ssh/github.rsa
User petdance
A possibly simpler alternative to editing the ssh config file (as suggested in all other answers), is to configure an individual repository to use a different (e.g. non-default) ssh key.
Inside the repository for which you want to use a different key, run:
git config core.sshCommand 'ssh -i ~/.ssh/id_rsa_anotheraccount'
If your key is passhprase-protected and you don't want to type your password every time, you have to add it to the ssh-agent. Here's how to do it for ubuntu and here for macOS.
It should also be possible to scale this approach to multiple repositories using global git config and conditional includes (see example).
I spent a lot of time to understand all the steps. So lets describe step by step:
ssh-keygen -t rsa
. Give it an alternative like proj1.id_rsa
and hit with no doubt because you don't need a passphrase.Add new section in .ssh/config
:
Host proj1.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/proj1.id_rsa
Take into account the first section and note that proj1.github.com
we will back to the section later.
ssh-add ~/.ssh/proj1.id_rsa
proj1.github.com
(exactly the host from the config file).
git clone [email protected]
.A good tutorial.
Don't mess up with hosts
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With