This is to setup ssh for wsl so that I can connect windsurf to wsl before their official support
First setup and start the ssh server on wsl
sudo apt install ssh
sudo systemctl start ssh
sudo systemctl enable ssh
In windows set up port forwarding to your wsl distro by running the following in PowerShell
$EXT_PORT=2222
$WSL_PORT=22
netsh interface portproxy add v4tov4 listenport=$EXT_PORT listenaddress=0.0.0.0 connectport=$WSL_PORT connectaddress=127.0.0.1
Now just connect to your wsl machine like this
ssh user@<windowsmachineIP> -p 2222
ssh vio@localhost -p 2222
Now to make this passwordless login we need to setup key based login
On windows run
ssh-keygen -t rsa -b 4096
- When asked for file location, press Enter for default (usually
C:\Users\YourUsername\.ssh\id_rsa
) - Leave passphrase empty for passwordless login (just press Enter twice)
- This creates two files:
-
id_rsa
(private key) -
id_rsa.pub
(public key)
-
Create SSH Config on Windows:
# Create .ssh directory if it doesn't exist
mkdir -Force "$env:USERPROFILE\.ssh"
# Create/edit config file using Notepad
notepad "$env:USERPROFILE\.ssh\config"
Add these lines to the config file:
Host myserver
HostName your-server-ip
User your-linux-username
IdentityFile C:\Users\YourWindowsUsername\.ssh\id_rsa
Replace:
-
myserver
with whatever name you want -
your-server-ip
with your server's IP address -
your-linux-username
with your Linux server username -
YourWindowsUsername
with your Windows username
Here is mine :
Host local_wsl
HostName localhost
User vio
Port 2222
IdentityFile C:\Users\vio\.ssh\id_rsa
Now copy over the pub key and add it to appropriate files
Get-Content "$env:USERPROFILE\.ssh\id_rsa.pub"
PS C:\Users\vio> ssh your-linux-username@your-server-ip "mkdir -p ~/.ssh && echo '$PUBKEY' >> ~/.ssh/authorized_keys && chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"
The command would be like this for me:
ssh vio@localhost -p 2222 "mkdir -p ~/.ssh && echo '$PUBKEY' >> ~/.ssh/authorized_keys && chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"
Things that might fail or be different for you
- Opening up the correct ports
- Enabling Key based auth and disabling password requirement in the ssh server ( in this case our wsl )
- File permissions
- SSH client not installed in windows
Now to connect to WSL from windows installation of windsurf simply click on connect to SSH Host button on the bottom left of editor, click on the remote ssh option. Your config must ideally be there and clicking on it should work
Top comments (0)