0

我的~/.ssh包含两个密钥对:

  • id_rsa_foo&id_rsa_foo.pub
    • foousername对于GitHub 上的用户( github.com)
  • id_rsa_bar&id_rsa_bar.pub
    • barusername对于GitHub 上的用户( github.com)
    • 对于barusername我服务器上的用户 ( indefero.myserver.com)

~/.ssh/config知道”密钥文件:

#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

~/.ssh/known_hosts知道”服务器:

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==

现在,当我尝试从 GitHub 克隆时,会出现错误: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

但是当我首先在我的本地机器上创建一个存储库并设置本地/项目config时:

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

我可以pull/ push。(这种方式适用于 GitHub 存储库,myserver ropos 甚至不需要密码:indefero@indefero.it-and-t.com's password:。但可能是另一个问题。)

为了克隆 repos,我该如何改变这种行为?

4

1 回答 1

1

您已~/.ssh/config正确设置,但您没有在git clone命令中使用正确的主机。使用您在配置文件中设置的主机名来使用相应的密钥:

# 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
于 2013-04-15T11:11:35.070 回答