How to Generate Multiple Ssh Public Key and Configure Those on Windows Machine from Gitbash

How to generate multiple SSH public key and configure those on windows machine from gitbash?

For the windows machine, you have to do one more configuration. Just follow the steps in below (if you're using the Git Bash):

  1. Go to the .ssh directory /c/Users/PC_USER_NAME/.ssh/, click right mouse button and choose "Git Bash Here"
  2. Create a file named "config" with the following command:
touch config

  1. Now open the config file with the command:
nano config

  1. Now write the following lines inside the config file

Let's assume you've created two files named id_rsa_hub for Github and id_rsa_lab for GitLab

# GITHUB
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_hub

# GITLAB
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_lab

Switching between multiple ssh keys in Git on Windows

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

How can I add an already generated SSH key to git bash?

On windows you might need to start the ssh agent like this

# start the ssh-agent in the background
$ eval $(ssh-agent -s)
> Agent pid 59566

Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_rsa in the command with the name of your private key file.

$ ssh-add <path/to/key>

Got this information from here under "Adding your SSH key to the ssh-agent":
https://help.github.com/en/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#adding-your-ssh-key-to-the-ssh-agent

Multiple ssh-keys for bitbucket server on Windows 10

You should replace the git@bitbucket.org portion of clone URLs with bitbucket.org-user4work when you want to access a repository with work credentials

You should replace the git@bitbucket.org portion of clone URLs with bitbucket.org-user4home when you want to access a repository with personal credentials

So, for example, if you have a work repository that is originally cloned using:

git clone git@bitbucket.org:organization/project.git

You should instead run

git clone bitbucket.org-user4work:organization/project.git

Running SSH Agent when starting Git Bash on Windows

2013: In a git bash session, you can add a script to ~/.profile or ~/.bashrc (with ~ being usually set to %USERPROFILE%), in order for said session to launch automatically the ssh-agent.

If the file doesn't exist, just create it.

This is what GitHub describes in "Working with SSH key passphrases".

The "Auto-launching ssh-agent on Git for Windows" section of that article has a robust script that checks if the agent is running or not.

Below is just a snippet, see the GitHub article for the full solution.

# This is just a snippet. See the article above.
if ! agent_is_running; then
agent_start
ssh-add
elif ! agent_has_keys; then
ssh-add
fi

Other Resources:

"Getting ssh-agent to work with git run from windows command shell" has a similar script, but I'd refer to the GitHub article above primarily, which is more robust and up to date.


hardsetting adds in the comments (2018):

If you want to enter the passphrase the first time you need it, and not when opening a shell, the cleanest way to me is:

  • removing the ssh-add from the .bash_profile, and
  • adding "AddKeysToAgent yes" to your .ssh/config file (see "How to make ssh-agent automatically add the key on demand?").

This way you don't even have to remember running ssh-add.


And Tao adds in the comments (2022):

It's worth noting why this script makes particular sense in Windows, vs (for example) the more standard linuxey script noted by @JigneshGohel in another answer:

By not relying on the SSH_AGENT_PID at all, this script works across different msys & cygwin environments.

An agent can be started in msys2, and still used in git bash, as the SSH_AUTH_SOCK path can be reached in either environment.

The PID from one environment cannot be queried in the other, so a PID-based approach keeps resetting/creating new ssh-agent processes on each switch.



Related Topics



Leave a reply



Submit