8

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?

8 Answers 8

4

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
4

Just spotting this due to another new answer, but most of the answers here suggest some form of startup script adjustment (e.g. ~/.bashrc). Rather than reinvent the wheel, I would suggest keychain.

After installing (available in most distro repos, e.g. sudo apt install keychain), just add:

eval $(keychain --eval --agents ssh id_rsa)

... to your ~/.bashrc. This will correctly set your SSH_AGENT_PID and SSH_AUTH_SOCK variables for the current session to point to the existing ssh-agent or start a new one if needed. This way only one shared ssh-agent is used for all open WSL/terminal sessions.

Fun tidbit -- keychain was originally written by Daniel Robbins, who also founded Gentoo Linux.

1
  • 1
    This is so much simpler than the above answers, and it is similar to MacOS which simplifies the overhead required to maintain multiple systems. Dec 7, 2022 at 22:29
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

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!

2
  • 1
    It looks like wsl-agent-ssl has replaced the helper in the above answer (which I think should be the accepted answer btw). Adding a link here in case the latter project gets archived at some point. Feb 12, 2020 at 13:39
  • While this may be useful to some, it doesn't seem to me that the original question is about sharing between Windows and WSL -- It's about sharing between multiple WSL terminal sessions, which can be done using tools already available in most distributions. Also, for future readers, note that this answer was written before WSL2 existed. The project that is linked (and its replacement) is specifically for WSL1 and won't work with WSL2. However, there are directions on the replacement project on how share Windows/WSL agents in WSL2. Oct 7, 2022 at 17:47
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

0

There is an even easier solution to that, automate the process by adding:

source <(ssh-agent)

to your .bashrc, and each time you login you will get the env variables automatically.

0

It can be dealt with a POSIX-shell grammar script to be added to ~/.profile as it does not depend on any Bash or Zsh specific feature.

# Setup ssh-agent if none available
__agent_file="$HOME/.ssh/agent"
if ! source "$__agent_file" > /dev/null 2>&1 ||
   ! ps -p "$SSH_AGENT_PID" -o pid= > /dev/null
then eval "$(ssh-agent -s | { umask 077; tee "$__agent_file"; })"
fi
unset __agent_file
# Registers default key
if ! ssh-add -l > /dev/null
then ssh-add
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .