多个GitHub帐户和SSH配置

rad*_*six 242 ssh github ssh-keys

我在使用两个不同的SSH密钥/ GitHub帐户以便一起玩时遇到一些麻烦.我有以下设置:

Repos可从一个帐户使用 git@github.com:accountname

Repos可从其他帐户使用 git@github.com:anotheraccount

每个帐户都有自己的SSH密钥.已添加两个SSH密钥,我已创建配置文件.我不相信配置文件是正确的.我不太确定如何指定使用的repos git@github.com:accountname应该使用id_rsa并且git@github.com:anotheraccount应该使用id_rsa_anotheraccount.

Jim*_*bel 303

安迪莱斯特的反应是准确的,但我找到了一个重要的额外步骤,我需要做的是让这个工作.在尝试设置两个配置文件时,一个用于个人配置,一个用于工作,我~/.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
Run Code Online (Sandbox Code Playgroud)

我的工作资料没有采取,直到我做了ssh-add ~/.ssh/work_rsa.之后,与github的连接使用了正确的配置文件.以前他们违约了第一张公钥.

对于无法在使用时打开与身份验证代理的连接ssh-add,
请检查:https: //stackoverflow.com/a/17695338/1760313

  • 谢谢! - ssh-add`就是我所缺少的. (38认同)
  • 要修复"它们默认为第一个公钥"的问题,将`IdentitiesOnly yes`添加到`〜/ .ssh/config`文件的`Host*`部分.这告诉ssh实际使用您指定的IdentityFiles,而不是用所有这些标识符向服务器发送垃圾邮件. (15认同)
  • 另请参见http://doblock.com/articles/using-public-keys-with-multiple-github-accounts.关键的新信息是,您可能需要将用户名(在此示例中为"work")添加到远程URL中的主机名,即git@work.github.com:work/my_repo.git(as反对"git@github.com ......") (5认同)

ste*_*ano 168

我最近不得不这样做,不得不筛选所有这些答案和他们的评论,最终将信息拼凑在一起,所以为了您的方便,我将把它全部放在这里,在一篇文章中:


第1步:ssh键
创建您需要的任何密钥对.在这个例子中,我将默认/原始'id_rsa'(这是默认值)和我的新'id_rsa-work'命名为:

ssh-keygen -t rsa -C "stefano@work.com"
Run Code Online (Sandbox Code Playgroud)


步骤2:ssh config
通过创建/修改〜/ .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
Run Code Online (Sandbox Code Playgroud)


第3步:ssh-add
您可能需要也可能不需要执行此操作.要检查,请运行以下列出身份指纹:

$ 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)
Run Code Online (Sandbox Code Playgroud)

如果您的条目不存在,请运行:

ssh-add ~/.ssh/id_rsa_work
Run Code Online (Sandbox Code Playgroud)


第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.
Run Code Online (Sandbox Code Playgroud)

请注意,您必须根据您要使用的密钥/标识更改主机名(github/work.github).但现在你应该好好去!:)

  • 这是一个很好的回应。我必须使用 ssh-add 添加两个 ssh 密钥才能利用配置文件。谢谢:) (2认同)

Gub*_*ron 39

假设alice是一个github.com用户,拥有2个或更多私有存储库repoN.对于此示例,我们将仅使用两个名为repo1and的存储库repo2

https://github.com/alice/repo1

https://github.com/alice/repo2

您需要从这些存储库中提取,而无需在服务器或多个服务器上输入密码.git pull origin master例如,您想要执行,并且您希望在不要求密码的情况下实现此目的.

你不喜欢处理ssh-agent,你发现(或者你现在正在发现)~/.ssh/config一个文件让你的ssh客户端知道要使用什么私钥,具体取决于主机名和用户名,简单的配置条目看起来像这个:

Host github.com
  HostName github.com
  User git
  IdentityFile /home/alice/.ssh/alice_github.id_rsa
  IdentitiesOnly yes
Run Code Online (Sandbox Code Playgroud)

所以你继续创建你的(alice_github.id_rsa, alice_github.id_rsa.pub)密钥对,然后你也去了你的存储库的.git/config文件,你修改了你的遥控器的URL是origin这样的:

[remote "origin"]
        url = "ssh://git@github.com/alice/repo1.git"
Run Code Online (Sandbox Code Playgroud)

最后你去了存储库Settings > Deploy keys部分并添加了内容alice_github.id_rsa.pub

此时,您可以在git pull origin master不输入密码的情况下执行操作.

但是第二个存储库怎么样?

因此,你的直觉是获取该密钥并将其添加到repo2部署密钥,但github.com会出错并告诉您该密钥已被使用.

现在你去生成另一个密钥(ssh-keygen -t rsa -C "alice@alice.com"当然没有使用密码),所以这不会变得一团糟,你现在可以像这样命名你的密钥:

  • repo1 密钥对: (repo1.alice_github.id_rsa, repo1.alice_github.id_rsa.pub)
  • repo2 密钥对: (repo2.alice_github.id_rsa, repo2.alice_github.id_rsa.pub)

您现在将新的公钥放在repo2github.com 上的部署密钥配置中,但现在您有一个ssh问题需要处理.

如果存储库托管在同一个github.com域上,ssh如何判断使用哪个密钥?

你的.ssh/config文件指向github.com并且它不知道在拉动时使用哪个键.

所以我在github.com上找到了一个技巧.您可以告诉您的ssh客户端每个存储库位于不同的github.com子域中,在这些情况下,它们将是repo1.github.comrepo2.github.com

首先是编辑.git/configrepo克隆上的文件,所以它们看起来像这样:

对于repo1

[remote "origin"]
        url = "ssh://git@repo1.github.com/alice/repo1.git"
Run Code Online (Sandbox Code Playgroud)

对于repo2

[remote "origin"]
        url = "ssh://git@repo2.github.com/alice/repo2.git"
Run Code Online (Sandbox Code Playgroud)

然后,在您的.ssh/config文件上,现在您将能够为每个子域输入配置:)

Host repo1.github.com
  HostName github.com
  User git
  IdentityFile /home/alice/.ssh/repo1.alice_github.id_rsa
  IdentitiesOnly yes

Host repo2.github.com
  HostName github.com
  User git
  IdentityFile /home/alice/.ssh/repo2.alice_github.id_rsa
  IdentitiesOnly yes
Run Code Online (Sandbox Code Playgroud)

现在,您git pull origin master无需从两个存储库输入任何密码.

如果你有多台机器,你可以将钥匙复制到每台机器上并重复使用它们,但我建议你做腿部工作,每台机器生成1个钥匙和回购.您将拥有更多要处理的密钥,但如果受到攻击,您将不那么容易受到攻击.

  • 指定与“.ssh/config”中的主机匹配的子域是关键的一步 - 非常感谢 (2认同)

Eri*_*ang 20

我在github上有两个帐户,这是我做的(上linux)使它工作.

按键

  • 创建2对rsa键,通过ssh-keygen,正确命名,以便让生活更轻松.
  • 通过私钥添加私钥到本地代理 ssh-add path_to_private_key
  • 对于每个github帐户,上传(不同的)公钥.

组态

的〜/ .ssh /配置

Host github-kc
    Hostname        github.com
    User git
    IdentityFile    ~/.ssh/github_rsa_kc.pub
    # LogLevel DEBUG3

Host github-abc
    Hostname        github.com
    User git
    IdentityFile    ~/.ssh/github_rsa_abc.pub
    # LogLevel DEBUG3
Run Code Online (Sandbox Code Playgroud)

为repo设置远程URL:

  • 对于主持人的回购github-kc:
    github-abc
  • 对于主持人的回购~/.ssh/config:
    Host

选项github-kc:

  • git@github- <identify_specific_user>
    主机可以是任何可以识别主机和帐户的值,它不需要是真正的主机,例如 git remote set-url origin git@github-kc:kuchaguangjie/pygtrans.git在github上为我的本地笔记本电脑识别我的一个帐户,

    当为git repo设置远程url时,这是后面的值,这就是Hostrepo映射到Host的方式,例如Hostname


  • [以下是子选项github.com]
  • User
    指定实际的主机名,只git用于github,
  • IdentityFilegit
    用户总是LogLevel为github,
  • DEBUG3
    指定要使用的密钥,只需将路径设为公钥,
  • linux
    指定要调试的日志级别,如果有任何问题,则ssh-keygen提供最详细的信息.

  • @MarkChackerian 我认为您不需要`ssh-add`,因为您的密钥不受密码保护,或者(如果您使用的是 Mac)OSX 钥匙串正在为您处理。`ssh-add` 可以防止您每次访问密钥时都需要输入密码。 (3认同)

And*_*ter 17

使用以下IdentityFile参数~/.ssh/config:

Host github.com
    HostName github.com
    IdentityFile ~/.ssh/github.rsa
    User petdance
Run Code Online (Sandbox Code Playgroud)


Jak*_*kul 7

编辑 ssh 配置文件的一种可能更简单的替代方法(如所有其他答案中所建议的)是将单个存储库配置为使用不同的(例如非默认)ssh 密钥。

在要使用不同密钥的存储库中,运行:

git config core.sshCommand 'ssh -i ~/.ssh/id_rsa_anotheraccount'
Run Code Online (Sandbox Code Playgroud)

如果您的密钥受密码保护并且您不想每次都输入密码,则必须将其添加到 ssh-agent。以下是ubuntumacOS 的操作方法

还应该可以使用全局 git config 和条件包含将这种方法扩展到多个存储库(参见示例)。


ber*_*iey 5

就我而言,上述解决方案都没有解决我的问题,但 ssh-agent 可以。基本上,我做了以下事情:

  1. 使用 ssh-keygen 生成密钥对,如下所示。它将生成一个密钥对(在本例中.\keyfile为 和.\keyfile.pub

    ssh-keygen -t rsa -b 4096 -C "yourname@yourdomain" -f keyfile

  2. 上传keyfile.pub到git提供商

  3. 在您的计算机上启动 ssh-agent(您可以检查ps -ef | grep ssh-agent它是否已经在运行)
  4. 运行ssh-add .\keyfile以添加凭据
  5. 现在你可以运行git clone git@provider:username/project.git


I15*_*159 5

我花了很多时间来理解所有的步骤。所以让我们一步一步地描述:

  1. 使用ssh-keygen -t rsa. proj1.id_rsa毫无疑问地给它一个替代选择,因为你不需要密码。
  2. 在 中添加新部分.ssh/config

    Host proj1.github.com
        HostName github.com
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/proj1.id_rsa
    
    Run Code Online (Sandbox Code Playgroud)

考虑第一部分并注意proj1.github.com我们稍后会回到该部分。

  1. 将身份添加到 ssh 代理 ssh-add ~/.ssh/proj1.id_rsa
  2. 这是我第一次搞砸的 - 现在当你想克隆一个 proj1 存储库时,你可以使用它proj1.github.com(正是配置文件中的主机)。 git clone git@proj1.github.com.

一个很好的教程。

不要惹房东


小智 5

I find solution of this problem and try to explain it. We should make change in both file ~/.ssh/config and in ~/.gitconfig

Let's show.

1. Create keys

You will need one key for each different account you will use on either GitHub or BitBucket.

Whichever site you have more identities with determines how many keys you will need.

A single key can act both as a GitHub and BitBucket key but cannot be associated with more than one BitBucket or GitHub account.

If you already have created a key in ~/.ssh/id_rsa (the default location), you may use that in place of the ~/.ssh/msmith key in my examples or you can leave that key and add additional keys for the other identities.

Create the keys and ssh-add them (make sure to enter a secure password and do not just leave it blank)

$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/key1_rsa -C "msmith@example.com" 
Run Code Online (Sandbox Code Playgroud)

Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): ************ Enter same passphrase again: Your identification has been saved in /Users/me/.ssh/key1_rsa. Your public key has been saved in /Users/me/.ssh/key1_rsa.pub. The key fingerprint is: ...

$  ssh-add ~/.ssh/key1_rsa
$  ssh-keygen -t rsa -b 4096 -f ~/.ssh/key2_rsa -C "jblige@example.com" 
Run Code Online (Sandbox Code Playgroud)

Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): ************ Enter same passphrase again: Your identification has been saved in /Users/me/.ssh/key2_rsa. Your public key has been saved in /Users/me/.ssh/key2_rsa.pub. The key fingerprint is: ...

$ ssh-add ~/.ssh/key2_rsa
Run Code Online (Sandbox Code Playgroud)

2. Setup ~/.ssh/config

Create a file in ~/.ssh/config (if it does not already exist). You must make sure it is readable only by the owner and the group and public bits are set off.

touch ~/.ssh/config
chmod 600 ~/.ssh/config
Run Code Online (Sandbox Code Playgroud)

We now need to add SSH configuration that specifies the github and bitbucket hostnames but with a suffix appended to qualify which key to use. We set the HostName to the correct github.com or bitbucket.org address.

注意:Linux 用户应该省略 UseKeychain yes 或添加 IgnoreUnknown UseKeychain (感谢 Soulofmischief)

~/.ssh/config
...

Host github.com-msmith
  HostName github.com
  UseKeychain yes
  AddKeysToAgent yes
  User git
  IdentityFile ~/.ssh/msmith_rsa
  IdentitiesOnly yes

Host bitbucket.org-msmith
  HostName bitbucket.org
  UseKeychain yes
  AddKeysToAgent yes
  User git
  IdentityFile ~/.ssh/msmith_rsa
  IdentitiesOnly yes

Host github.com-jblige
  HostName github.com
  UseKeychain yes
  AddKeysToAgent yes
  User git
  IdentityFile ~/.ssh/jblige_rsa
  IdentitiesOnly yes

Host bitbucket.org-jblige
  HostName bitbucket.org
  UseKeychain yes
  AddKeysToAgent yes
  User git
  IdentityFile ~/.ssh/jblige_rsa
  IdentitiesOnly yes

...
Run Code Online (Sandbox Code Playgroud)

3.将公钥添加到GitHub和BitBucket

登录每个用户的 GitHub,并将 ~/.ssh/xxxxx.pub 中的密钥添加到相应用户授权的 SSH 密钥中。

有关更多信息,请参阅: https://confluence.atlassian.com/bitbucket/set-up-an-ssh-key-728138079.html

或者

https://help.github.com/en/articles/adding-a-new-ssh-key-to-your-github-account

4. 创建关键特定的 .gitconfig

您将需要一个目录,其中与给定键对应的所有代码都将被签出。

我更喜欢将所有这些目录保存在我的 home ~/src 的一个目录中,并根据与密钥关联的帐户名来命名它们

mkdir -p ~/src/msmith
mkdir -p ~/src/jblige
Run Code Online (Sandbox Code Playgroud)

在每个目录中放置一个 .gitconfig 文件。

~/src/msmith/.gitconfig
...
[user]
  email = msmith@example.com
    
[url "git@bitbucket.org-msmith"]
  insteadOf = git@bitbucket.org
  
[url "git@github.com-msmith"]
  insteadOf = git@github.com
~/src/jblige/.gitconfig
...
[user]
  email = jblige@example.com
  signingkey = ABCD1234
  
[url "git@bitbucket.org-jblige"]
  insteadOf = git@bitbucket.org
  
[url "git@github.com-jblige"]
  insteadOf = git@github.com
  
[commit]
  gpgsign = true
Run Code Online (Sandbox Code Playgroud)

这样,我就为两个密钥使用了正确的电子邮件地址,甚至为 jblige 设置了自动提交签名。我还将原始 SSH 连接的所有主机名重写为我在 SSH 配置文件中创建的正确后缀主机名。

有关 GPG 签名的更多信息,请参阅:

https://help.github.com/en/articles/signing-commits

或者

https://confluence.atlassian.com/bitbucketserver/using-gpg-keys-913477014.html

5. 设置 Git 配置 includeif

要激活 ~/src/* 中的 .gitconfig 文件,请编辑主目录中的 .gitconfig 文件,并为引用其所在目录的每个 .gitconfig 文件添加 includeif 语句

~/.gitconfig
...

[includeif "gitdir:~/src/msmith/"]
    path = ~/src/msmith/.gitconfig
    
[includeif "gitdir:~/src/jblige/"]
    path = ~/src/jblige/.gitconfig
    
Run Code Online (Sandbox Code Playgroud)

不要忘记 [includeif "gitdir:... 语句中的尾部斜杠。

6. 克隆存储库

然后,您使用 SSH 克隆地址克隆代码(即 git@bitbucket.org... 或 git@github.com...,而不是 https://bitbucket.org... 也不是 https://github.com。 ..) 进入与您要用于该克隆的密钥相对应的目录。

$  cd ~/src/msmith
$  git clone git@github.com:someuser/somerepo.git
...
Run Code Online (Sandbox Code Playgroud)

由于重写,git 实际上会尝试使用与 SSH 配置文件中的配置相对应的后缀地址进行克隆,但由于 SSH 配置,它在实际连接到主机时将使用原始主机名,确保您使用正确的密钥。

所有向这些存储库的提交/拉取/推送都将使用相应的配置/密钥/帐户。


归档时间:

查看次数:

101352 次

最近记录:

6 年,3 月 前