局域网内,mac通过windows app客户端,远程桌面连接Windows wsl2中的ubuntu

    需求描述:要在同一局域网下的 Mac 上,通过 Mac 版的 Windows App (原 Microsoft Remote Desktop) 客户端,连接到 Windows 电脑中 WSL2 (Windows Subsystem for Linux 2) 运行的 Ubuntu Desktop,

    解决方案思路: 我们需要在 Ubuntu 内安装 RDP 服务(XRDP),并修改其端口(例如改为 3390),然后在 Windows 主机上配置端口转发 (Port Forwarding),将外部局域网对 Windows IP:3390 的访问转发给 WSL2 IP:3390。


    完全配置指南

    第一阶段:在 Windows WSL2 Ubuntu 中配置图形界面和 RDP

    1. 打开 Ubuntu 终端。

    2. 安装轻量级桌面环境 (XFCE)。

    sudo apt update
    sudo apt install xfce4 xfce4-goodies -y
    

    3. 安装 XRDP 服务。

    sudo apt install xrdp -y
    

    4. 配置 XRDP 端口。 将 XRDP 的默认端口 3389 改为 3390,以避免与 Windows 自身的远程桌面冲突。

    # 备份配置
    sudo cp /etc/xrdp/xrdp.ini /etc/xrdp/xrdp.ini.bak
    # 修改端口为 3390
    sudo sed -i 's/3389/3390/g' /etc/xrdp/xrdp.ini
    # 配置 session 以启动 XFCE
    echo "startxfce4" > ~/.xsession
    

    5. 启动 XRDP 服务。

    sudo systemctl enable xrdp
    sudo systemctl start xrdp
    

    第二阶段:在 Windows 主机上配置端口转发

    此阶段必须在 Windows 的 PowerShell (管理员身份) 中执行。

    1. 获取 WSL2 Ubuntu 的内部 IP 地址。

     wsl hostname -I
    # 输出为: 172.24.109.115 (记住这个地址)
    

    注意:WSL IP 地址在每次重新启动后可能会改变。

    2. 配置网络端口转发。 此命令告诉 Windows:将所有监听 0.0.0.0 (即所有网络接口,包括局域网Wi-Fi/以太网) 上 端口 3390 的 TCP 流量,转发到 WSL2 内部 IP 的 3390 端口。

    netsh interface portproxy add v4tov4 listenport=3390 listenaddress=0.0.0.0 connectport=3390 connectaddress=172.24.109.115
    

    3. 配置 Windows 防火墙允许 3390 端口入站。

    New-NetFirewallRule -DisplayName "WSL2_XRDP_PortForward" -Direction Inbound -Protocol TCP -LocalPort 3390 -Action Allow
    

    第三阶段:在 Mac 上连接

    1. 获取 Windows 主机的局域网 IP 地址。 在 Windows PowerShell 中输入:

    ipconfig
    # 显示 192.168.1.6 
    

    2. 打开 Mac 上的 "Windows App" (Microsoft Remote Desktop)。

    3. 新建一个 PC 连接。

    • PC name: 输入 192.168.1.6:3390 。

    4. 启动连接。 在登录界面输入你的 WSL Ubuntu 的用户名和密码(注意:不是 Windows 的登录凭据)。

    alt text

    alt text


    高级:如何解决 WSL IP 经常改变的问题

    每次 Windows 重启后,netsh interface portproxy 规则就会失效,因为 WSL 的 <WSL_IP> 变了。你可以创建一个自动化脚本来一键解决(或配置为开机自动运行):

    在 Windows 上创建一个名为 stop_start_wsl_rdp.ps1 的 PowerShell 脚本:

    # 管理员权限运行
    if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
        Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
        Exit
    }
    
    # 1. 获取 WSL IP
    $wsl_ip = (wslhostname -I).Trim().Split(' ')[0]
    Write-Host "Current WSL IP: $wsl_ip" -ForegroundColor Cyan
    
    # 2. 清除旧规则
    netsh interface portproxy reset
    
    # 3. 添加新端口转发规则 (LAN:3390 -> WSL:3390)
    netsh interface portproxy add v4tov4 listenport=3390 listenaddress=0.0.0.0 connectport=3390 connectaddress=$wsl_ip
    
    # 4. 确保防火墙规则存在 (这步通常只需做一次,但保留以便脚本健壮)
    if (-not (Get-NetFirewallRule -DisplayName "WSL2_XRDP_PortForward" -ErrorAction SilentlyContinue)) {
        New-NetFirewallRule -DisplayName "WSL2_XRDP_PortForward" -Direction Inbound -Protocol TCP -LocalPort 3390 -Action Allow
    }
    
    Write-Host "Port Forwarding updated: Windows:3390 -> WSL:$wsl_ip:3390" -ForegroundColor Green
    Write-Host "Please start XRDP service inside Ubuntu if it's not running." -ForegroundColor Yellow
    Write-Host "Press any key to exit..."
    $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    
    

    每次开机后,只需在 Windows 上管理员身份运行这个脚本即可快速更新网络配置。同时,确保在 Ubuntu 内部运行了 sudo systemctl start xrdp。