【问题标题】:How do I use multiple SSH keys on GitHub?如何在 GitHub 上使用多个 SSH 密钥?
【发布时间】:2015-05-15 10:07:01
【问题描述】:

GitHub 上的 SSH 配置似乎是一场噩梦。我有多个 GitHub 帐户,但我可以有多个 SSH 密钥。在 GitHub SSH 配置部分,他们提到了这一点:

ssh-keygen -t rsa -C "your_email@example.com"
# Creates a new ssh key, using the provided email as a label
# Generating public/private rsa key pair.

我们强烈建议保持默认设置不变,因此当系统提示您“输入要保存密钥的文件”时,只需按 Enter 即可继续。

# Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]

为什么我应该总是使用id_rsa 文件?它将覆盖我现有的密钥。无论如何,我在这里给一个新名称并生成密钥。我执行将其添加到代理的所有其他步骤,并在 GitHub SSH 密钥部分进行更新。

完成所有这些步骤后,我来到最后一步:

$ ssh -vT git@github.com
Hi animesh11! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 3128, received 1976 bytes, in 0.5 seconds
Bytes per second: sent 6077.0, received 3838.9
debug1: Exit status 1

一切都很好,但不知何故git clone 仍然抱怨:

$ git clone git@github.com:regentmarkets/devbox.git
Cloning into 'devbox'...
ERROR: Repository not found.
fatal: Could not read from remote repository.

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

我不知道为什么 ssh -vT 有效而简单克隆无效。

【问题讨论】:

  • 您可以为不同的 github 帐户设置多个 ssh 密钥,使用 .git 文件夹下的配置文件负责身份验证。

标签: git github ssh


【解决方案1】:

我会使用 .ssh 配置为每个特定用户设置不同的配置。

例如,在您的用户根目录下的 .ssh 文件夹中编辑(或创建)配置文件,并添加类似以下内容:

Host user1-github
    HostName github.com
    User git
    IdentityFile ~/.ssh/user1_rsa
Host user2-github
    HostName github.com
    User git
    IdentityFile ~/.ssh/user2_rsa

其中user1_rsa 和user2_rsa 是ssh-keygen -t rsa -C "user1@example.com" 和ssh-keygen -t rsa -C "user2@example.com" 的输出

然后,在测试时只需使用ssh -vT user1-github 和ssh -vT user2-github。

此外,在克隆存储库时,请使用 git clone user1-github:username1/project.git 或 git clone user2-github:username2/project.git。

【讨论】:

    【解决方案2】:

    作为对 Serban 解决方案的补充,这里提供了另一种使用.ssh/config 文件来区分用户的方法。有点hacky,但不需要更改主机名。

    假设您有一个专业帐户和一个个人帐户,并且您这样设置您的密钥:

    $ ssh-keygen -t rsa -f ~/.ssh/perso_rsa -C "nick@perso.org"
    $ ssh-keygen -t rsa -f ~/.ssh/pro_rsa   -C "name.surname@pro.company.com"
    

    现在大多数时候,您可以使用一些东西来区分这两个身份。通常,在处理 git 存储库时,您的 git 配置与您要使用的身份相关联,例如,git config user.email 将返回两个电子邮件地址之一,该地址与此存储库的身份相关联。

    基于此,我们可以使用.ssh/config中的Match exec <cmd>过滤指令,写成这样:

    Match host github.com exec "[ $(git config user.email) = nick@perso.org ]"
        IdentityFile ~/.ssh/perso_rsa
    
    Host github.com
        IdentityFile ~/.ssh/pro_rsa
    

    只需对两个身份使用常规的git@github.com:user/project.git。

    其工作原理如下:当主机为github.com时,使用第一个匹配规则来确定要使用的IdentityFile。对于第一条规则,运行exec 命令,如果该命令返回零退出状态,则选择该规则。我们的测试(传递给用户的shell)检查git config user.email 是否返回nick@perso.org。如果是这种情况,则检查返回 true,规则匹配,并选择 ~/.ssh/perso_rsa。如果不是,则第二条规则将作为github.com 的通用规则,而选择~/.ssh/pro_rsa。

    我们可以为第二个身份添加另一个检查,但只有两个规则没有必要,“catch-all”行为应该足够了。

    我们还可以调整检查以测试来自 git 的 user.email 的其他参数。例如,我们可以检查当前工作目录的名称。有关Match exec 的更多详细信息,请参阅man ssh_config。

    补充说明(归功于 Acumenus):

    • 如果用户的 shell(至少是 bash、zsh)支持,如果部分检查(在我们的例子中,$(git config user.email))评估为多个单词,则需要使用双方括号,因为单方括号将是 break。理想情况下,我们希望在双引号之间加上引号,但它们已经用于 exec 的参数,我没有找到逃避它们的方法。
    • 使用此设置,通过 SSH 克隆新存储库将使用与默认用户电子邮件关联的密钥(git config -l 中的第一个 user.email 条目)。要使用另一个密钥克隆存储库,一种解决方案是首先创建存储库,然后添加远程并拉取:mkdir foo; cd foo; git init; git config ...; git remote add origin <URL>; git pull。
    • 如果密钥的文件是 ssh 默认查找的文件,例如$HOME/.ssh/id_rsa,那么包罗万象的规则当然是不必要的。
    • host 指令可用于在必要时同时匹配多个以逗号分隔的主机 (Match host github.com,gitlab.com exec ...)。

    【讨论】:

    • 对于任何想要clone 而不是pull 的人,请使用mkdir <repo_dir>; git clone -c user.email=nick@perso.org git@github.com:<user>/<repo>.git <repo_dir>,注意clone -c 不适用于git 本身创建的新目录。
    【解决方案3】:

    我创建了一个 CLI 命令来处理这个问题! 查看我的repo~~
    这个 repo 使用 ssh-agent 来切换你的 ssh 账户。

    Git_SSH-Account_Switch

    CLI 工具可以将 ssh 帐户切换到您当前的 shell。使用服务器时,您将轻松切换到您的 git 帐户和 ssh 密钥,并使用您的帐户来操作服务器上的项目。

    安装

    $ bash ./setup.sh
    

    它将在您的profile 和$logout_profile 中添加一些代码,并在$HOME 上设置git-acc 和.gitacc。
    文件:

    git-acc.sh -> $HOME/.git-acc, git-acc 函数。
    .gitacc -> $HOME/.gitacc,保存信息。在git-acc注册。

    控制

            +---------------+
            |    git-acc    |
            +---------------+
    
    SYNOPSIS
    
      git-acc [account]|[option]
    
    OPTIONS
    
      [account]               use which accounts on this shell, type the account name that you register.
      -h, --help              print help information.
      -add, --add_account     build git_account info. & ssh-key.
      -rm, --remove_account   remove git_account info. & ssh-key from this device
    
    
    EXAMPLES
    
      $ git-acc tw-yshuang
    

    【讨论】:

    • 不禁止链接到您自己的存储库,但您绝对应该提供有关如何使用它的更多信息。
    猜你喜欢
    • 2022-01-04
    • 2016-01-19
    • 1970-01-01
    • 2015-09-19
    • 2021-09-07
    • 2014-09-11
    • 1970-01-01
    • 2022-01-19
    • 2015-10-22
    相关资源
    最近更新 更多