How to properly configure ssh keys to multiple rem

2019-04-16 07:45发布

问题:

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?

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


回答2:

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