Mervin Hemaraju
Mervin Hemaraju

Reputation: 2117

GitHub using the wrong SSH key

I have two GitHub accounts setup with ssh keys, one personal and one enterprise.

I have an ssh config file as such:

# * GitHub CKO SSH Key
Host github-enterprise
  HostName github.com
  AddKeysToAgent yes
  UseKeychain yes
  User git
  IdentityFile ~/.ssh/id_ed25519_github

# * GitHub Personal SSH Key
Host github-personal
  HostName github.com
  AddKeysToAgent yes
  UseKeychain yes
  User git
  IdentityFile ~/.ssh/gh_mervinhemaraju_ed25519

Both keys were created seperately and attached to the respective account.

The weird issue is that I was using this for like a month, and it was working. Today, when i logged in, i committed some work on my personal repo and when i tried to do a remote push (which was working for this repo previously), i got and user permission denied.

I then performed an ssh test on both ssh keys and the results was as such:

ssh -T ssh -T git@github-personal

Hi mervin-hemaraju-enterprise! You've successfully authenticated, but GitHub does not provide shell access.

ssh -T git@github-enterprise

Hi mervin-hemaraju-cko! You've successfully authenticated, but GitHub does not provide shell access.

The personal key test is wrong. It should've been Hi mervinhemaraju! You've successfully authenticated, but GitHub does not provide shell access. since mervinhemaraju is my personal account, but is instead referring to the enterprise one.

I am on MacOs. Can someone please help ?

Upvotes: 2

Views: 2398

Answers (2)

Jimmy Bernljung
Jimmy Bernljung

Reputation: 529

Add IdentitiesOnly yes for the entries in the ssh config. This will prevent the SSH Agent from trying all the keys it knows of and only use the ones specified in the config file.

Specifies that ssh should only use the identity keys configured in the ssh_config files, even if ssh-agent offers more identities. https://www.ssh.com/academy/ssh/config

Upvotes: 10

m0j0
m0j0

Reputation: 3864

I have had a similar issue, and what I did was create a local git config that explicitly specifies which SSH key to use.

My ~/.ssh/config file specifies to use my "work" ssh key, since that's most common on my work computer.

I have something like this in my ~/.ssh/config:

Host *
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/my-work-ssh-key
  ServerAliveInterval 60

In my "personal" projects, I configure Git to ignore my ~/.ssh/config file, and I set the identity file to my "personal" SSH key.

Something like this:

git config --local core.sshCommand "ssh -i ~/.ssh/my-personal-ssh-key -F /dev/null"

That puts the following in .git/config:

[core]
    sshCommand = ssh -i ~/.ssh/my-personal-key -F /dev/null

Upvotes: 0

Related Questions