如何在 GitHub 上使用多个 SSH 密钥?
How do I use multiple SSH keys on GitHub?
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.
We strongly suggest keeping the default settings as they are, so when you're prompted to "Enter a file in which to save the key", just press Enter to continue.
# 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
有效而简单克隆无效。
我会使用 .ssh config 为每个特定用户设置不同的配置。
例如,编辑(或创建)您用户根目录下的 .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
。
作为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
将 return 两个电子邮件地址之一,与此 repo 的身份关联的那个。
基于此,我们可以在.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
命令是 运行,如果该命令 return 的退出状态为零,则选择该规则。我们的测试(传递给用户的 shell)检查是否 git config user.email
returns nick@perso.org
。如果是这种情况,则检查 return 为真,规则匹配,并且 ~/.ssh/perso_rsa
被选中。如果不是,则第二条规则作为 github.com
的包罗万象,并选择 ~/.ssh/pro_rsa
。
我们可以为第二个身份添加另一个检查,但是只有两个规则是没有必要的,“包罗万象”的行为应该足够了。
我们还可以调整检查以测试 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
. ,那么 catch-all 规则当然是不必要的
- 如有必要,
host
指令可用于同时匹配多个以逗号分隔的主机 (Match host github.com,gitlab.com exec ...
)。
我创建了一个 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
function.
.gitacc -> $HOME/.gitacc, save info. that regist on 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
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.
We strongly suggest keeping the default settings as they are, so when you're prompted to "Enter a file in which to save the key", just press Enter to continue.
# 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
有效而简单克隆无效。
我会使用 .ssh config 为每个特定用户设置不同的配置。
例如,编辑(或创建)您用户根目录下的 .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
。
作为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
将 return 两个电子邮件地址之一,与此 repo 的身份关联的那个。
基于此,我们可以在.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
命令是 运行,如果该命令 return 的退出状态为零,则选择该规则。我们的测试(传递给用户的 shell)检查是否 git config user.email
returns nick@perso.org
。如果是这种情况,则检查 return 为真,规则匹配,并且 ~/.ssh/perso_rsa
被选中。如果不是,则第二条规则作为 github.com
的包罗万象,并选择 ~/.ssh/pro_rsa
。
我们可以为第二个身份添加另一个检查,但是只有两个规则是没有必要的,“包罗万象”的行为应该足够了。
我们还可以调整检查以测试 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
. ,那么 catch-all 规则当然是不必要的
- 如有必要,
host
指令可用于同时匹配多个以逗号分隔的主机 (Match host github.com,gitlab.com exec ...
)。
我创建了一个 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
function.
.gitacc -> $HOME/.gitacc, save info. that regist ongit-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