I'm having some trouble getting two different SSH keys/GitHub accounts to play well together. I have the following setup:
Repos accessible from one account using git@github.com:accountname
Repos accessible from another account using git@github.com:anotheraccount
Each account has its own SSH key. Both SSH keys have been added and I have created a config file. I don't believe the config file is correct though. I'm not quite sure how to specify that repos accessed using git@github.com:accountname
should use id_rsa
and git@github.com:anotheraccount
should use id_rsa_anotheraccount
.
I used,
It wokred fine.
Use the above setting in your .ssh/config file for different rsa keys for different usernames.
In my case none of the solutions above solved my issue, but ssh-agent does. Basically, I did the following:
Generate key pair using ssh-keygen shown below. It will generate a key pair (in this example
.\keyfile
and.\keyfile.pub
)ssh-keygen -t rsa -b 4096 -C "yourname@yourdomain" -f keyfile
Upload
keyfile.pub
to the git providerps -ef | grep ssh-agent
to see if it is running already)ssh-add .\keyfile
to add credentialsgit clone git@provider:username/project.git
As a complement of @stefano 's answer, It is better to use command with
-f
when generate a new SSH key for another account,Since
id_rsa_work
file doesn't exist in path~/.ssh/
, and I create this file manually, and it doesn't work :(Let's say
alice
is a github.com user, with 2 or more private repositoriesrepoN
. For this example we'll work with just two repositories namedrepo1
andrepo2
https://github.com/alice/repo1
https://github.com/alice/repo2
You need to be to pull from these repositories without entering a passwords probably on a server, or on multiple servers. You want to perform
git pull origin master
for example, and you want this to happen without asking for a password.You don't like dealing with ssh-agent, you have discovered (or you're discovering now) about
~/.ssh/config
a file that let's your ssh client know what private key to use depending on Hostname and username, with a simple configuration entry that looks like this:So you went ahead and created your
(alice_github.id_rsa, alice_github.id_rsa.pub)
keypair, you then also went to your repository's.git/config
file and you modified the url of your remoteorigin
to be something like this:And finally you went to the repository
Settings > Deploy keys
section and added the contents ofalice_github.id_rsa.pub
At this point you could do your
git pull origin master
without entering a password without issue.but what about the second repository?
So your instinct will be to grab that key and add it to
repo2
's Deploy keys, but github.com will error out and tell you that the key is already being used.Now you go and generate another key (using
ssh-keygen -t rsa -C "alice@alice.com"
without passwords of course), and so that this doesn't become a mess, you will now name your keys like this:repo1
keypair:(repo1.alice_github.id_rsa, repo1.alice_github.id_rsa.pub)
repo2
keypair:(repo2.alice_github.id_rsa, repo2.alice_github.id_rsa.pub)
You will now put the new public key on
repo2
's Deploy keys configuration at github.com, but now you have an ssh problem to deal with.How can ssh tell which key to use if the repositories are hosted on the same
github.com
domain?Your
.ssh/config
file points togithub.com
and it doesn't know which key to use when it's time to do the pull.So I found a trick with github.com. You can tell your ssh client that each repository lives in a different github.com subdomain, in these cases, they will be
repo1.github.com
andrepo2.github.com
So first thing is editing the
.git/config
files on your repo clones, so they look like this instead:For repo1
For repo2
And then, on your
.ssh/config
file, now you will be able to enter a configuration for each subdomain :)Now you are able to
git pull origin master
without entering any passwords from both repositories.If you have multiple machines, you could copy the keys to each of the machines and reuse them, but I'd advise doing the leg work to generate 1 key per machine and repo. You will have a lot more keys to handle, but you will be less vulnerable if one gets compromised.
Andy Lester's response is accurate but I found an important extra step I needed to make to get this to work. In trying to get two profiles set up, one for personal and one for work, my
~/.ssh/config
was roughly as follows:My work profile didn't take until I did a
ssh-add ~/.ssh/work_rsa
. After that connections to github used the correct profile. Previously they defaulted to the first public key.For Could not open a connection to your authentication agent when using
ssh-add
,check: https://stackoverflow.com/a/17695338/1760313
Use the
IdentityFile
parameter in your~/.ssh/config
: