【问题标题】:How to setup two different Github accounts for two different repositories through SSH?如何通过 SSH 为两个不同的存储库设置两个不同的 Github 帐户?
【发布时间】:2017-07-04 11:11:04
【问题描述】:

在开始之前,我应该说我已经阅读了很多关于这个问题的主题(列在这篇文章的末尾),但没有一个对我有用,或者我在这里遗漏了一些琐碎的东西。

起初我确实使用 HTTPS 克隆了存储库,但后来我按照 here 的文档切换到了 SSH。我确实根据here 的文档生成了每个 SSH 密钥并将它们添加到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/*
    

但它不起作用,我的意思是我得到了与以前完全相同的错误。

我做错了什么?我错过了什么?

我读过的:

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

【问题讨论】:

标签: git github ssh


【解决方案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-)。

【讨论】:

    猜你喜欢
    • 2019-07-10
    • 2018-03-22
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 2018-04-23
    • 2022-12-11
    • 2021-03-28
    • 2023-03-22
    相关资源
    最近更新 更多