WSL: Forward Windows SSH Agent to WSL
In this article, we will discuss how to forward the Windows SSH agent to WSL (Windows Subsystem for Linux), solving the issue of opening SSH connections that fail. The process involves using a simple script that automates the task, which you can find on GitHub.
What is an SSH Agent?
An SSH (Secure Shell) agent is a program that runs in the background and stores the private keys used for authentication when connecting to remote servers via SSH. It eliminates the need to repeatedly enter your password or passphrase when establishing connections.
Why Forward the Windows SSH Agent to WSL?
When working within the WSL environment, you might encounter problems connecting to remote servers using SSH due to missing or outdated keys. Forwarding the Windows SSH agent to WSL ensures that the private keys stored in the Windows agent are available within the WSL environment.
Prerequisites
Before we start the process of forwarding the Windows SSH agent to WSL, ensure you have the following installed:
- The latest version of WSL (available via Windows Update)
- An SSH client for Windows (e.g., OpenSSH as part of Windows 10 or later)
- A WSL Linux distribution (e.g., Ubuntu, available via Microsoft Store)
- A remote server requiring SSH-based authentication
Forwarding the Windows SSH Agent to WSL: A Simple Script
A convenient method to forward the Windows SSH agent to WSL is by using a helper script provided on GitHub. This script automates the following steps:
- Authentication request (to prompt for user permission)
- Connection to WSL using
wsl.exe - Executing
ssh-agentwithin WSL (when not already started) - Forwarding the authentication socket from the Windows SSH agent to WSL
Installing the Script
To install the script:
- Clone the repository onto your Windows machine:
git clone https://github.com/rupor-github/wsl-ssh-agent.git
Executing the Script
Run the script using a PowerShell session on your Windows machine:
.\wsl-ssh-agent\start.ps1
This action forwards the Windows SSH agent to WSL. To check that the connection has been established successfully:
$ WSL_SSH_AUTH_SOCK=$(wslpath -w "$(wslvar SSH_AUTH_SOCK)")
ssh-add -l
You should see the list of your SSH keys.
Setting Up Automatic Forwarding
To enable automatic forwarding of the Windows SSH agent to WSL:
- Modify the
start.ps1script by changing the-nflag in thessh-agentcommand to a dash (-d):
# Before
ssh-agent -n
<...>
# After
ssh-agent -d
Modifying Windows PATH Variable
Add the WSL-SSH-Agent direc tory to your Windows PATH variable:
- Search for
Environment Variablesin the Windows start menu and selectEdit the system environment variables. - Click on
Environment Variables. - Under
System Variables, locate thePathvariable and clickEdit. - Add the path to the
wsl-ssh-agentdirectory (e.g.,C:\Users\YourUsername\wsl-ssh-agent).
Creating a PowerShell Profile Script
Create a PowerShell profile script by executing:
notepad.exe $PROFILE
Add the following line to the script:
.\wsl-ssh-agent\start.ps1
Save and close the file.
This article has covered forwarding the Windows SSH agent to WSL, focusing on key concepts like the purpose of an SSH agent and why you might want to forward the agent. By using the provided script, you can quickly and easily set up automatic forwarding of the Windows SSH agent to WSL.