kjo
kjo

Reputation: 35321

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.

Upvotes: 13

Views: 11801

Answers (2)

Hiery Nomus
Hiery Nomus

Reputation: 17769

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.

Upvotes: 10

Alister Bulman
Alister Bulman

Reputation: 35149

Yes, the Host line in the .ssh/config file is an alias. You use it in place of github.com, and anything that would properly use SSH will read the file and swap in the real hostname, and also use the given keyfile and username, if they have been given.

Upvotes: 4

Related Questions