Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Setup SSH Authentication for Git Bash on Windows

Setup SSH Authentication for Git Bash on Windows

Prepararation

  1. Create a folder at the root of your user home folder (Example: C:/Users/uname/) called .ssh.
  2. Create the following files if they do not already exist (paths begin from the root of your user home folder):
    • .ssh/config
    • .bash_profile
    • .bashrc

Create a New SSH Key

Follow the steps in the section named "Generating a new SSH Key" found in the following documentation from GitHub: Generating a new SSH key and adding it to the ssh-agent

Configure SSH for Git Hosting Server

Add the following text to .ssh/config (.ssh should be found in the root of your user home folder):

Host github.com
 Hostname github.com
 IdentityFile ~/.ssh/id_rsa

Enable SSH Agent Startup Whenever Git Bash is Started

First, ensure that following lines are added to .bash_profile, which should be found in your root user home folder:

test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc

Now, add the following text to .bashrc, which should be found in your root user home folder:

# Start SSH Agent
#----------------------------

SSH_ENV="$HOME/.ssh/environment"

function run_ssh_env {
  . "${SSH_ENV}" > /dev/null
}

function start_ssh_agent {
  echo "Initializing new SSH agent..."
  ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
  echo "succeeded"
  chmod 600 "${SSH_ENV}"

  run_ssh_env;

  ssh-add ~/.ssh/id_rsa;
}

if [ -f "${SSH_ENV}" ]; then
  run_ssh_env;
  ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
    start_ssh_agent;
  }
else
  start_ssh_agent;
fi
@pensebien

This comment has been minimized.

Copy link

pensebien commented Nov 28, 2017

Thank you for the gist. But I found it easy to just add to my ~./profile file
localUser@mycomputer MINGw64 / notepad /.profile
then I Paste this

`
# Start SSH Agent
#----------------------------

SSH_ENV="$HOME/.ssh/environment"

function run_ssh_env {
  . "${SSH_ENV}" > /dev/null
}

function start_ssh_agent {
  echo "Initializing new SSH agent..."
  ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
  echo "succeeded"
  chmod 600 "${SSH_ENV}"

  run_ssh_env;

  ssh-add ~/.ssh/id_rsa;
}

if [ -f "${SSH_ENV}" ]; then
  run_ssh_env;
  ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
    start_ssh_agent;
  }
else
  start_ssh_agent;
fi`

I also took time to read about ssh-agent

@rianquinn

This comment has been minimized.

Copy link

rianquinn commented Feb 27, 2018

This worked perfectly, Thanks

@leoreeves

This comment has been minimized.

Copy link

leoreeves commented Mar 6, 2018

@EmmaAkin, thanks that works great. FYI, there's a backtick at the start and end your code snippet that will cause an error.

@pauljohn32

This comment has been minimized.

Copy link

pauljohn32 commented Mar 18, 2018

The part that launches the ssh agent is valuable. However, I'm not persuaded there is a benefit in the git config file. The Default key name is "id_rsa" and that is what ssh will look for. I think the config file is not having an effect. At least for me...

In my case, the name of the key is different for every server and, for reasons I cannot understand, the ssh system only wants to look for "id_rsa". On Linux, this is a symptom of a permissions problem, permissions should be 700. But in Windows, it appears not possible to set permissions at all. In the Git bash shell, permissions always have g+r and o+r.

The config file is ignores, at least for me. You can see same result if you name your key in another file, put in

IdentityFile ~/.ssh/new_keyname

And it wont work. If it does work, I'd love to hear from you.

@smutel

This comment has been minimized.

Copy link

smutel commented Mar 22, 2018

If you use an old dsa key, put this in config PubkeyAcceptedKeyTypes=+ssh-dss

@gregbown

This comment has been minimized.

Copy link

gregbown commented May 14, 2018

Wow! First of all, thank you! I am astonished as to why none of this presented anywhere on GitHub? I spent a couple of hours looking for the missing steps, All they say is paste your public key here with no mention of how GitHub is supposed to find the private key on Windows, where it should be stored, what else is required, etc.

The only thing I would add to this is that if you are generating you keys with PuTTY Key Generator, you must select from the menu -> Conversions -> Export SSH key(force new file format) and save the private key as just "id_rsa" NOT id_rsa.ppk the default PuTTY format

Thank you again Brandon

@demiters

This comment has been minimized.

Copy link

demiters commented Aug 27, 2018

Perfect, thanks!

@fushenghua

This comment has been minimized.

Copy link

fushenghua commented Sep 4, 2018

good!

@bonovski

This comment has been minimized.

Copy link

bonovski commented Sep 13, 2018

Thank you, works perfectly!
bitbucket and github are really missing these crucial steps, lost almost 8 hours setting it up on windows.

@surjikal

This comment has been minimized.

Copy link

surjikal commented Sep 15, 2018

I added this to /etc/ssh/ssh_config so that I could use a shared deployment key for all users on my server. Thanks for the info!

Host bitbucket.org
 Hostname bitbucket.org
 IdentityFile /code/.ssh/id_rsa
@Tri125

This comment has been minimized.

Copy link

Tri125 commented Jan 2, 2019

If you want to add multiple keys to the agent simply replace the line ssh-add ~/.ssh/id_rsa; with the following block:

for key in ${HOME}/.ssh/id_*; do
     if grep -q PRIVATE "$key"; then
          /usr/bin/ssh-add "$key"
     fi
done

This will add every ssh key that start with id_
The inconvenience is that when the ssh-agent start, it's going to prompt you to enter the password for every keys even if you don't necessarily use them. I haven't found a solution for this.

You might want to use that with git for windows if, say, you have a different github account: 1 for your personal work and another for your professional work. Github won't let you re-use the same ssh key for both accounts so you need 2 keys.

To make this work, you will need to do 2 more steps. in ${HOME}/.ssh create the config file with the following content:

# Default Github

Host github.com
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa

# Example config for a secondary github account

Host github-corpo
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_corpo

In this example, we have the secondary ssh key named id_rsa_corpo.

If you already cloned the repo for the secondary key, change the remote to point to github-corpo.
If not, then just clone by replacing the host :

git@github-corpo:myUserName/myProject.git

@zeroboo

This comment has been minimized.

Copy link

zeroboo commented Jul 2, 2019

it works like a charm, thank you very much

@semiromid

This comment has been minimized.

Copy link

semiromid commented Aug 21, 2019

Thank you! But, every time after restarting the computer I have to re-enter the password.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.