ailia
ailia

Reputation: 639

Is it possible to have 2 (or more) GitHub accounts, 2 (or more) ssh key in one computer?

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?

Upvotes: 1

Views: 3151

Answers (4)

erny
erny

Reputation: 2499

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 [email protected]

  3. Create an alternative SSH key for user2: ssh-keygen -C "[email protected]" -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 [email protected]:user/repo.git

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

Upvotes: 1

Pali
Pali

Reputation: 1357

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.

Upvotes: 0

Pali
Pali

Reputation: 1357

$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 ...

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83577

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.

Upvotes: 2

Related Questions