Using multiple RSA keys to connect through SSH

I was just having trouble because you can’t use the same RSA key in order to connect to multiple accounts on bitbucket, this can be solved creating a new one, but you need to know how to handle them, and there is two ways to do it.

If you just need to connect with ssh to a host well you can just put it as a parameter, but this doesn’t work as a complete solution.

ssh -i PATH_TO_SSH_KEY username@host.com

This problem can be solved using a config file, for this solution you just need to create a file ~/.ssh/config and

Host shortname realname.example.com
    HostName realname.example.com
    IdentityFile ~/.ssh/realname_rsa
    User remoteusername

Host bitbucket_1 bitbucket.org
    HostName bitbucket.org
    IdentityFile ~/.ssh/account1_rsa
    User git

Host bitbucket_2 bitbucket.org
    HostName bitbucket.org
    IdentityFile ~/.ssh/account2_rsa
    User git

This way you could make, for example a clone in the following way:

git clone git@bitbucket_1:user/repo.git
git clone git@bitbucket_2:otheruser/repo.git

And it works just fine with your multiple accounts.

Note: Keep in mind that according to that configuration, because two hosts share the same bitbucket.org alias if you clone from the real url, meaning git@bitbucket.org:something/repo.git it will use the later, in this case account_2 for the authentication. (You can obviously erase it from the Host tag, but this is just to explain the case)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s