RileyE
RileyE

Reputation: 11074

Using multiple ssh keys for same host, without using an alias

I have two bitbucket accounts and ssh keys to match. I've seen many solutions to manage using multiple keys on the same host through aliasing, but unfortunately, I need my git references to be common.

The reason I need them to be common is because I'm working on shared projects with Pods. This means that the remote git source url needs to be the same for all developers working on the projects, but I can't guarantee that everyone will be able to setup the same alias as me and they shouldn't be expected to.

I've tried the foolish attempt at specifying my user accounts like so:

Host BitbucketA
 HostName bitbucket.org
 IdentityFile ~/.ssh/id_rsa
 User UserA

Host BitbucketB
 HostName bitbucket.org
 IdentityFile ~/.ssh/id_rsa_userB
 User UserB

Thinking that I could specify to use id_rsa_userB whenever I'm trying to access one of UserB's repositories. Unfortunately, that doesn't seem to be how it works.

Is there any way to accomplish what I want?

Upvotes: 4

Views: 4208

Answers (3)

Yinzara
Yinzara

Reputation: 832

I wrote a Gist about this: https://gist.github.com/yinzara/bbedc35798df0495a4fdd27857bca2c1

You can manage multiple keys for the same host using directories.

Upvotes: 1

zaph
zaph

Reputation: 112857

You are really close to a solution, that is essentially what I have done and it does work. See my answer and the link below.

Here is how I setup my ssh keys for two accounts on the same server on my Mac:

This is only necessary if you have logins to two separate accounts on the same Git remote repo.

Create two SSH keys:

$ ssh-keygen -t rsa -f id_rsa_account1
$ ssh-keygen -t rsa -f id_rsa_account2

Add this to ~/.ssh/config:

Host git_account1
        User git
        HostName git.xxxx.com
        IdentityFile ~/.ssh/id_rsa_account1
Host git_account2
        User git
        HostName git.xxxx.com
        IdentityFile ~/.ssh/id_rsa_account2

SSH translates the Host name into the HostName prepending the User and selects the IdentityFile. This allows two or more repos for the same URL with different ssl certs.

Also see Nerderati: ~/.ssh/config & git

Upvotes: 2

kostix
kostix

Reputation: 55443

You could resort to using a pair of shell scripts, and an environment variable:

$ cat >~/bin/ssh-acct-one
#!/bin/sh
exec /usr/bin/ssh -i ~/.ssh/id_rsa_account1 $@
^D
$ chmod +x $_
$ GIT_SSH=ssh-acct-one git clone ssh://[email protected]/...

…and the same for the second identity file. (The call on the last line assumes you have ~/bin on your $PATH).

The reason for this clumsiness is that Git passes $GIT_SSH directly to exec() and not to /bin/sh -c or the like, so no ususal shell expansions are performed.

When debugging, you can also use GIT_TRACE=1 to make Git show you what is being executed and modify your scripts passng -vvv to ssh to make that report what's being done (including what key (identity file) it's using).

Upvotes: 0

Related Questions