How to properly configure ssh keys to multiple remote accounts involving github and bitbucket?

孤街浪徒 提交于 2019-12-04 19:23:52
CodeWizard

Whats you did id the right way to do it all.

Its described here as well: Managing two ssh keys

Looks like you did not add the keys to your remote server.


Multiple SSH Keys settings for different github account:

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


create different public key

create different ssh key according the article Mac Set-Up Git

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

for example, 2 keys created at:

~/.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
ricardoramos

My solution was:

My accounts:

Bitbucket
Usuário: ricardoramos
E-mail: ricardoramos.usp@gmail.com

Github – 01
Usuário: ricardousp
E-mail: ricardoramos@icmc.usp.br

Github – 02
Usuário: ricardormoliveira
E-mail: ricardo.comp.ufla@gmail.com

For each of the accounts performed the following steps:

  1. ssh-keygen -t rsa -C "my email"
  2. ssh-add ~/.ssh/(key name without the .pub)
  3. After the key does not run the command ssh-add -D, because I thought that this command only wipe the chache and in fact this was being my mistake!
  4. ssh-add -l
  5. go to the directory ~/.ssh
  6. if there is no config file, simply create it in the directory ~/.ssh
  7. sudo nano config
  8. add the following settings in the config file the ssh folder

My final ssh configuration file:

#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

After you finish creating and configuring all the keys just set the remote repositories:

  1. cat ~/.ssh/id_rsa.pub
  2. copy my key ssh-rsa AAAAB3Nz... my email to clipboard
  3. in bitbucket or github access my configuration > SSH keys add the key

After performing all the steps the push worked normally!

Link that helped me solve the problem:

tutsplus
youtube
stackoverflow

If I do not forget anything you think that's it! Hahaha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!