• 公网访问内网中Wsl2服务器(借助frp)


    0 先决条件

    • 公网服务器一台(本文以腾讯云服务器为例)
    • 已安装好的Wsl2子系统(本文为Ubuntu20.04)

    1 Wsl2配置

    编辑 wsl 的 ssh 配置文件:

    vim /etc/ssh/sshd_config
    
    • 1

    新增一行(若之前有需先删除):

    PermitRootLogin yes
    
    • 1

    启动 ssh 服务:

    service sshd start
    
    • 1

    2 宿主机映射

    将如下脚本写入到 wsl2-network.ps1 文件中,并放入 C:\Windows\System32

    $remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
    $found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
     
    if( $found ){
      $remoteport = $matches[0];
    } else{
      echo "The Script Exited, the ip address of WSL 2 cannot be found";
      exit;
    }
     
    #[Ports]
     
    #All the ports you want to forward separated by coma
    $ports=@(22);
     
     
    #[Static ip]
    #You can change the addr to your ip config to listen to a specific address
    $addr='0.0.0.0';
    $ports_a = $ports -join ",";
     
     
    #Remove Firewall Exception Rules
    #iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";
     
    #adding Exception Rules for inbound and outbound Rules
    #iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
    #iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";
     
    for( $i = 0; $i -lt $ports.length; $i++ ){
      $port = $ports[$i];
      iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
      iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    以管理员权限打开 PowerShell,运行该文件:

    cd C:\Windows\System32
    .\wsl2-network.ps1
    
    • 1
    • 2

    查看是否成功完成端口映射:

    netsh interface portproxy show all
    
    • 1

    在这里插入图片描述

    3 配置frp服务器

    frp下载地址(https://github.com/fatedier/frp/releases/tag/v0.45.0),以目前最新的v0.45.0为例:
    在这里插入图片描述
    解压缩并进入文件夹:

    tar -zxvf frp_0.45.0_linux_amd64.tar.gz
    cd frp
    
    • 1
    • 2

    编辑frps.ini文件:

    vim frps.ini
    
    • 1

    写入如下内容:

    [common]
    bind_port = 6666
    
    • 1
    • 2

    端口号可自由选择,此处以6666为例;

    依次执行以下命令:

    cp frps /usr/bin
    mkdir /etc/frp
    cp frps.ini /etc/frp
    
    • 1
    • 2
    • 3

    设置frps自启动:

    vim /etc/systemd/system/frps.service
    
    • 1

    写入如下内容:

    [Unit]
    Description=My Frp Client Service - %i
    After=network.target syslog.target
    Wants=network.target
    
    [Service]
    Type=simple
    Restart=on-failure
    RestartSec=5s
    ExecStart=/bin/bash -c 'frps -c /etc/frp/frps.ini'
    
    [Install]
    WantedBy=multi-user.target                         
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    启动frps

    systemctl start frps
    systemctl enable frps
    
    • 1
    • 2

    公网主机配置完成,同时需要在腾讯云控制台开放6666端口访问权限

    4 宿主机连接frps服务器

    宿主机下载frp,根据自身系统选择对应版本,同3,此处不再赘述

    解压下载好的frp文件,修改其中的frpc.ini,写入如下内容:

    [common]
    server_addr = 公网服务器IP
    server_port = 6666
    
    [ssh]
    type = tcp
    local_ip = 127.0.0.1
    local_port = 22
    remote_port = 8888
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    remote_port可以自由选择,此处以8888为例;
    注意:需要在腾讯云服务器控制台开放8888端口

    在当前文件加打开PowerShell,启动frpc

    .\frpc.exe -c .\frpc.ini
    
    • 1

    在这里插入图片描述

    5 完结

    现在,我们就可以在办公室连接家里的Wsl2服务器了:

    ssh -p 8888 root@公网IP
    
    • 1
  • 相关阅读:
    Intel汇编-内联汇编使用volatile
    es分组查询多个字段
    安装软件显示“为了对电脑进行保护,已阻止此应用”——已解决
    MongoDB 常用命令
    IO进程及相关函数
    Java学习 --- hashCode、toString、finalize方法
    ssm南工二手书交易平台毕业设计源码172334
    JPA概述
    【教3妹学算法-每日1题】竞赛题:矩阵中的局部最大值
    单例模式(面试常考)
  • 原文地址:https://blog.csdn.net/airenKKK/article/details/127660989