Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple github accounts: what values for Host in .ssh/config?

I'm trying to understand how the configurations under .ssh/config and .git/config interact.

Here's the situation: I have two separate github accounts, let's call them GH0 and GH1, and I want to interact with both "passwordlessly", i.e., using ssh keys in ~/.ssh/id_rsa.GH0.pub and ~/.ssh/id_rsa.GH1.pub. At the moment this works for GH0 but not for GH1. (E.g., push commands to GH1 die with ERROR: Repository not found.\nfatal: The remote end hung unexpectedly.; ssh -T [email protected] works, but only because it connects GH0.)

This means that for each of these github accounts I have to have a corresponding section in ~/.ssh/config, specifying which ssh key file to use for it. (E.g., the section for GH0 will have something like IdentityFile ~/.ssh/id_rsa.GH0, etc.)

The question is: what else do I have to put in each of these sections? More specifically,

what do I have to put as the argument to the Host keyword in ~/.ssh/config?

The information that I've found so far on this makes no sense to me. In some examples, I see stuff like

Host github.com
    Hostname github.com
    User git
    IdentityFile ~/.ssh/id_rsa

Host ac2.github.com
    Hostname github.com
    User git
    IdentityFile ~/.ssh/id_rsa_ac2

Where does that "ac2." prefix in the second Host come from???

Where do I find the corresponding ones for my github accounts?

Some of the info I've found lead one to guess that arguments to the Host keyword are in fact arbitrary, implying that the following would be fine too

Host omgwtfbbq
    Hostname github.com
    User git
    IdentityFile ~/.ssh/GH0

Host 4.8.15.16.23.42
    Hostname github.com
    User git
    IdentityFile ~/.ssh/GH1

But if so, this raises yet another question: How does git (or github) know which of these two sections to use for any given command?

Again, I guess1 that this would be specified in the project's .git/config file, under some [remote ...] section, but how?

1I must resort to guessing because, for one thing, I have not been able to find documentation for the interpretation of the key-value pairs in .git/config. The closest I've found is the man page for git-config, but I can't find any documentation in this page for the url = ... field under a [remote ...] section. The man page for git-remote, too, has no documentation about this field.

like image 727
kjo Avatar asked Feb 04 '13 15:02

kjo


People also ask

Can same SSH key for multiple GitHub accounts?

GitHub does not allow us to use the same SSH key in multiple accounts, so we'll have to create separate keys for each account. We can create SSH keys and add them to our SSH agent by following this guide from the GitHub Documentation.

Can I use SSH key for multiple accounts?

So, No - you'll need a separate key for each account. Although you need multiple ssh key pairs for multiple accounts you can configure multiple ssh identities and use via aliases on your machine. You can also just use your username in place of "git" or "hg".


1 Answers

Do the following:

$ git remote add omgwtfbbq git@omgwtfbbq:user/repo.git
$ git pull omgwtfbbq master

This will use the Host alias you've defined in your ~/.ssh/config as the git host, and thus will use the IdentityFile you've setup.

like image 83
Hiery Nomus Avatar answered Sep 19 '22 09:09

Hiery Nomus