如何通过SSH为两个不同的存储库设置两个不同的Github帐户?

时间:2017-07-04 02:26:08

标签: git github ssh

在开始之前,我应该说我已经阅读了很多关于这个问题的主题(在本文末尾列出),但没有一个对我有用,或者我在这里错过了一些微不足道的东西。

起初我使用HTTPS克隆了存储库但后来我根据here的文档切换到了SSH。我确实生成了每个SSH密钥,并按照here的文档将其添加到ssh-agent

在几行中(使用假数据,~表示我的主目录)这就是我所做的:

$ ssh-keygen -t rsa -b 4096 -C "first@gmail.com"
Enter a file in which to save the key (~/.ssh/id_rsa): [Press enter]
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

$ ssh-keygen -t rsa -b 4096 -C "second@gmail.com"
Enter a file in which to save the key (~/.ssh/id_rsa_second): [Press enter]
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

$ eval "$(ssh-agent -s)"
Agent pid 12697

$ ssh-add ~/.ssh/id_rsa
Identity added: ~/.ssh/id_rsa (~/.ssh/id_rsa)

$ ssh-add ~/.ssh/id_rsa_second
Identity added: ~/.ssh/id_rsa_second (~/.ssh/id_rsa_second)

$ ssh-add -l
4096 SHA256:gb+Gn4SqiyAP5ABUsmX6Xz11RHTSvDsWgEE5P2R2VTE ~/.ssh/id_rsa (RSA)
4096 SHA256:yxWMompayDNtYjv5y+FfJl7OpQ5Qu90kPgdXXvx6DRA ~/.ssh/id_rsa_second (RSA)

下一步是创建包含以下内容的~/.ssh/config文件:

#first account
Host github.com-first
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa

#second account
Host github.com-second
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_second

我在这里停下来尝试将id_rsa_second的SSH发布密钥添加到我想要使用它的存储库(这不需要解释)。下一个git pull

$ git pull
Bad owner or permissions on /home/rperez/.ssh/config
fatal: Could not read from remote repository.

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

然后我尝试了以下内容:

  • .git/config文件更改为以下内容:

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

但它没有用,我的意思是我得到了与之前完全相同的错误。

我做错了什么?我缺少什么?

我读过的内容:

  

注意:标题可能看起来令人困惑,但它是正确的,因为它是我想要实现的,我只是在谈论一个例子,但最后我应该能够设置多个指向不同存储库的帐户。

1 个答案:

答案 0 :(得分:1)

它无效的原因是因为这个错误:

  

/home/rperez/.ssh/config

上的所有者或权限不正确

对于敏感文件的权限,Ssh很挑剔(出于安全原因)。

man ssh说:

~/.ssh/config
        This is the per-user configuration file. The file format and configuration 
        options are described in ssh_config(5). Because of the potential for abuse,
        this file must have strict permissions: read/write for the user, and not
        writable by others.

因此请检查/home/rperez/.ssh/config的文件权限。它们应为0644(-rw-r--r-)。