Automatically starting ssh-agent when powershell or git-bash are started | Dmitry Avtonomov (2024)

Including a snippet for .bashrc on linux-like environment

2020-83-118 2 min read

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.envagent_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 runningagent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then agent_start ssh-addelif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then ssh-addfiunset env

Automatically starting ssh-agent when powershell or git-bash are started | Dmitry Avtonomov (1)

Principal Bioinformatics Scientist

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

Automatically starting ssh-agent when powershell or git-bash are started | Dmitry Avtonomov (2024)
Top Articles
Latest Posts
Article information

Author: Merrill Bechtelar CPA

Last Updated:

Views: 6550

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Merrill Bechtelar CPA

Birthday: 1996-05-19

Address: Apt. 114 873 White Lodge, Libbyfurt, CA 93006

Phone: +5983010455207

Job: Legacy Representative

Hobby: Blacksmithing, Urban exploration, Sudoku, Slacklining, Creative writing, Community, Letterboxing

Introduction: My name is Merrill Bechtelar CPA, I am a clean, agreeable, glorious, magnificent, witty, enchanting, comfortable person who loves writing and wants to share my knowledge and understanding with you.