How to work with multiple ssh keys [duplicate]
Asked Answered
B

1

33

GitHub Users,

I am newbie on github and have some issue with git setup. I have 2 account with different users on github and i have setup git on my system 2 times

First /.ssh folder (as usual) (contain id_rsa which copied in first account)
Second /.ssh/new folder (contain id_rsa which copied in second account)

now at the time of push how can i switch between ssh key?

Because if i would like to push for second account it will use .ssh key instead of .ssh/new and gives me error.

Please make me correct if i am understood something wrong here.

Thanks.

Bushel answered 19/1, 2012 at 10:8 Comment(2)
I wonder why would you want to place "second" keys in ~/.ssh sub-folder? Why not to stick to convention of having key suffixed by numbers, like: id_rsa, id_rsa.1, id_rsa.2, etc...Rusticus
@JuicyScripter Just want to make separation , you are also right however i need to tell git which key need to use and how?Bushel
Y
50

(I've voted to close this as a possible duplicate, but I might as well add a similar answer anyway.)

When using the SSH transport, GitHub identifies you as a user based on the SSH key that you use to authenticate. So, you need to make sure that git is using one SSH key for one repository and another for the other.

I'm going to assume that:

  1. You have a GitHub account called user1, and you've added to that account the public key corresponding to your local private key /home/whoever/.ssh/id_rsa. Let's say that the repository you're interested in accessing as user1 is user1/whatever on GitHub.
  2. You have a second GitHub account called user2 and you've added to that account the public key corresponding to your local private key /home/whoever/.ssh/new/id_rsa. Let's say that the repository you're interested in accessing as user2 is user2/whatever on GitHub.

The simplest way to do deal with this is to create a new "remote" (i.e. a nickname for a remote repository) for each repository, where the hostname is in each remote's URL is actually an alias that you've set up in ~/.ssh/config. (If that config file doesn't exist, you'll have to create it.)

For example, one entry in the ~/.ssh/config file might look like:

Host github-as-user1
  HostName github.com
  User git
  IdentityFile /home/whoever/.ssh/id_rsa

Then you can add a remote called gh-user1, say, with:

git remote add gh-user1 git@github-as-user1:user1/whatever.git

... and then if you want to push your master branch to the repository user1/whatever on GitHub using the ~/.ssh/id_rsa key, you can just do:

git push gh-user1 master

In order to push as the other user (user2) to the second repository, you need to add a second alias to you ~/.ssh/config file. For example:

Host gh-as-user2
  HostName github.com
  User git
  IdentityFile /home/whoever/.ssh/new/id_rsa

Then to push to that second repository as the different user, you can just do:

git push gh-user2 master
Yeaton answered 19/1, 2012 at 10:54 Comment(3)
So git will not work with out .ssh/config? Why can't git take a parameter like standard ssh? -i .ssh/my_github_keyInapplicable
I would add that this can be tested through ssh -T git@gh-as-user1 and ssh -T git@gh-as-user1. If it does not work, try to run ssh-add ~/.ssh/new/id_rsa or ssh-add ~/.ssh/id_rsaBurgonet
If the keys you're using exist outside of the ~/.ssh directory, they won't work unless you do: ssh-add /path/to/keyArticular

© 2022 - 2024 — McMap. All rights reserved.