如何正确配置涉及github和bitbucket的多个远程帐户的ssh密钥?

时间:2016-03-06 04:37:07

标签: git github ssh bitbucket

我在本地和远程创建和配置了三个ssh密钥,如下所示:

SSH密钥 - 电子邮件

$> cat ~/.ssh/id_rsa.pub (E-mail Bitbucket)
ssh-rsa AAAAB3.../kJVKej/5 ricardoramos.usp@gmail.com

$> cat ~/.ssh/id_rsa_git_hub.pub (E-mail Github1 is the same account Bitbucket)
ssh-rsa AAAAB3...Iq9FkLN6L ricardoramos.usp@gmail.com

$> cat ~/.ssh/id_rsa_back_track.pub (E-mail Github2)
ssh-rsa AAAAB3N...MSdYFaZ0d ricardo.comp.ufla@gmail.com

列出SSH密钥(两个不同的ssh密钥使用相同的电子邮件)

$> ssh-add -l
2048 6b:0b:dd...e6:b7 ricardoramos.usp@gmail.com (RSA)
2048 fc:20:37...1a:ec ricardo.comp.ufla@gmail.com (RSA)
2048 45:4c:92...40:70 ricardoramos.usp@gmail.com (RSA)

配置〜/ .ssh /配置文件

#Default Bitbucket - User = ricardoramos
Host bitbucket.org
  HostName bitbucket.org
  User git
  IdentityFile ~/.ssh/id_rsa

#Account GitHub1 - User = ricardousp
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_git_hub

#Account GitHub2 - User = ricardormoliveira
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_back_track

此外,我还创建了一个名为 test 的本地存储库和远程配置:

$> git remote -v

origin  git@github.com:ricardormoliveira/testing.git (fetch)
origin  git@github.com:ricardormoliveira/testing.git (push)

但是,当我尝试使用我的ricardormoliveira远程用户时,会出现以下消息:

$> git push origin master
ERROR: Permission to ricardormoliveira/testing.git denied to ricardousp.
fatal: Could not read from remote repository.

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

我如何使用我的ricardormoliveira用户进行git push而不是ricardousp用户?为什么git会改变我的用户?我做错了什么?

2 个答案:

答案 0 :(得分:1)

你确实认为这是正确的方法。

此处也有描述:Managing two ssh keys

看起来您没有将密钥添加到远程服务器。

针对不同github帐户的多个SSH密钥设置:

https://gist.github.com/jexchan/2351996

<强> create different public key

根据文章Mac Set-Up Git创建不同的ssh密钥

$ ssh-keygen -t rsa -C "your_email@youremail.com"
例如,

创建了2个键:

~/.ssh/id_rsa_activehacker
~/.ssh/id_rsa_jexchan

<强> Add these two keys to the ssh-agent:

$ ssh-add ~/.ssh/id_rsa_activehacker
$ ssh-add ~/.ssh/id_rsa_jexchan
you can delete all cached keys before

$ ssh-add -D

<强> check your keys

$ ssh-add -l

<强> Modify the ssh config

$ cd ~/.ssh/
$ touch config
$ subl -a config

Add the keys to the config file: ***

#activehacker account
Host github.com-activehacker
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_activehacker

#jexchan account
Host github.com-jexchan
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_jexchan

<强> Clone you repo and modify your Git config

# clone your repo 
git clone git@github.com:activehacker/gfs.git gfs_jexchan

cd gfs_jexchan and modify git config

$ git config user.name "jexchan"
$ git config user.email "jexchan@gmail.com" 

$ git config user.name "activehacker"
$ git config user.email "jexlab@gmail.com" 

# or you can have global 
git config $ git config --global user.name "jexchan" 
git config --global user.email "jexchan@gmail.com"

<强> push your code

git add .
git commit -m "your comments"
git push

答案 1 :(得分:1)

我的解决方案是:

我的帐户:

Bitbucket
Usuário:ricardoramos
电子邮件:ricardoramos.usp@gmail.com

Github - 01
Usuário:ricardousp
电子邮件:ricardoramos@icmc.usp.br

Github - 02
Usuário:ricardormoliveira
电子邮件:ricardo.comp.ufla@gmail.com

对于每个帐户执行以下步骤:

  1. ssh-keygen -t rsa -C "my email"
  2. ssh-add ~/.ssh/(key name without the .pub)
  3. 密钥没有运行命令ssh-add -D,因为我 以为这个命令只擦拭了chache,事实上就是这样 是我的错!
  4. ssh-add -l
  5. 转到目录~/.ssh
  6. 如果没有配置文件,只需在目录中创建它 ~/.ssh
  7. sudo nano config
  8. 在配置文件中添加以下设置ssh文件夹
  9. 我的最终ssh配置文件:

    #Default Bitbucket email:ricardoramos.usp@gmail.com
    Host bitbucket.org-ricardoramos
      HostName bitbucket.org
      User git
      IdentityFile ~/.ssh/id_rsa
    
    #Account GitHub1 email:ricardoramos@icmc.usp.br
    Host github.com-ricardousp
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_github
    
    #Account GitHub2 email:ricardo.comp.ufla@gmail.com
    Host github.com-ricardormoliveira
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_sec
    

    完成创建和配置所有密钥后,只需设置远程存储库:

    1. cat ~/.ssh/id_rsa.pub
    2. 将我的密钥ssh-rsa AAAAB3Nz... my email复制到剪贴板
    3. 在bitbucket或github中访问我的配置&gt; SSH密钥添加 键
    4. 执行完所有步骤后,推动工作正常!

      帮助我解决问题的链接:

      tutsplus
      youtube
      stackoverflow

      如果我不忘记任何你认为就是这样的话!哈哈哈