ricardoramos
ricardoramos

Reputation: 946

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

I created and configured three ssh keys both locally and remotely as follows:

SSH keys - Emails

$> cat ~/.ssh/id_rsa.pub (E-mail Bitbucket)
ssh-rsa AAAAB3.../kJVKej/5 [email protected]

$> cat ~/.ssh/id_rsa_git_hub.pub (E-mail Github1 is the same account Bitbucket)
ssh-rsa AAAAB3...Iq9FkLN6L [email protected]

$> cat ~/.ssh/id_rsa_back_track.pub (E-mail Github2)
ssh-rsa AAAAB3N...MSdYFaZ0d [email protected]

List SSH keys (Two different ssh keys with the same email)

$> ssh-add -l
2048 6b:0b:dd...e6:b7 [email protected] (RSA)
2048 fc:20:37...1a:ec [email protected] (RSA)
2048 45:4c:92...40:70 [email protected] (RSA)

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

In addition, I also created a local repository with the name test and remote configuration:

$> git remote -v

origin  [email protected]:ricardormoliveira/testing.git (fetch)
origin  [email protected]:ricardormoliveira/testing.git (push)

But, when I try to push with my ricardormoliveira remote user the following message appears:

$> 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.

How do I do that git push with my ricardormoliveira user and not for ricardousp user? Why git is changing my users? What am I doing wrong?

Upvotes: 2

Views: 3175

Answers (2)

ricardoramos
ricardoramos

Reputation: 946

My solution was:

My accounts:

Bitbucket
Usuário: ricardoramos
E-mail: [email protected]

Github – 01
Usuário: ricardousp
E-mail: [email protected]

Github – 02
Usuário: ricardormoliveira
E-mail: [email protected]

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:[email protected]
Host bitbucket.org-ricardoramos
  HostName bitbucket.org
  User git
  IdentityFile ~/.ssh/id_rsa

#Account GitHub1 email:[email protected]
Host github.com-ricardousp
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_github

#Account GitHub2 email:[email protected]
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

Upvotes: 3

CodeWizard
CodeWizard

Reputation: 141986

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 "[email protected]"

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 [email protected]:activehacker/gfs.git gfs_jexchan

cd gfs_jexchan and modify git config

$ git config user.name "jexchan"
$ git config user.email "[email protected]" 

$ git config user.name "activehacker"
$ git config user.email "[email protected]" 

# or you can have global 
git config $ git config --global user.name "jexchan" 
git config --global user.email "[email protected]"

push your code

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

Upvotes: 1

Related Questions