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.
- Install OpenSSH Server in WSL:
sudo apt update && sudo apt install openssh-server- Change the port (important): Windows might already use port 22. Use a dedicated port like
2222. Edit: Port 2222PasswordAuthentication yes(or use SSH keys)ListenAddress 0.0.0.0
sudo nano /etc/ssh/sshd_config- Start the service:
sudo service ssh start3. 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 AllowStep 2: Connect from Mac (VS Code)
- Install the Remote - SSH extension.
- Add this to your Mac’s
~/.ssh/config:
Host win-wsl
HostName <windows-lan-ip>
User <wsl-username>
Port 2222- 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-hostPro: 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 WSLauthorized_keys.
- Auto-start SSH in WSL: Add
sudo service ssh startto~/.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 portproxyat 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:
- Microsoft WSL overview: https://learn.microsoft.com/en-us/windows/wsl/about
- VS Code Remote Development docs: https://code.visualstudio.com/docs/remote/ssh
上一篇
Obsidian + AI:打造你的第二大脑知识管理新范式
下一篇
2026 Deep Guide: Mirrored Mode + Auto-Activation for Instant Mac → WSL 2 Remote Dev
- 作者:airouter.me
- 链接:https://airouter.me/en/article/remote-mac-to-wsl2
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。