Automatically starting ssh-agent when powershell or git-bash are started

Including a snippet for .bashrc on linux-like environment

Windows environment

It's best to configure OpenSSH Authentication Agent service to automatically start. Alternatively, you can start it manually every time when opening powershell for the first time:

Start-Service ssh-agent

To have SSH agent to automatically start with Windows, you can run (from elevated powershell prompt):

Set-Service ssh-agent -StartupType Automatic 

After that, you need to add your ssh key once:

ssh-add C:\Users\your-name\ssh\id_rsa 

Now everytime the ssh-agent is started, the key will be there. You can check which keys are registered with the ssh-agent:

ssh-add -l


Linux-like environement

On linux or in git-for-windows environment, I use the following snippet in my .bashrc to achieve the same effect:

# This is used to start ssh-agent once when git-bash is started.
# Saves typing the ssh key password every time you interact with
# a remote repo.


agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }

agent_start () {
    (umask 077; ssh-agent >| "$env")
    . "$env" >| /dev/null ; }


# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then

unset env
Dmitry Avtonomov
Software Engineer

My main research interests are data visualization and processing for mass spectrometry.