Skip to main content
deleted 11 characters in body
Source Link
Arad
  • 121
  • 4

Inspired by Collin Anderson's answer, I wrote an alternative script (that needs to be put inside of your .bashrc file) which unlike his, actually works in environmentsan environment like Git Bash and WSL (where the /tmp directory doesn't get cleared, which is what his solution relies on):

source ~/.ssh/agent_out &> /dev/null
if ! ps -p $SSH_AGENT_PID &> /dev/null
then
  ssh-agent > ~/.ssh/agent_out
  source ~/ssh/agent_out &> /dev/null
fi

To understand what this script is actually doing, let’s consider what it would do the first time it’s run:

  1. It spins up an SSH agent, stores the output of the ssh-agent command inside a file called agent_out in the user’s .ssh directory for later use. The output of the ssh-agent command contains statements to assign the right values to environment variables like SSH_AUTH_SOCK and SSH_AGENT_PID.
  2. For every new shell session, it first executes the output of the last ssh-agent command which was stored in the ~/.ssh/agent_out file, and then checks whether the process with the ID included in SSH_AGENT_PID actually exists or not. If it doesn’t, it performs the first step.

This, coupled with the new SSH config option AddKeysToAgent — see the manual —, would yield a nice user experience, and eliminate the need for third-party tools like keychain and ssh-ident for the most part:

~/.ssh/config:

AddKeysToAgent yes

Inspired by Collin Anderson's answer, I wrote an alternative script (that needs to be put inside of your .bashrc file) which unlike his, actually works in environments like Git Bash and WSL (where the /tmp directory doesn't get cleared, which is what his solution relies on):

source ~/.ssh/agent_out &> /dev/null
if ! ps -p $SSH_AGENT_PID &> /dev/null
then
  ssh-agent > ~/.ssh/agent_out
  source ~/ssh/agent_out &> /dev/null
fi

To understand what this script is actually doing, let’s consider what it would do the first time it’s run:

  1. It spins up an SSH agent, stores the output of the ssh-agent command inside a file called agent_out in the user’s .ssh directory for later use. The output of the ssh-agent command contains statements to assign the right values to environment variables like SSH_AUTH_SOCK and SSH_AGENT_PID.
  2. For every new shell session, it first executes the output of the last ssh-agent command which was stored in the ~/.ssh/agent_out file, and then checks whether the process with the ID included in SSH_AGENT_PID actually exists or not. If it doesn’t, it performs the first step.

This, coupled with the new SSH config option AddKeysToAgent — see the manual —, would yield a nice user experience, and eliminate the need for third-party tools like keychain and ssh-ident for the most part:

~/.ssh/config:

AddKeysToAgent yes

Inspired by Collin Anderson's answer, I wrote an alternative script (that needs to be put inside of your .bashrc file) which unlike his, actually works in an environment like WSL (where the /tmp directory doesn't get cleared, which is what his solution relies on):

source ~/.ssh/agent_out &> /dev/null
if ! ps -p $SSH_AGENT_PID &> /dev/null
then
  ssh-agent > ~/.ssh/agent_out
  source ~/ssh/agent_out &> /dev/null
fi

To understand what this script is actually doing, let’s consider what it would do the first time it’s run:

  1. It spins up an SSH agent, stores the output of the ssh-agent command inside a file called agent_out in the user’s .ssh directory for later use. The output of the ssh-agent command contains statements to assign the right values to environment variables like SSH_AUTH_SOCK and SSH_AGENT_PID.
  2. For every new shell session, it first executes the output of the last ssh-agent command which was stored in the ~/.ssh/agent_out file, and then checks whether the process with the ID included in SSH_AGENT_PID actually exists or not. If it doesn’t, it performs the first step.

This, coupled with the new SSH config option AddKeysToAgent — see the manual —, would yield a nice user experience, and eliminate the need for third-party tools like keychain and ssh-ident for the most part:

~/.ssh/config:

AddKeysToAgent yes
Source Link
Arad
  • 121
  • 4

Inspired by Collin Anderson's answer, I wrote an alternative script (that needs to be put inside of your .bashrc file) which unlike his, actually works in environments like Git Bash and WSL (where the /tmp directory doesn't get cleared, which is what his solution relies on):

source ~/.ssh/agent_out &> /dev/null
if ! ps -p $SSH_AGENT_PID &> /dev/null
then
  ssh-agent > ~/.ssh/agent_out
  source ~/ssh/agent_out &> /dev/null
fi

To understand what this script is actually doing, let’s consider what it would do the first time it’s run:

  1. It spins up an SSH agent, stores the output of the ssh-agent command inside a file called agent_out in the user’s .ssh directory for later use. The output of the ssh-agent command contains statements to assign the right values to environment variables like SSH_AUTH_SOCK and SSH_AGENT_PID.
  2. For every new shell session, it first executes the output of the last ssh-agent command which was stored in the ~/.ssh/agent_out file, and then checks whether the process with the ID included in SSH_AGENT_PID actually exists or not. If it doesn’t, it performs the first step.

This, coupled with the new SSH config option AddKeysToAgent — see the manual —, would yield a nice user experience, and eliminate the need for third-party tools like keychain and ssh-ident for the most part:

~/.ssh/config:

AddKeysToAgent yes