Lazy loaded image
Tech Sharing
[2026] How to Connect from a Mac to WSL 2 on Windows with VS Code (Remote SSH)
字数 517阅读时长 2 分钟
2026-5-1
2026-5-7
type
Post
status
Published
date
May 1, 2026
slug
remote-mac-to-wsl2
summary
A practical guide to connect VS Code on macOS directly into WSL 2 inside a Windows host, using port forwarding or SSH ProxyJump for a fast, native Linux dev experience.
tags
Tools
category
Tech Sharing
icon
password
URL

0. Introduction

In a multi-device workflow, a common pain point is this: your primary machine is a Mac, but the high-performance development environment (deep learning, large builds, etc.) runs inside WSL 2 on a Windows desktop.
You might already be using VS Code Remote-SSH to connect to the Windows host, but once you realize your code actually lives inside WSL, you’ll run into issues like slow file access and mismatched toolchains. This article explains how to “pierce through” Windows and make VS Code on your Mac connect directly to the WSL 2 environment.

1. Why is this harder than it looks?

WSL 2 uses a Hyper-V-based NAT networking model:
  • Windows has a LAN IP (e.g. 192.168.1.100).
  • WSL 2 gets a private virtual IP (e.g. 172.18.x.x) that only Windows can see.
  • Your Mac typically cannot access that virtual IP directly.
To break this barrier, there are two mature approaches: Port Forwarding and SSH ProxyJump.

2. The key: make WSL “listen” for connections

First, WSL needs an SSH server running.
  1. Install OpenSSH Server in WSL:
    1. sudo apt update && sudo apt install openssh-server
  1. Change the port (important): Windows might already use port 22. Use a dedicated port like 2222. Edit:
    1. sudo nano /etc/ssh/sshd_config
      • Port 2222
      • PasswordAuthentication yes (or use SSH keys)
      • ListenAddress 0.0.0.0
  1. Start the service:
    1. sudo service ssh start

3. Solution A: Port forwarding (most common, most stable)

The idea: tell Windows, “If a request hits port 2222, forward it to WSL’s port 2222.”

Step 1: Set up forwarding on Windows

Open PowerShell as Administrator and run:
# Get WSL internal IP $wsl_ip = (wsl hostname -I).Trim() # Forward host 2222 -> WSL 2222 netsh interface portproxy add v4tov4 listenport=2222 listenaddress=0.0.0.0 connectport=2222 connectaddress=$wsl_ip # Allow firewall inbound New-NetFirewallRule -DisplayName "WSL SSH" -Direction Inbound -LocalPort 2222 -Protocol TCP -Action Allow

Step 2: Connect from Mac (VS Code)

  1. Install the Remote - SSH extension.
  1. Add this to your Mac’s ~/.ssh/config:
    1. Host win-wsl HostName <windows-lan-ip> User <wsl-username> Port 2222
  1. Connect to win-wsl. Done.

4. Solution B: SSH ProxyJump (jump host)

If Windows OpenSSH Server is enabled, you can use SSH ProxyJump to hop from Windows into WSL—without maintaining portproxy rules.
Example ~/.ssh/config on Mac:
# Hop 1: Windows host Host win-host HostName 192.168.1.100 User win-user # Hop 2: WSL via Windows Host wsl-dev HostName localhost User wsl-user Port 2222 ProxyJump win-host
Pro: You don’t have to chase the changing WSL IP, because from Windows’ perspective WSL stays “near localhost.”

5. Make it “frictionless”

  • Passwordless login: Add your Mac public key (~/.ssh/id_rsa.pub) to both Windows and WSL authorized_keys.
  • Auto-start SSH in WSL: Add sudo service ssh start to ~/.bashrc (or better, use systemd if enabled).
  • Handle IP changes: WSL’s virtual IP changes after restart. Consider a Windows scheduled task to refresh netsh portproxy at startup.

6. Summary

Connecting VS Code on Mac to WSL 2 is essentially treating WSL as a remote Linux server.
For the best performance and a more native experience, use SSH (as above) rather than file sharing or drive mapping. Once connected, VS Code automatically installs vscode-server in WSL, so you get full Linux IntelliSense/debugging from your Mac.

References:
上一篇
Obsidian + AI:打造你的第二大脑知识管理新范式
下一篇
2026 Deep Guide: Mirrored Mode + Auto-Activation for Instant Mac → WSL 2 Remote Dev