【问题标题】:How to properly configure ssh keys to multiple remote accounts involving github and bitbucket?如何正确配置涉及 github 和 bitbucket 的多个远程帐户的 ssh 密钥?
【发布时间】:2022-03-18 08:18:52
【问题描述】:

我在本地和远程创建并配置了三个 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 文件

#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 push?为什么 git 会改变我的用户?我做错了什么?

【问题讨论】:

  • 您是否将 pub 密钥添加到您的 github 个人资料中?
  • github 和 bitbucket 都提供 ssh 进行调试。首先尝试使用-vvv 标志。
  • 你好@steveax,就像我使用这个 -vvv 标志一样?你能帮帮我吗?

标签: git github ssh bitbucket


【解决方案1】:

我的解决方案是:

我的帐户:

比特桶
Usuário: 里卡多拉莫斯
邮箱: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. key之后没有运行命令ssh-add -D,因为我 以为这个命令只会擦除 chache 而实际上这是 是我的错!
  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 密钥添加 键

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

帮助我解决问题的链接:

tutsplus
youtube
stackoverflow

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

【讨论】:

    【解决方案2】:

    你所做的一切都是正确的。

    这里也有描述:Managing two ssh keys

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


    为不同的 github 帐户设置多个 SSH Keys:

    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
    

    【讨论】:

    • 你好@codeWizard,我怀疑这是因为我有两个不同的ssh密钥和同一个电子邮件,因为运行命令ssh-add -l它出现了结果:key 1 = 2048 6b:0b:dd:ca:...:b7 ricardoramos.usp@gmail.com (RSA) key 2 = 2048 fc:20:37:8....:6c:1a:ec ricardo.comp.ufla@gmail.com (RSA) key 3 = 2048 45:...2:41:d6:85:40:70 ricardoramos.usp@gmail.com (RSA),是不是因为这个不行?
    • 我添加了相关列表。
    • 如果您在 github 的同一个帐户下有 2 个密钥,那就有问题了
    • 一个key是bitbucket,一个是github,但是两者配置为同一个email。
    • 电子邮件应该是个问题,因为您的主机首先基于主机(url)
    猜你喜欢
    • 2020-02-18
    • 2014-09-11
    • 2011-03-14
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多