多个GitHub帐户&;SSH配置
我在获取两个不同的SSH密钥/GitHub帐户时遇到了一些问题。我有以下设置: 使用多个GitHub帐户&;SSH配置,ssh,github,ssh-keys,Ssh,Github,Ssh Keys,我在获取两个不同的SSH密钥/GitHub帐户时遇到了一些问题。我有以下设置: 使用git@github.com:accountname 使用git@github.com:其他帐户 每个帐户都有自己的SSH密钥。已经添加了两个SSH密钥,并且我已经创建了一个配置文件。但是我不相信配置文件是正确的。我不太确定如何指定使用git@github.com:accountname应使用id\u rsa和git@github.com:其他帐户应使用id\u rsa\u其他帐户 在~/.ssh/config中
git@github.com:accountname
使用git@github.com:其他帐户
每个帐户都有自己的SSH密钥。已经添加了两个SSH密钥,并且我已经创建了一个配置文件。但是我不相信配置文件是正确的。我不太确定如何指定使用
git@github.com:accountname
应使用id\u rsa
和git@github.com:其他帐户
应使用id\u rsa\u其他帐户
在~/.ssh/config
中使用IdentityFile
参数:
Host github.com
HostName github.com
IdentityFile ~/.ssh/github.rsa
User petdance
Host proj1.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/proj1.id_rsa
安迪·莱斯特的回答是准确的,但我发现我需要采取一个重要的额外步骤来实现这一点。在尝试设置两个配置文件时,一个用于个人,一个用于工作,我的
~/.ssh/config
大致如下:
Host me.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/me_rsa
Host work.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/work_rsa
直到我做了一个ssh添加~/.ssh/work\u rsa
之后,我的工作配置文件才生效。之后,到github的连接使用了正确的配置文件。以前,它们默认使用第一个公钥
对于,在使用ssh add
时无法打开与身份验证代理的连接,请检查:
我发布了我用来处理这些问题的技巧我最近不得不这样做,必须筛选所有这些答案和他们的评论,最终将信息拼凑在一起,因此为了您的方便,我将把它们全部放在这里的一篇帖子中:
步骤1:ssh密钥
创建您需要的任何密钥对。在本例中,我将我的名字命名为default/original'id_rsa'(这是默认名称),并将我的新名字命名为'id_rsa-work':
ssh-keygen -t rsa -C "stefano@work.com"
步骤2:ssh配置
通过创建/修改~/.ssh/config设置多个ssh配置文件。请注意略有不同的“主机”值:
# Default GitHub
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
# Work GitHub
Host work.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_work
步骤3:ssh添加
你可能需要也可能不需要这样做。要检查,请通过运行以下命令列出身份指纹:
$ ssh-add -l
2048 1f:1a:b8:69:cd:e3:ee:68:e1:c4:da:d8:96:7c:d0:6f stefano (RSA)
2048 6d:65:b9:3b:ff:9c:5a:54:1c:2f:6a:f7:44:03:84:3f stefano@work.com (RSA)
如果您的条目不存在,请运行:
ssh-add ~/.ssh/id_rsa_work
步骤4:测试
为了测试您是否正确完成了这一切,我建议进行以下快速检查:
$ ssh -T git@github.com
Hi stefano! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T git@work.github.com
Hi stefano! You've successfully authenticated, but GitHub does not provide shell access.
注意,您必须根据要使用的密钥/标识更改主机名(github/work.github)。但是现在你应该可以走了!:) 我曾经
Host github.com
HostName github.com
IdentityFile ~/.ssh/github_rsa
User abc@gmail.com
天气很好
在.ssh/config文件中为不同用户名的不同rsa密钥使用上述设置。假设
alice
是一个github.com用户,拥有两个或多个私有存储库repoN
。
在本例中,我们将使用两个名为repo1
和repo2
https://github.com/alice/repo1
https://github.com/alice/repo2
您需要在不输入密码的情况下从这些存储库中提取密码,可能是在一台服务器上,也可能是在多台服务器上。
例如,您希望执行git pull origin master
,并且希望在不要求密码的情况下执行此操作
您不喜欢与ssh代理打交道,您已经发现(或现在正在发现)关于~/.ssh/config
一个文件,该文件让您的ssh客户端根据主机名和用户名知道要使用什么私钥,其中有一个简单的配置条目,如下所示:
Host github.com
HostName github.com
User git
IdentityFile /home/alice/.ssh/alice_github.id_rsa
IdentitiesOnly yes
[remote "origin"]
url = "ssh://git@github.com/alice/repo1.git"
[remote "origin"]
url = git@github.com-account1:invitedByUserName/gfs.git
因此,您继续创建了(alice\u github.id\u rsa,alice\u github.id\u rsa.pub)
密钥对,然后您还访问了存储库的.git/config
文件,并将远程源文件的url修改为类似以下内容:
Host github.com
HostName github.com
User git
IdentityFile /home/alice/.ssh/alice_github.id_rsa
IdentitiesOnly yes
[remote "origin"]
url = "ssh://git@github.com/alice/repo1.git"
[remote "origin"]
url = git@github.com-account1:invitedByUserName/gfs.git
最后,您转到了存储库Settings>deploykeys
部分,添加了alice\u github.id\u rsa.pub
此时,您可以在不输入密码的情况下执行git pull origin master
但是第二个存储库呢?
因此,您的本能是抓取该密钥并将其添加到repo2
的部署密钥中,但github.com会出错并告诉您该密钥已被使用
现在您可以生成另一个密钥(使用ssh-keygen-t rsa-C“alice@alice.com“
当然没有密码),为了不让这变得一团糟,您现在可以这样命名密钥:
Host github.com
HostName github.com
User git
IdentityFile /home/alice/.ssh/alice_github.id_rsa
IdentitiesOnly yes
[remote "origin"]
url = "ssh://git@github.com/alice/repo1.git"
[remote "origin"]
url = git@github.com-account1:invitedByUserName/gfs.git
repo1
密钥对:(repo1.alice\u github.id\u rsa,repo1.alice\u github.id\u rsa.pub)
repo2
密钥对:(repo2.alice\u github.id\u rsa,repo2.alice\u github.id\u rsa.pub)
现在,您将把新公钥放在github.com上的repo2
部署密钥配置中,但现在您需要处理ssh问题
如果存储库托管在同一github.com
域上,ssh如何判断要使用哪个密钥?
您的.ssh/config
文件指向github.com
,它不知道在执行拉取操作时使用哪个键
所以我在github.com上找到了一个窍门。您可以告诉您的ssh客户端,每个存储库位于不同的github.com子域中,在这些情况下,它们将是repo1.github.com
和repo2.github.com
因此,第一件事是编辑repo克隆上的.git/config
文件,因此它们看起来像这样:
报告1
[remote "origin"]
url = "ssh://git@repo1.github.com/alice/repo1.git"
报告2
[remote "origin"]
url = "ssh://git@repo2.github.com/alice/repo2.git"
然后,在.ssh/config
文件中,现在可以为每个子域输入配置:)
现在,您可以git拉取原始主机
,而无需从两个存储库输入任何密码
如果您有多台机器,您可以将密钥复制到每台机器上并重用它们,但我建议您进行腿部工作,以便为每台机器生成一个密钥并进行回购。您将有更多的密钥需要处理,但如果其中一个密钥被泄露,您将不那么容易受到攻击。作为@stefano的补充
~/.ssh/id_rsa_account1
~/.ssh/id_rsa_account2
ssh-add ~/.ssh/id_rsa_account2
ssh-add ~/.ssh/id_rsa_account1
cd ~/.ssh/
touch config
#Github account1
Host github.com-account1
HostName github.com
User account1
IdentityFile ~/.ssh/id_rsa_account1
#Github account2
Host github.com-account2
HostName github.com
User account2
IdentityFile ~/.ssh/id_rsa_account2
[remote "origin"]
url = git@github.com-account1:account1/gfs.git
[remote "origin"]
url = git@github.com-account1:invitedByUserName/gfs.git
[remote "origin"]
url = git@github.com-account2:account2/gfs.git
git config user.name "account1"
git config user.email "account1@domain.com"
git config user.name "account2"
git config user.email "acccount2@doamin.com"