2

I have 2 github accounts in the same computer, I want to be able to provide the user with which I wish to authenticate (i.e ssh userA@github or ssh userB@github). This works fine with the following .ssh/config file:

Host github.com
  Hostname github.com
  PreferredAuthentications publickey
  IdentityFile .ssh\userA_key
  User userA
Host github.com
  Hostname github.com
  PreferredAuthentications publickey
  IdentityFile .ssh\userB_key
  User userB

However, if I authenticate with the git user (i.e. ssh git@github.com). I get a successfully authenticated message with the user that appears first in the config file (userA). Is there a way to prevent this? I want to provide either userA or userB, and not being able to authenticate as git user.

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
sebsmgzz
  • 361
  • 2
  • 11
  • 2
    You only ever authenticate as `git@github.com`. You are identified by the ssh key you present, not the username. – larsks Jan 01 '21 at 23:09
  • https://stackoverflow.com/search?q=%5Bgithub%5D+%5Bssh%5D+multiple+accounts – phd Jan 02 '21 at 11:57

1 Answers1

4

If you are using GitHub, you need to always used the git user. GitHub, like many other places that provide Git hosting, always uses the user git and then determines the identity of the user based on the SSH key provided.

So if you use userA's SSH key with the git user, then GitHub will think you're userA, and if you use userB's SSH key with the git user, then GitHub will think you're userB. There's no need to identify yourself by the username.

In some other hosting environments, such as when using Gitolite or other self-hosted environments, the git user is used for Git operations, and normal access to the server is granted by other users. For example, if I wanted to push Git data, I'd authenticate as user git, but if I wanted to access the server as normal, I might authenticate as bk2204. This is not true for GitHub, which uses a dedicated service to terminate Git SSH connections, but it may be true for other hosting environments, and is part of why the pattern is popular.

bk2204
  • 64,793
  • 6
  • 84
  • 100