Home > OS >  GitHub authenticates as the wrong SSH user
GitHub authenticates as the wrong SSH user

Time:03-04

I have 2 GitHub users, for work and personal.

Each has their own ssh key: the personal one happens to be id_rsa, and the work one is id_ed25519. Each key is associated with the correct account in GitHub.

When working on work stuff, I changed ~/.ssh/config to use the work SSH key for everything (such as pushing to [email protected]):

UseRoaming no

Host *
    UseKeychain yes
    AddKeysToAgent yes
    IdentityFile ~/.ssh/id_ed25519

Now, I want to use my personal account again. I changed IdentityFile back to ~/.ssh/id_rsa, but when I try to push changes to a repo it authenticates as the wrong user.

git remote -v shows nothing that would indicate using the work ssh key:

origin  [email protected]:redacted-user/repo.git (fetch)
origin  [email protected]:redacted-user/repo.git (push)

ssh [email protected] is indeed authenticating as the wrong user:

Hi [work account]! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

How is GitHub authenticating my user?

I've confirmed there are no specific entries for github.com in ssh config, and I've confirmed my id_rsa is linked in my personal GitHub account.

CodePudding user response:

How is GitHub authenticating my user

By the public key registered to the GitHub account, matching the private key used locally.

Try a ssh -Tv [email protected] to understand which private key is actually used.

I prefer changing the remote URL to make sure I am using one account or the other.

In my ~/.ssh/config file, I would register the second account as:

Host ghuser2
  User git
  hostname github.com
  IdentityFile ~/.ssh/secondUserKey

Then:

ssh -T ghuser2
cd /path/to/local/repo
git remote set-url origin ghuser2:<aUser>/<aRepo>
git ls-remote # for testing

Note that it does not change the authorship of the commits made in that repository.
If git config user.name is still user1, and you make new commits, those will be displayed on GitHub as user1, even if you authenticate (when pushing them) as user2.

CodePudding user response:

Need this after changing the ssh config:

eval $(ssh-agent)
  • Related