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

Credit: https://superuser.com/questions/1327633/how-to-maintain-ssh-agent-login-session-with-windows-10s-new-openssh-and-powers

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.

env=~/.ssh/agent.env

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

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

agent_load_env

# 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
    agent_start
    ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    ssh-add
fi

unset env
Principal Bioinformatics Scientist

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