支持内网穿透的工具有很多,如frp,zerotier,cf tunnel等等;这些都是优透的第三方组件,需要准备不少配置。如果只是临时用一下某个特定的服务呢?SSH 就是不错的方案。

  • 场景: A是内网机器windows. B是有公网IP的 vps;让所有能访问B的机器都可以用3389 登录 A。

A机器用密钥免登录B (推荐),如果用账密,这步可省

  • 生成密钥对
    ssh-keygen -t ed25519 -f ~/.ssh/tunnel_key
    image-1747376301349
  • 复制公钥到B
    mkdir -p /home/tunneluser/.ssh
    touch /home/tunneluser/.ssh/authorized_keys
    cat  tunnel_key.pub >> /home/tunneluser/.ssh/authorized_keys
    chown -R tunneluser:tunneluser /home/tunneluser/.ssh
    chmod 700 /home/tunneluser/.ssh
    chmod 600 /home/tunneluser/.ssh/authorized_keys
    
  • 在B机器上 创建专用 tunneluser 用户,并配置SSH tcp转发
    useradd -m -s /usr/sbin/nologin tunneluser
    nano /etc/ssh/sshd_config
    Match User tunneluser
    AllowTcpForwarding yes
    GatewayPorts yes
    PermitOpen any
    ForceCommand echo 'This account is only for port forwarding.'
    
    systemctl restart sshd

远程登录A机器

  • 在A机器使用SSH
    ssh -i tunnel_key -N -R 0.0.0.0:9000:localhost:3389 tunneluser@B公网IP
  • 在任一有mstsc 机器执行 mstsc -v 公网IP:9000
    image-1747377236811
    image-1747377270036