如何正确配置 ssh 密钥到涉及 github 和 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/config file

#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 用户而不是 ricardousp 用户一起做那个 git 推送?为什么 git 要改变我的用户?我做错了什么?

我的解决方案是:

我的账户:

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

吉特哈布 – 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,因为我以为这个命令只是擦拭查切,实际上这是是我的错!
  4. ssh-add -l
  5. 转到目录~/.ssh
  6. 如果没有配置文件,只需在目录中创建它 ~/.ssh
  7. sudo nano config
  8. 在配置文件 SSH 文件夹中添加以下设置

我的最后一个 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 中,访问我的配置> SSH 密钥添加.key

执行所有步骤后,推送工作正常!

帮助我解决问题的链接:

图茨加

优酷堆栈溢出

如果我没有忘记任何事情,你认为就是这样!哈

你做了什么 id 正确的方法去做这一切。

这里也描述了它:管理两个 ssh 密钥

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


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

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


create different public key

根据文章创建不同的 ssh 密钥 Mac 设置Git

$ 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

最新更新