6

I am running Ubuntu 16.04 within a Windows Subsystem for Linux (WSL) shell on Windows 10. From within a terminal I can start ssh-agent and use it many times over the life of that terminal. I can see the ssh-agent running from ps, and also from the Windows 10 task manager.

However, if I start a 2nd WSL bash terminal from the start menu, it is unaware of the ssh-agent. Oddly, if I start a 2nd terminal from the windows command line, it works. A possible workaround would be to open new bash windows from the existing one, but I don't know how to do that either.

Is there any solution to this?

3

Perhaps not: it sounds as if ssh-agent is being run in the usual way, where it is the parent process of your shell. That other window has its own hierarchy of process parent/child which doesn't tie into the window that you are using.

One way to work with this feature of ssh-agent is to run screen (or tmux) within the window where you have started the agent. Those screen/tmux windows are also children of the ssh-agent process, and can talk to it without problems.

2

You can modify your .bashrc to find an existing running ssh-agent.

I use something based on the gist

# ssh-agent configuration
if [ -z "$(pgrep ssh-agent)" ]; then
    rm -rf /tmp/ssh-*
    eval $(ssh-agent -s) > /dev/null
else
    export SSH_AGENT_PID=$(pgrep ssh-agent)
    export SSH_AUTH_SOCK=$(find /tmp/ssh-* -name agent.*)
fi

# optional... potentially annoying
#if [ "$(ssh-add -l)" == "The agent has no identities." ]; then
#    ssh-add
#fi
1

I prefer this script that use ssh-add to find if your keys has already been added

ssh-add -l &>/dev/null
if [ "$?" == 2 ]; then
  test -r ~/.ssh-agent && \
    eval "$(<~/.ssh-agent)" >/dev/null

  ssh-add -l &>/dev/null
  if [ "$?" == 2 ]; then
    (umask 066; ssh-agent > ~/.ssh-agent)
    eval "$(<~/.ssh-agent)" >/dev/null
    ssh-add
  fi
fi

You can read more about it on: http://rabexc.org/posts/pitfalls-of-ssh-agents

0

There is now. Install the latest version of OpenSSH for windows, and then get the ssh-agent-wsl helper. Follow the instructions, and you'll very easily get a working way to share credentials across WSL and Windows! It works like a charm!

0

One possible solution to your problem is the work-around / hack that I am using at the time of writing. However, I am also searching for another easy solution. I am using Windows 10 Pro Version 1903 Build 18362.418 and Ubuntu 18.04 in WSL.

I noticed that when ever I start 'ssh-agent' in WSL, a process of 'ssh-agent' is started in Windows 10. ssh-agent process in Windows 10 Task Manager

The work-around / hack I am using is that I start 'ssh-agent' is WSL by using ssh-agent, It gives me some output like

SSH_AUTH_SOCK=/tmp/ssh-4T3ZkVnWhIA7/agent.19; export SSH_AUTH_SOCK; SSH_AGENT_PID=20; export SSH_AGENT_PID;

I copy these commands and paste / execute them in WSL to connect with 'ssh-agent' and then save these commands in a text file in windows 10.

I add keys to 'ssh-agent' in WSL as usual.

Whenever I open a new WSL session, I simple paste / execute these saved commands in New WSL session terminal and the new WSL session connects to already existing 'ssh-agent' process and I don't have to start a new 'ssh-agent' and add keys again.

EDIT 1: Recently I used another work-around / hack. Instead of coping the output generated by ssh-agent and then pasting the copied content to New WSL session terminal to use already started 'ssh agent'.

I performed these commands in first WSL session terminal.

ssh-agent > ssh_env source ssh_env

and then if I needed New WSL session terminal, I just started it and used

source ssh_env

to connect with already started 'ssh agent'. This just reduced the steps of coping, saving and later using the output generated by ssh-agent that I had mentioned before.

Hope this helps.

PS. I don't launch WSL from Windows 10 Command Prompt using wsl.exe.

I usually open WSL form 'Start Menu'. launching WSL from start menu

New contributor
Umair Khan is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.