1

The thing is me and my brother both engaged in programming and we just learned about GitHub (and similar repository sites) but we have only one computer in the house so my question is:

Is it possible the I can have 2 (or more) ssh key for 2 (or more) GitHub accounts?

If I have 2 accounts on GitHub and I created a ssh key on both account will the ssh key of 2nd account overwrites the ssh key of the first?

double-beep
  • 5,031
  • 17
  • 33
  • 41
ailia
  • 629
  • 6
  • 9
  • It is definitely possible? Just need to know a little bit about your environment to provide an accurate answer (OS, do you have different system accounts?). – Gonzalo Matheu May 17 '19 at 22:16
  • @GonzaloMatheu OS - currently using windows10, the computer is full dual boot with linux but just for beginners sake we just focus on windows for now. i (we) haven't accessed github on linux yet we just use the git bash on windows – ailia May 17 '19 at 22:23
  • What OS are you using? Do you have separate user accounts? – Code-Apprentice May 17 '19 at 22:23
  • 1
    Possible duplicate of [Multiple github accounts on the same computer?](https://stackoverflow.com/questions/3860112/multiple-github-accounts-on-the-same-computer) – Pali May 17 '19 at 22:53
  • Possible duplicate of [Multiple GitHub Accounts & SSH Config](https://stackoverflow.com/questions/3225862/multiple-github-accounts-ssh-config) – Code-Apprentice May 19 '19 at 18:10
  • Please don't add "Solved" to the title or describe the solution in the question. The way to indicate that your problem is solved is to accept an answer (which you've done). BTW, the obvious solution is to have two distinct user accounts on your computer, so each of you has a distinct `$HOME` directory. – Keith Thompson May 20 '19 at 21:43

4 Answers4

2

i created an ssh key on both account

If you followed the GitHub documentation, you should have created two SSH keys on your local computer. Then you upload the public key to GitHub, one for each account.

will the ssh key of 2nd account overwrites the ssh key of the first?

No, the SSH keys are stored on your local machine. When you share the public key for a key with your GitHub account, it does nothing to affect the keys in other accounts.


For this particular situation, the easiest solution would be to create two separate Windows users that each contain their own credentials for GitHub.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • thnaks i will try that ... so to make it short only one ssh-key for every windows account because it will overwrite any existing ssh-key on the current account – ailia May 17 '19 at 22:31
  • or simply use the same key for both users ... since the same person is using both accounts there is no advantage of using different keys ... – Pali May 17 '19 at 22:32
  • @Pali 'use the same key for both users' does that work even on different github accounts? because me and my brother had our own github accounts – ailia May 17 '19 at 22:40
  • @ailia when github's ssh daemon challenges your ssh client with public key authentication your ssh client automatically responses with the right key since both accounts use the same key ... thats the trick here, just like using the same password for multiple accounts, no matter the username, the password is always right – Pali May 17 '19 at 22:47
  • 1
    @Pali While what you say works in theory, GitHub will not allow you to upload the same public key to two different accounts. It will give an error when you attempt to upload it to the second account. – Code-Apprentice May 17 '19 at 22:48
  • I would doubt that ... if I get a duplication warning than I know that my private/public keypair matches with another user's keypair ... – Pali May 17 '19 at 22:50
  • "only one ssh-key for every windows account" You can have multiple keys for the same windows account without any problem. Typically, you configure SSH to use a given key for a given domain. You can easily use the same key for several different domains, or even a different key for each domain. But I haven't yet figured out how to use two different keys for the same domain. – Code-Apprentice May 17 '19 at 22:51
  • @Pali You can try it for yourself if you have two separate GitHub accounts. – Code-Apprentice May 17 '19 at 22:53
1

I found the solution in this SO answer. Basically, you can have several users with several git (GitHub/GitLab) accounts, each with his/her own SSH key, but you need two separate repository clones, configure the second repository to use another git username AND another hostname. The hostname is not real, it's just an entry into SSH config for using an alternative SSH key pair.

Steps:

  1. Clone the repo: git clone repo repo.user2

  2. Config local username: cd repo.user2 && git config user.name user2 && git config user.email user@example.com

  3. Create an alternative SSH key for user2: ssh-keygen -C "user@example.com" -f ~/.ssh/id_rsa.user2

  4. Add a section to ~/.ssh/config:

     Host github-user2.com
       Hostname github.com
       User git
       IdentityFile ~/.ssh/id_rsa.user2
       IdentitiesOnly yes
    
  5. Set the remote URL: git remote set-url origin git@github-user2.com:user/repo.git

  6. Now, you should be able to access de repo: git pull

erny
  • 1,296
  • 1
  • 13
  • 28
0

$HOME/.ssh/config

Host dev
    HostName dev.example.com
    Port 22000
    User fooey
Host github.com
    IdentityFile ~/.ssh/github.key

https://www.ssh.com/ssh/config/

Otherwise use the same ssh key for both users ...

Pali
  • 1,337
  • 1
  • 14
  • 40
  • How can the OP use this to use two different keys for two different users on github? – Code-Apprentice May 17 '19 at 22:27
  • trial-and-error ... identical hosts with different users maybe – Pali May 17 '19 at 22:28
  • 2
    "trial and error" doesn't seem like an answer that is well-suited for SO – Code-Apprentice May 17 '19 at 22:29
  • I provided an example of how to configure the ssh client to use different settings for different situations, I am not responsible for its limitations – Pali May 17 '19 at 22:34
  • 2
    this one is overwhelming for me right now but when i got a bit of understanding on this matter i will make sure to try this option too... thanks @Pali – ailia May 17 '19 at 22:36
  • I think they could define two host nicknames `github1` and `github2` with different keys but for both `HostName github.com` – theblackips May 17 '19 at 22:36
  • @theblackips How do you configure `github1` to use its key for `user1` only and `github2` use its key for `user2` only? – Code-Apprentice May 17 '19 at 22:58
  • simly google it ... there are tons of examples for this question ... that's why I flagged it as duplicate – Pali May 17 '19 at 23:02
  • @Pali I think this is the most promising, general-purpose answer. However, it seems incomplete because it doesn't actually show how to use two separate keys for the same domain. If it truly is a duplicate because the question has been answered elsewhere, then we should definitely close this as a dupe. – Code-Apprentice May 19 '19 at 18:05
  • okey, after several trial and error... i finally got it.. now i was able to understand that config code .. the given thread link to me by sir @Pali is the one that solves it... so technically i just followed the tutorial video given in that thread which is this https://www.youtube.com/watch?v=fnSRBRiQIU8 and that's it... it all worked out, no need to create separate account on windows OS.. just rename the ssh key upon creation and set it to that config file. – ailia May 20 '19 at 21:09
  • actually, is should edit my post and state how did i do it too... thank you so much sir @Pali and sir Code-Apprentice for such effort answering my question – ailia May 20 '19 at 21:11
  • @ailia Don't "sir" me, I work for a living =p but seriously, I'm glad you figured out a solution that works for you. – Code-Apprentice May 20 '19 at 21:47
0

A more practical answer:

Simply use the same key for both accounts.

Since the same person (you) is using both accounts, there is no advantage of using different keys.

ssh keys are used for identification, and the identity of both accounts is you ...

You can use a single ssh key for hundred of accounts, just make sure that your private key is secure and password protected.

Pali
  • 1,337
  • 1
  • 14
  • 40
  • I wouldn't say that using the same key for everything you do is a good idea. What if you *do* lose it? Then everything is gone. – theblackips May 17 '19 at 22:41
  • Sorry, I was unclear there. What if it becomes compromized? – theblackips May 17 '19 at 22:44
  • then both keys are compromised since they are in the same directory ... – Pali May 17 '19 at 22:48
  • 2
    Note that GitHub will refuse to allow you to upload a public key that already belongs to another account on their service. This means that the OP can upload an SSH key to one account, but when they attempt to upload the same key to the second account GitHub will return an error. – Code-Apprentice May 17 '19 at 22:56