Running SSH Agent when starting Git Bash on Windows Running SSH Agent when starting Git Bash on Windows windows windows

Running SSH Agent when starting Git Bash on Windows


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-addelif ! agent_has_keys; then    ssh-addfi

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.


P.S: These instructions are in context of a Bash shell opened in Windows 10 Linux Subsystem and doesn't mention about sym-linking SSH keys generated in Windows with Bash on Ubuntu on Windows

1) Update your .bashrc by adding following in it

# Set up ssh-agentSSH_ENV="$HOME/.ssh/environment"function start_agent {    echo "Initializing new SSH agent..."    touch $SSH_ENV    chmod 600 "${SSH_ENV}"    /usr/bin/ssh-agent | sed 's/^echo/#echo/' >> "${SSH_ENV}"    . "${SSH_ENV}" > /dev/null    /usr/bin/ssh-add}# Source SSH settings, if applicableif [ -f "${SSH_ENV}" ]; then    . "${SSH_ENV}" > /dev/null    kill -0 $SSH_AGENT_PID 2>/dev/null || {        start_agent    }else    start_agentfi

2) Then run $ source ~/.bashrc to reload your config.

The above steps have been taken from https://github.com/abergs/ubuntuonwindows#2-start-an-bash-ssh-agent-on-launch

3) Create a SSH config file, if not present. Use following command for creating a new one: .ssh$ touch config

4) Add following to ~/.ssh/config

Host github.com-<YOUR_GITHUB_USERNAME> HostName github.comUser gitPreferredAuthentications publickeyIdentityFile ~/.ssh/id_work_gmail # path to your private keyAddKeysToAgent yesHost csexperimental.abc.comIdentityFile ~/.ssh/id_work_gmail # path to your private keyAddKeysToAgent yes<More hosts and github configs can be added in similar manner mentioned above>

5) Add your key to SSH agent using command $ ssh-add ~/.ssh/id_work_gmail and then you should be able to connect to your github account or remote host using ssh. For e.g. in context of above code examples:

$ ssh github.com-<YOUR_GITHUB_USERNAME>

or

$ ssh <USER>@csexperimental.abc.com

This adding of key to the SSH agent should be required to be performed only one-time.

6) Now logout of your Bash session on Windows Linux Subsystem i.e. exit all the Bash consoles again and start a new console again and try to SSH to your Github Host or other host as configured in SSH config file and it should work without needing any extra steps.

Note:

Thanks.


I found the smoothest way to achieve this was using Pageant as the SSH agent and plink.

You need to have a putty session configured for the hostname that is used in your remote.

You will also need plink.exe which can be downloaded from the same site as putty.

And you need Pageant running with your key loaded. I have a shortcut to pageant in my startup folder that loads my SSH key when I log in.

When you install git-scm you can then specify it to use tortoise/plink rather than OpenSSH.

The net effect is you can open git-bash whenever you like and push/pull without being challenged for passphrases.

Same applies with putty and WinSCP sessions when pageant has your key loaded. It makes life a hell of a lot easier (and secure).