I use WSL with Ubuntu on my laptop to develop on Linux while I'm away from my desktop (with native Linux, where I don't experience any problems like this). I have private git repo setup, and I use ssh to connect to it. I have ssh configured (in .ssh/config), and I have multiple keys for my VPS, GitHub, other VPS user account etc. so I use ssh-add on my desktop to manage them. But on my laptop, in WSL, when I try to connect to my VPS, ssh asks for the passphrase to the key. When I try to do ssh-add .ssh/mykey I get an error:

$ ssh-add .ssh/mykey
Could not open a connection to your authentication agent.

But ssh-agent is running:

$ ps -e | grep ssh
4455 ?        00:00:00 ssh-agent
5517 ?        00:00:00 sshd
6164 pts/0    00:00:00 ssh

I don't see why is there a problem. If I kill currently running ssh-agent and launch it again (using eval $(ssh-agent -s)) it works fine.

  • For normal SSH in Windows to WSL in localhost, the configuration PasswordAuthentication yes must be added in /etc/ssh/sshd_config file. – Biswapriyo Oct 2 '18 at 20:23

Looks like you are starting ssh-agent but you are not setting up the variable pointing to it.

When you just run ssh-agent it will output something like:

SSH_AUTH_SOCK=/tmp/ssh-UfASAcTfsiJm/agent.77; export SSH_AUTH_SOCK; SSH_AGENT_PID=78; export SSH_AGENT_PID;

Copy that as commands and paste to bash, or make your life simpler by just executing:

eval `ssh-agent`

After that variables will be set and ssh-add will know how to connect to ssh-agent.

| improve this answer | |

The problem is that with WSL (as of 2019) you can start multiple shells from the Window subsystem, they are aware of each others but don't inherit any variables. Therefore they don't know that ssh-agent is already running.

I managed to get it working by adding this bit of code to my .bashrc:

source /tmp/ssh-agent >/dev/null 2>&1
ssh-add 2> /dev/null
if [ $? -ne 0 ]; then
  ssh-agent >/tmp/ssh-agent
  source /tmp/ssh-agent >/dev/null 2>&1

Maybe not the most eloquent code, but it solves the problem.

| improve this answer | |

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.