2

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

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

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

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  git@github.com:ricardormoliveira/testing.git (fetch)
origin  git@github.com: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?

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
ricardoramos
  • 891
  • 3
  • 18
  • 35
  • Did you add the pub key to your github profile? – afuzzyllama Mar 06 '16 at 04:44
  • Both github and bitbucket offer ssh for debugging. Try that first with the `-vvv` flag. – steveax Mar 06 '16 at 04:45
  • Hello @steveax, as I do to use this -vvv flag? Could you help me please? – ricardoramos Mar 06 '16 at 04:48
  • 1
    [github instructions](https://help.github.com/articles/testing-your-ssh-connection/), bitbucket has instructions too: https://confluence.atlassian.com/bitbucket/troubleshoot-ssh-issues-271943403.html#TroubleshootSSHIssues-ssh-Tconnectiontest(GitBash/MacOSX/Linux) – steveax Mar 06 '16 at 04:51

2 Answers2

3

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

Community
  • 1
  • 1
ricardoramos
  • 891
  • 3
  • 18
  • 35
1

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
Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Hello @codeWizard, I suspect that is because I have two different ssh keys with the same email, because to run the command `ssh-add -l` It appears the result: 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)`, is it because of this that is not working? – ricardoramos Mar 06 '16 at 13:05
  • I added the listing in question. – ricardoramos Mar 06 '16 at 13:14
  • If you have the 2 keys under the same account in github its a problem – CodeWizard Mar 06 '16 at 13:16
  • One key is the bitbucket and the other is with github, but the two are configured to the same email. – ricardoramos Mar 06 '16 at 13:18
  • Email should be a problem since your host is baesd uponteh host (url) first – CodeWizard Mar 06 '16 at 13:20
  • tru to remove one and check if its working (elimination) – CodeWizard Mar 06 '16 at 13:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105482/discussion-between-ricardoramos-and-codewizard). – ricardoramos Mar 06 '16 at 13:23