Set up SSH for WSL to use windsurf IDE before official WSL support

govindsb

Govind.S.B

Posted on November 20, 2024

Set up SSH for WSL to use windsurf IDE before official WSL support

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Now just connect to your wsl machine like this

ssh user@<windowsmachineIP> -p 2222
Enter fullscreen mode Exit fullscreen mode
ssh vio@localhost -p 2222
Enter fullscreen mode Exit fullscreen mode

Now to make this passwordless login we need to setup key based login

On windows run

ssh-keygen -t rsa -b 4096
Enter fullscreen mode Exit fullscreen mode
  • 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"
Enter fullscreen mode Exit fullscreen mode

Add these lines to the config file:

Host myserver
    HostName your-server-ip
    User your-linux-username
    IdentityFile C:\Users\YourWindowsUsername\.ssh\id_rsa
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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

đź’– đź’Ş đź™… đźš©
govindsb
Govind.S.B

Posted on November 20, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related