0

My ~/.ssh contains two keypairs:

  • id_rsa_foo&id_rsa_foo.pub
    • for user foousername on GitHub (github.com)
  • id_rsa_bar&id_rsa_bar.pub
    • for user barusername on GitHub (github.com)
    • for user barusername on my server (indefero.myserver.com)

The ~/.ssh/config "knows" the keyfiles:

#github.com-foousername account
Host github.com-foousername
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_foo

#github.com-barusername account
Host github.com-barusername
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_bar

#indefero.myserver.com-foousername account
Host indefero.myserver.com-foousername
    HostName indefero.myserver.com
    User foousername
    IdentityFile ~/.ssh/id_rsa_bar

The ~/.ssh/known_hosts "knows" the server:

github.com,207.97.227.239 ssh-rsa AAAAB3N...AaQ==
204.232.175.90 ssh-rsa AAAAB3N...AaQ==
indefero.myserver.com,111.222.333.444 ssh-rsa AAAAB3N...Ytw==

Now when I'm trying to clone from GitHub, an error ocurres: Permission denied (publickey). fatal: The remote end hung up unexpectedly

user@machine ~/Desktop/test
$ git clone git@github.com:foousername/project1.git
Cloning into 'foousername'...
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

But when I create a repository on my local machine first and set the local/project config up:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@github.com-foousername:foousername/project1.git

I can pull/push. (This way works for the GitHub repos, the myserver ropos don't want even so and require a password: indefero@indefero.it-and-t.com's password:. But might be another problem.)

How can I change this behaviour, in order to clone repos?

automatix
  • 14,018
  • 26
  • 105
  • 230

1 Answers1

1

You have set your ~/.ssh/config correctly, but you are not using the correct host in your git clone command. Use the hostname that you have set in your config file to use the corresponding key:

# Use your default key
git clone git@github.com:foousername/project1.git

# Use the ~/.ssh/id_rsa_foo key
git clone git@github.com-foousername:foousername/project1.git

# Use the ~/.ssh/id_rsa_bar key
git clone git@github.com-barusername:barusername/project1.git
Tim Heap
  • 1,671
  • 12
  • 11
  • Yes, sure, hostname by cloning must correspond to the alias in the config file! Now it works! Thank you very much! – automatix Apr 15 '13 at 11:18
  • I noticed, that part of the path after the `HostName` is for GitHub and for my server a bit different. On GitHub I additionally need my username. I have an idea, why it's so. But what is the technical cause? – automatix Apr 15 '13 at 11:46
  • 1
    The address of a git remote just tells git where to find the repo. A remote address could be a local file path: `~/repos/project1.git`, a web address: `http://repo.org/project1.git`, or a SSH address: `git@github.com:foousername/project1.git`. The SSH address tells git to SSH to `github.com`, log in as the `git` user, and look for the repository at the path `foousername/project1.git`. If your repo on the `indefero` server does not live at `~foousername/foousername/project1.git`, you will have to specify a different path, what ever that may be. – Tim Heap Apr 16 '13 at 00:49