0

My global user overrides my local git user.

Here's the full context: I have 2 Github accounts, so I wanted to set my ssh keys for those repos.

I've tried all of those guides.

article 1

article 2

article 3

article 4

Here's my ~/.ssh/config

# Personal GitHub
Host github.com
   HostName github.com
   User mirkancal
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_rsa
   IdentitiesOnly yes
# Yazılım Kafe
Host github.com-yazilimkafe
   HostName github.com
   User yazilimkafe
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_rsa_yazilimkafe
   IdentitiesOnly yes

My git remote -v output for the repo of yazilimkafe

origin  git@github.com-yazilimkafe:yazilimkafe/git-dersi.git (fetch)
origin  git@github.com-yazilimkafe:yazilimkafe/git-dersi.git (push)

I've added local usernames and emails accordingly. Related SO post 1 SO post 2

Here's my local gitconfig

[user]
        name = yazilimkafe
        email = mirkancaliskan.dev@gmail.com

So I believe I'm all set but when I try to use git push origin master

It gives me that:

ERROR: Permission to yazilimkafe/git-dersi.git denied to mirkancal.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I think on the ssh side, I don't have a problem, but I can't prevent my global user to not the override my local user in the repo. My question is, how to do that? Am I doing something wrong on the ssh part?

mirkancal
  • 4,762
  • 7
  • 37
  • 75
  • check this approach as well - https://stackoverflow.com/questions/25388499/how-can-i-run-git-push-pull-commands-with-ssh-verbose-mode - it might shed some light on what's going wrong. Likely you need some adjustment of git remote URL – agg3l Apr 13 '19 at 12:52
  • What kind of adjustments? – mirkancal Apr 13 '19 at 13:01
  • Both `User`s must be `git`. Git hosts perform identification and autentication on `ssh` protocol using keys, not user names. – phd Apr 13 '19 at 14:02
  • @phd I've changed them now, error still exists. – mirkancal Apr 13 '19 at 14:10
  • Do you have an ssh-agent running with the `mirkancal` identity loaded? If so, try killing it to see if it changes things. The message says you logged in using that identity, which can only come from your ssh agent or your `.ssh/config` as far as I know. – joanis Apr 13 '19 at 14:16
  • 1
    The `[user]` setting in your local or global git config is not relevant, by the way, that only controls how you're identified in commits, it doesn't affect connections to GitHub. – joanis Apr 13 '19 at 14:17
  • PS: this related post may help: https://stackoverflow.com/questions/55371929/push-to-git-but-denied-to-wrong-user-finally-solved/55377599#55377599 – joanis Apr 13 '19 at 14:22
  • @joanis I've terminated this: 3799 0.0 0.0 7080 4032 ? S 17:09 0:00 /usr/bin/ssh-agent -D -a /run/user/1000/keyring/.ssh Didn't help, is there a way to log out from git? The related post that you give worked. Thank you very much. But since my config file failed, do you think I have to manually add and remove id_rsa's? – mirkancal Apr 13 '19 at 14:31
  • I've reproduced your scenario now and the `.ssh/config` entry I show at that link works for me. You don't don't have to reload your ssh agent once you set it up. – joanis Apr 14 '19 at 15:27

1 Answers1

2

Expanding on the solution posted here Push to .git but denied to wrong user , finally solved for your specific situation, I have reproduced your scenario and this worked for me.

Setting up two GitHub host names

Step 1: Load all your identities using ssh-add.

Step 2. Add these entries to .ssh/config

Host github-yazilimkafe
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_yazilimkafe

Host github-mirkancal
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa

Step 3. clone the repos or set the remote urls using those names

git clone github-yazilimkafe:yazilimkafe/git-dersi.git
git clone github-mirkancal:mirkancal/some-project.git

At this point, you should be able to push and fetch in either sandbox without any difficulty, and without resetting your agent in between. The host name will make your agent provide the right identity to github when you log in via ssh in that sandbox.

Also using the name github.com:

If you also want to use the host name github.com, i.e., cloning git@github.com/... directly, then you have to worry about the order in which your ssh agent will present the ssh keys. The order in which you add your identities using ssh-add does not seem to matter. In my case .ssh/id_rsa was the default key my agent provided, but that's not always the case.

If you're using gnome-keyring, the suggestion to use a separate folder in this question seems to help: https://unix.stackexchange.com/questions/429730/how-to-change-the-order-of-keys-in-gnome-keyring

However, using my Cygwin/OpenSSH installation of the ssh agent, I could not make .ssh/id_rsa_2 the default.

So in my case, I have to use a named entry in .ssh/config to use .ssh/id_rsa_2 with GitHub, but I can use the name github.com when I want .ssh/id_rsa, my primary key.

joanis
  • 10,635
  • 14
  • 30
  • 40
  • Oh god, finally now I see the problem. Thank you. All of the articles and posts that I've applied, uses one host for work, like github-yazilimkafe and one for personal, which is only github. But in your answer you use github-mirkancal for personal host. I think using a general host as plain "github" overrides the other host. But nobody mention that? Is there a way to define a general host name for other github projects? – mirkancal Apr 14 '19 at 15:42
  • In my tests, I also had a plain github.com sandbox working while the customized one worked. For that one too work, I think the order of your calls to `ssh-add` matters and I think my other answer says it backwards, ssh might use the last one added first. I'm not at my PC now, but try changing the order of your keys to see if it works. I'll update my answer with that info when I have a chance to test again. – joanis Apr 14 '19 at 15:55
  • This solved my problem. https://unix.stackexchange.com/questions/429730/how-to-change-the-order-of-keys-in-gnome-keyring – mirkancal Apr 14 '19 at 16:07
  • I see a question there but no solution, mind adding a brief note as to what you actually did that worked? – joanis Apr 14 '19 at 16:12
  • I have moved id_rsa_yazilimkafe to another folder in .ssh/, so it and .pub file are in .ssh/other_keys folder, I don’t know why but this way its on the top on the output of ssh-add -l. When they are in the same folder, .ssh, even though I’ve added id_rsa_yazilimkafe just new, it was in the bottom on the output. Helped me change the order in the output of ssh-add -l, I guess. – mirkancal Apr 14 '19 at 16:34
  • I see, thanks for the details. I'm using a different agent, but obviously in each case ordering is the key. – joanis Apr 14 '19 at 16:49