WhitewaterFoundry / Fedora-Remix-for-WSL

Fedora Remix for Windows Subsystem for Linux.
Other
698 stars 51 forks source link

Idea: provide an easy way to set up a shared ssh-agent instance across WSL sessions #84

Open ncoghlan opened 4 years ago

ncoghlan commented 4 years ago

When setting up WSL, one of the first things I do is add the following snippet to the end of ~/.bashrc so all interactive shells have access to ssh-agent by default:

# Set up ssh-agent for WSL
SSH_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 applicable
if [ -f "${SSH_ENV}" ]; then
    . "${SSH_ENV}" > /dev/null
    kill -0 $SSH_AGENT_PID 2>/dev/null || {
        start_agent
    }
else
    start_agent
fi

Now, as discussed at https://stackoverflow.com/questions/18880024/start-ssh-agent-on-login this particular snippet has its drawbacks, but there still seems to be an opportunity here to have an off-by-default snippet in the default .bashrc that can be turned on with a simple flag like:

# Uncomment next line to automatically start ssh-agent when logging in to WSL
# AUTOSTART_SSH_AGENT=1

(Edit: I did consider proposing this to upstream Fedora instead, but I think the idea makes much less sense there, as Fedora Workstation already provides this functionality at the login manager level, and headless Fedora instances are far more likely to be server hosts than they are to be client development environments).

crramirez commented 4 years ago

Excellent, thank you for the suggestion