57

I need to have multiple keys in my client to access two different accounts on Repository hosting. See https://redefy.repositoryhosting.com/support -> How do I manage multiple accounts with multiple keypairs on my client?

I do not understand how to change between different ssh keys in Git on Windows, can anybody help me?

Tobias Ekblom
  • 795
  • 2
  • 7
  • 11
  • Are you using OpenSSH or Plink for your SSH connections? – Justin ᚅᚔᚈᚄᚒᚔ Mar 12 '12 at 19:01
  • 1
    You can find more about setting up ssh in [Best way to use multiple ssh private keys on one client](http://stackoverflow.com/questions/2419566/best-way-to-use-multiple-ssh-private-keys-on-one-client) – peron May 06 '12 at 07:52

5 Answers5

75

I assume you use git bash and openssh.

Like what it's written in the article, you can make a configuration file for ssh client that lists all of your accounts. You can write the following configuration in your own ssh client configuration file in ~/.ssh/config

Host account-one
HostName server.example.com
User user-one
IdentityFile ~/.ssh/key-one

Host account-two
HostName server.example.com
User user-two
IdentityFile ~/.ssh/key-two

What it says is you define two, kind of, "host aliases" named account-one and account-two. If you use them, when making connection, the ssh client will use the corresponding HostName, User, and IdentityFile for the server address, username, and ssh key file. With this you can use them to access your accounts and keys at even the same server.

In git, you can define two remotes using them

$ git remote add one account-one:repository.git
$ git remote add two account-two:repository.git

then you can push to those remotes

$ git push one master
$ git push two master
fajran
  • 2,769
  • 22
  • 13
  • hi, How will I clone using this config file. "$ git clone one repositoyr.git master" gives error "Too many arguments" – Tariq Dec 15 '14 at 11:52
  • 1
    Don't forget to add colon between the hostname and repository path. In your case it would be `one:repositoyr.git` – fajran Dec 16 '14 at 08:29
  • as specified in document here http://linux.die.net/man/5/ssh_config IdentityFile Specifies a file from which the user's RSA or DSA authentication identity is read. The file name may use the tilde syntax to refer to a user's home directory or one of the following escape characters: '%d' (local user's home directory), '%u' (local user name), '%l' (local host name), '%h' (remote host name) or '%r' (remote user name). for example IdentityFile %u/.ssh/id_rsa – Shridutt Kothari Jul 03 '15 at 10:07
  • 2
    Is there any way to access two repos *on the same host* using separate user for each repo? – Putnik Aug 05 '17 at 16:35
  • @Putnik see https://danielwertheim.se/help-i-need-multiple-accounts-and-ssh-keys-to-the-same-git-provider/ – JohnLBevan May 01 '19 at 14:48
  • @fajran how should we create agent for them? Or how do we force to execute this config? I have added them, and have created the agents for both (first is working but second is not), I'm getting the error below while using second one: `Unable to open connection: Host does not existfatal: Could not read from remote repository.` **My git config is:** `yilmazhasan:git@github.com:yilmazhasan/practice-workspace.git` **ssh config is:** `Host yilmazhasan HostName https://github.com/ User yilmazhasan IdentityFile C:\\Users\\hasany\\.ssh\\id_rsa-yilmazhasan` I am using windows – hasany Jan 16 '21 at 14:49
  • @farjan I have added them to config but, when I test the connection (`ssh account-one`), it gives `Permission denied` message, although I am using that without problems with git commands. I think it has been kept in windows. (Maybe done when I have run ssh-add) I wonder when that config file is considered. – hasany Jan 17 '21 at 15:58
18

Which key is used for which server is handled by the SSH program that git is using to connect. In the default setup this should be the command line SSH client (openSSH?).

Using openSSH you can configure particular keyfiles for particular hosts in the ~/.ssh/config file:

Host foo.example.com
  IdentityFile ~/.ssh/foo.example.com-id_rsa

Host bar.example.com
  IdentityFile ~/.ssh/bar.example.com-id_rsa

Where ~/.ssh/*.example.com-id_rsa are the private key files for each server.

I hope this helps.

mamapitufo
  • 4,680
  • 2
  • 25
  • 19
  • 3
    Should the paths have forward slashes even if you are running on Windows? – Benny Code Aug 08 '14 at 15:10
  • all of those paths are unix-like, so if you are using cygwin on windows,they'll work, if you are using something else as your shell, you will need to adjust them. – mamapitufo Aug 11 '14 at 12:25
  • 4
    Thanks! I just saw that you can also use sth. like "/C/Users/Benny/.ssh/id_rsa" on Windows. – Benny Code Aug 11 '14 at 17:24
  • I am connecting to a non-default port (22)in SSH. Where shall I specify this port in the config file? – Tariq Dec 15 '14 at 10:49
  • just include `Port 2666` or whatever the port is, in the specific host section. – mamapitufo Dec 16 '14 at 16:52
  • What is the location for ~/.ssh/config for windows system? – Krunal Sep 04 '20 at 10:56
  • @Krunal, I guess it would depend on the SSH software you are using. On Windows I've always used the client included with the Git installation (typically called "Git Bash"), and in that case, the paths all look UNIX-like, as in my original response. If you are using something else, you need to check the documentation for that particular software. – mamapitufo Sep 08 '20 at 08:58
  • @Krunal usual default is C:\Users\\.ssh – Daniel.P. Dec 12 '20 at 13:33
2

On Windows you should try Pageant an SSH authentication agent for PuTTY, PSCP, PSFTP, and Plink. This tool can manage yout ssh keys and its pass-phrases. To use it together with Git you have to install Putty and link to the plink.exe setting the GIT_SSH variable.

  1. Install Putty and friends (http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html)
  2. Set GIT_SSH

    set GIT_SSH=<path-to-plink.exe>

  3. Start Pageant and add you keys
  4. Run Git

hth Daniel

Daniel S.
  • 628
  • 6
  • 12
  • I am awared that this thread is very old, but I have similar problem. Don't you need everytime if you want to work git to start Pageant? – Doan Dec 27 '19 at 00:06
  • @Doan Yes you need. I added it to scheduled tasks. – grzegorz May 23 '20 at 22:39
1

In my case, I use

Host account-one
User git

not

Host account-one
User user-any
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31951372) – A.E. Drew Jun 08 '22 at 03:52
0

I'll answer this a little indirectly. I have previously used git bash and I've found that when I'm using git via the git bash shell, that it behaves just like a mac or linux bash shell. Meaning, when using git bash, that I can answer your question like:

'If you use git bash, you can manage multiple accounts just as you would if you were on linux or mac, using ssh-agent/ssh-add and friends'

sethcall
  • 2,837
  • 1
  • 19
  • 22