Skip to content

Instantly share code, notes, and snippets.

@luizcosta
Created June 16, 2025 16:54
Show Gist options
  • Save luizcosta/f3c9774ed8d19643fd818ed47c75933a to your computer and use it in GitHub Desktop.
Save luizcosta/f3c9774ed8d19643fd818ed47c75933a to your computer and use it in GitHub Desktop.
WSL Ubuntu SSH Setup for Cursor Remote Development - Connect from macOS to WSL2
#!/bin/bash
# WSL SSH Connection Setup Script for macOS
# This script helps configure SSH connection to WSL Ubuntu for Cursor remote development
echo "=== WSL SSH Connection Setup for Cursor ==="
echo
# Check if SSH key exists
if [ ! -f ~/.ssh/id_ed25519 ]; then
echo "No SSH key found. Generating a new one..."
ssh-keygen -t ed25519 -C "cursor-wsl-connection"
echo
fi
# Get WSL connection details
read -p "Enter your Windows machine IP address: " WINDOWS_IP
read -p "Enter your WSL username: " WSL_USER
read -p "Enter SSH port (default 2222): " SSH_PORT
SSH_PORT=${SSH_PORT:-2222}
# Display public key
echo
echo "Your SSH public key (copy this to WSL):"
echo "======================================="
cat ~/.ssh/id_ed25519.pub
echo "======================================="
echo
# Test connection
echo "Testing SSH connection..."
echo "Command: ssh -p $SSH_PORT $WSL_USER@$WINDOWS_IP"
echo
read -p "Do you want to add this connection to your SSH config? (y/n): " ADD_CONFIG
if [ "$ADD_CONFIG" = "y" ]; then
# Backup existing config
if [ -f ~/.ssh/config ]; then
cp ~/.ssh/config ~/.ssh/config.backup
echo "Backed up existing SSH config to ~/.ssh/config.backup"
fi
# Add WSL host to SSH config
echo "
Host wsl-ubuntu
HostName $WINDOWS_IP
Port $SSH_PORT
User $WSL_USER
IdentityFile ~/.ssh/id_ed25519
ServerAliveInterval 60
ServerAliveCountMax 3" >> ~/.ssh/config
echo "Added 'wsl-ubuntu' to your SSH config"
echo "You can now connect using: ssh wsl-ubuntu"
echo
fi
# Instructions for Cursor
echo "=== Cursor Setup Instructions ==="
echo "1. Open Cursor"
echo "2. Open Command Palette (Cmd+Shift+P)"
echo "3. Type 'Remote-SSH: Add New SSH Host'"
echo "4. Enter: ssh -p $SSH_PORT $WSL_USER@$WINDOWS_IP"
echo " Or if you added to config: ssh wsl-ubuntu"
echo "5. Select your SSH config file location"
echo "6. Then use 'Remote-SSH: Connect to Host' to connect"
echo
echo "=== Next Steps on WSL Ubuntu ==="
echo "1. Install OpenSSH server: sudo apt install openssh-server"
echo "2. Configure SSH port in /etc/ssh/sshd_config to $SSH_PORT"
echo "3. Add your public key to ~/.ssh/authorized_keys"
echo "4. Restart SSH: sudo systemctl restart ssh"
echo
echo "For detailed instructions, see: ~/wsl-ssh-setup.md"
# WSL Port Forwarding Script for SSH Access
# Run this script as Administrator on Windows
Write-Host "WSL2 Port Forwarding Script" -ForegroundColor Cyan
Write-Host "===========================" -ForegroundColor Cyan
Write-Host ""
# Get WSL IP address
$wslIP = wsl hostname -I
if ($wslIP -match '(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})') {
$wslIP = $matches[1]
Write-Host "WSL IP Address: $wslIP" -ForegroundColor Green
} else {
Write-Host "Failed to get WSL IP address" -ForegroundColor Red
exit 1
}
# SSH Port (change if you use a different port)
$sshPort = 2222
# Remove existing port proxy
Write-Host "Removing existing port proxy..." -ForegroundColor Yellow
netsh interface portproxy delete v4tov4 listenport=$sshPort listenaddress=0.0.0.0 2>$null
# Add new port proxy
Write-Host "Adding port proxy for SSH (port $sshPort)..." -ForegroundColor Yellow
netsh interface portproxy add v4tov4 listenport=$sshPort listenaddress=0.0.0.0 connectport=$sshPort connectaddress=$wslIP
# Show current port proxies
Write-Host ""
Write-Host "Current port proxies:" -ForegroundColor Cyan
netsh interface portproxy show v4tov4
# Get Windows IP for connection info
$windowsIP = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.InterfaceAlias -notlike "*Loopback*" -and $_.InterfaceAlias -notlike "*WSL*"}).IPAddress | Select-Object -First 1
Write-Host ""
Write-Host "Setup Complete!" -ForegroundColor Green
Write-Host "===============" -ForegroundColor Green
Write-Host "Connect from your Mac using:" -ForegroundColor Yellow
Write-Host " ssh -p $sshPort your-username@$windowsIP" -ForegroundColor White
Write-Host ""
Write-Host "Note: Run this script each time WSL starts or create a scheduled task." -ForegroundColor Gray

WSL Ubuntu SSH Setup for Cursor Remote Development

On WSL Ubuntu Instance

1. Install and Configure SSH Server

# Update packages
sudo apt update && sudo apt upgrade -y

# Install OpenSSH server
sudo apt install openssh-server -y

# Configure SSH to start automatically
sudo systemctl enable ssh
sudo systemctl start ssh

# Check SSH status
sudo systemctl status ssh

2. Configure SSH for WSL

Edit the SSH configuration file:

sudo nano /etc/ssh/sshd_config

Make sure these settings are configured:

Port 2222  # Use a different port to avoid conflicts with Windows SSH
PasswordAuthentication yes  # Initially, we'll set up keys later
PubkeyAuthentication yes

Restart SSH:

sudo systemctl restart ssh

3. Set up SSH Keys (Recommended)

On your WSL Ubuntu:

# Create .ssh directory if it doesn't exist
mkdir -p ~/.ssh
chmod 700 ~/.ssh

On Your Macbook

1. Generate SSH Key (if you don't have one)

ssh-keygen -t ed25519 -C "[email protected]"
# Press Enter to accept default location
# Enter a passphrase (recommended) or leave empty

2. Copy your public key to WSL

First, get the IP address of your Windows machine on the local network. You'll need this, not the WSL IP.

# Copy your public key (you'll paste this in WSL)
cat ~/.ssh/id_ed25519.pub

Then SSH into your WSL instance (replace WINDOWS_IP with your Windows machine's IP):

ssh -p 2222 your-wsl-username@WINDOWS_IP

Add your public key to authorized_keys on WSL:

# On WSL, add the public key
echo "YOUR_PUBLIC_KEY_HERE" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

Windows Firewall Configuration

On your Windows machine, you need to allow the SSH port through the firewall:

  1. Open Windows PowerShell as Administrator
  2. Run:
New-NetFirewallRule -Name "WSL2 SSH" -DisplayName "WSL2 SSH" -Direction Inbound -LocalPort 2222 -Protocol TCP -Action Allow

Network Configuration for WSL2

Option 1: Port Forwarding (Recommended)

Create a PowerShell script on Windows to forward ports:

# Save as wsl-port-forward.ps1
$wslIP = wsl hostname -I | ForEach-Object { $_.Trim() }
netsh interface portproxy delete v4tov4 listenport=2222 listenaddress=0.0.0.0
netsh interface portproxy add v4tov4 listenport=2222 listenaddress=0.0.0.0 connectport=2222 connectaddress=$wslIP

Run this script as Administrator whenever you start WSL.

Option 2: Bridge Mode (Alternative)

Edit .wslconfig in your Windows user folder:

[wsl2]
networkingMode=bridged
vmSwitch=WSL

Cursor Remote Development Setup

1. Install Remote Development Extension

In Cursor, install the "Remote - SSH" extension if it's not already installed.

2. Configure SSH Connection

  1. Open Command Palette (Cmd+Shift+P)
  2. Type "Remote-SSH: Add New SSH Host"
  3. Enter: ssh -p 2222 your-wsl-username@WINDOWS_IP
  4. Select the SSH config file to update (usually ~/.ssh/config)

3. Connect to WSL

  1. Open Command Palette (Cmd+Shift+P)
  2. Type "Remote-SSH: Connect to Host"
  3. Select your WSL host
  4. Cursor will open a new window connected to your WSL instance

4. SSH Config File (Optional but Recommended)

Add this to ~/.ssh/config on your Macbook for easier connection:

Host wsl-ubuntu
    HostName WINDOWS_IP
    Port 2222
    User your-wsl-username
    IdentityFile ~/.ssh/id_ed25519

Then you can simply use ssh wsl-ubuntu to connect.

Troubleshooting

Can't connect to SSH

  1. Check Windows IP address:

    • On Windows: ipconfig in Command Prompt
    • Look for IPv4 address on your network adapter
  2. Verify SSH is running in WSL:

    sudo systemctl status ssh
  3. Check Windows Firewall:

    • Ensure port 2222 is allowed
    • Temporarily disable firewall to test
  4. Test connection from WSL to itself:

    ssh -p 2222 localhost

WSL IP changes frequently

Create a scheduled task on Windows to run the port forwarding script at startup.

Performance Tips

  1. Use WSL2 for better performance
  2. Store your projects on the WSL filesystem (not /mnt/c/)
  3. Use .gitignore to exclude large files and node_modules

Alternative: VS Code Server

If you have issues with SSH, you can also use:

  • code-server: Web-based VS Code that runs on WSL
  • GitHub Codespaces: If your project is on GitHub
  • Tailscale: For secure remote access without port forwarding

This setup will give you a seamless development experience, allowing you to work on your WSL projects from your Macbook using Cursor with full IntelliSense, debugging, and terminal access.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment