The Computer Oracle

SSH config - same host but different keys and usernames

-------------------------------------------------------------------------------
Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn
-------------------------------------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Ancient Construction

--

Chapters
00:00 Question
01:24 Accepted answer (Score 74)
03:52 Thank you

--

Full question
https://superuser.com/questions/366649/s...

Question links:
https://stackoverflow.com/a/3828682

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#ssh #git #github

#avk47



ACCEPTED ANSWER

Score 78


Yes, only the section headers (the Host or Match lines) are searched for – everything else is only applied as a setting. In other words, if you connect to foo@bar.com, OpenSSH will only look for a section titled Host bar.com.

So if you have Host github_username2, you must use this exact same "hostname" in your Git remotes as well. OpenSSH will not find this section if you use git@github.com.

However, that is not what causes authentication failures. When connecting to GitHub via SSH, you must use git as your username – the server will recognize you based on the key alone. (In other words, the "git@" in "git@github.com" is actually the SSH username that GitHub uses – not some kind of URI scheme.)

So a correct SSH configuration would be:

Host github_username1
    Hostname github.com
    User git
    IdentityFile ~/.ssh/rsa_1
    IdentitiesOnly yes

Host github_username2
    Hostname github.com
    User git
    IdentityFile ~/.ssh/rsa_2
    IdentitiesOnly yes

with this Git configuration:

[remote "origin"]
    url = github_username1:username2/repo.git

(It doesn't matter where you specify the SSH username – you can have git@ in the URL, or you can have User git in .ssh/config, or both.)


Alternative Git configuration to automatically switch accounts depending on repo path:

  1. Create a file ~/.config/git/config.user1 containing:

    [url "github_username1:"]
        insteadOf = git@github.com:
    
  2. Create a config.user2 file that's the same except with "github_username2".

  3. In your main ~/.config/git/config file, tell Git to "include" one of the two files depending on which directory you're at:

    [includeIf "gitdir:~/projects/"]
        path = ~/.config/git/config.user1
    
    [includeIf "gitdir:~/src/work/"]
        path = ~/.config/git/config.user2
    
  4. Now, whenever you're at ~/src/work/, cloning anything from git@github.com:[etc] will automatically rewrite the URL to github_username2:[etc].