osnosn

  博客园 :: 首页 :: 博问 :: 闪存 :: :: 联系 :: 订阅 订阅 :: 管理 ::

win10_linux子系统_WSL_WSL2_debian10_frp

转载注明来源: 本文链接 来自osnosn的博客,写于 2020-07-23.

WSL(WSL1), WSL2 的网络

  • WSL
    • WSL就是让win支持ELF可执行文件。直接让linux的程序在win下运行。
      win中的设备(如usb,串口...),WSL中也都能看到。
    • WSL 中的linux,因为直接在win下运行,所见到的网络,和win中见到的一样。
      • iptables之类的不能用,被win的防火墙接管了。
      • pppd,etherwake,nmap,ss,arp 用不了。
      • WSL 中可用的替代指令(其实就是执行win的命令行指令)
        /mnt/c/Windows/System32/ARP.EXE -a ,
        /mnt/c/Windows/System32/ipconfig.exe ,
        /mnt/c/Windows/System32/NETSTAT.EXE -n ,
        /mnt/c/Windows/System32/quser.exe ,
      • ssh,telnet,ping 可用。
    • 在"windows功能" 中,同时勾选 "linux子系统" 和 "hyper-v", 是没问题的,可以都装上,共存。
      • 如有hyper-v,会导致Linux监听某些端口permission denied.
        可以在win下执行 netsh interface ipv4 show excludedportrange protocol=tcp
        或者在WSL中执行 /mnt/c/Windows/System32/netsh.exe interface ipv4 show excludedportrange protocol=tcp
        查看是否因为,hyper-v的保留端口冲突。
  • WSL2
    • WSL2和 hyper-v 一样。是个虚拟机。
      win中的设备,WSL2中看不到。
    • WSL2 使用的是单独分配的 IP,和虚拟机的 nat 模式一样,因为底层就是 hyper-v。
      外网访问Linux要用netsh interface portproxy add ...
    • WSL2中的客户机的IP每次启动都不同,老变。
    • win中访问WSL2的服务,据说只能通过localhost:port,不能用127.0.0.1
    • WSL2中访问win的服务,通过win的实际网卡IP访问。

文件系统

  • WSL
    • 在win中访问Linux文件夹。在资源管理器中输入 \\wsl$ 即可读写。
    • Linux子系统的文件系统一般放在 C:\Users\..你的用户名..\AppData\Local\Packages\...DebianGNULinux_...\LocalState\rootfs
      但不能在win中写入rootfs, 会导致Linux访问错误。要用 \\wsl$ 的方式访问。
    • Linux访问win的文件系统,cd /mnt 就能见到 c/, d/, e/, 几个目录,对于win中的 c: d: e: 盘符。
  • WSL2 (没用过,具体不知道)
    • Linux文件系统放在一个hyper-v映像文件(VHD)中。除了象WSL一样的访问方法之外,还能通过win-linux的网络共享文件的方式访问。

WSL1 的系统初始安装时,需下载的大小 (在 2020-08 记录)

  • debian 10, 77.76MB. 下载有点慢。
    • 下载后第一次启动安装较快(约50sec), 安装完占207MB. 已装154个缺省包。无gcc,sshd.
    • 缺省的 Debian 源比较慢,建议换国内源。
    • 装完sshd-7.9,make-4.2,gcc-8.3, 占620MB.
  • Ubuntu (即 Ubuntu 20.04 LTS), 432.9MB. 下载挺快。
    • 下载后第一次启动安装略慢(约3min), 安装完占999MB. 已装603个缺省包。无gcc,sshd.
    • 缺省的 Ubuntu 源挺快的。
    • 装完sshd-8.2,make-4.2,gcc-9.3, 占1465MB(1.45GB).
  • Ubuntu 18.04 LTS, 258.9MB. 没装,不知道。
  • Kali Linux, 185.5MB.
  • SUSE Linux Enterprise Server 15 SP1, 295.5MB

debian10 apt source.list

# /etc/apt/sources.list
#deb http://deb.debian.org/debian buster main
#deb http://deb.debian.org/debian buster-updates main
#deb http://security.debian.org/debian-security/ buster/updates main
#deb http://ftp.debian.org/debian buster-backports main

deb https://mirrors.huaweicloud.com/debian buster main
deb https://mirrors.huaweicloud.com/debian buster-updates main
deb https://mirrors.huaweicloud.com/debian-security/ buster/updates main
deb https://mirrors.huaweicloud.com/debian buster-backports main
  • 先用 deb http://mirrors......., apt update, apt install ca-certificates 之后,再把所有 http 改为 https
    • ca-certificates 安装耗时4min多,因为win中360的防毒。
  • 可以在所有的 main 后面加上contrib non-free, 变成 deb https://..... main contrib non-free

WSL1 中 的 debian10 少了好多缺省指令

  • 没有 man 指令 apt install man-db, 安装耗时7m30s,因为360防毒。
  • 没有 wget 指令 apt install wget
  • 没有 sshd , apt install openssh-server,安装耗时2m24s
  • 有 cron
  • 没有 telnet , apt install telnet
  • 没有 vim , 但已经装了 vim.tiny 也挺好用。
    • 如果vim/nano编辑中文乱码,设export LC_ALL=en_US.UTF-8,可以放在~/.bashrc/etc/bash.bashrc中。

man,wget,telnet,vim,file 指令,debian10缺省没有装,Ubuntu20.04LTS缺省都已经有了。

  • 装点工具。比如: tmux, p7zip-full, unrar, unzip, vim, tcpdump, tsocks, mutt, nmap, telnet, wget, file, dos2unix, tofrodos, dnsutils, apache2-utils, traceroute, ... 参考【装-debian-的-vm

WSL 中第一次启动网络服务

  • 比如 service ssh start 第一次启动时,win的桌面上,会跳出"Internet防火墙"的警告/设置(是否允许开放端口)。
    • ssh,nginx,3proxy,frps 等需要监听端口的服务。第一次启动,win会弹出防火墙设置。
    • rsyslog,cron,frpc 启动,win不会弹窗。

WSL, linux中的服务

  • win系统重启后,WSL中的服务不会自己启动。
    如果要启动,要写脚本:
rem debian_service_start.vbs , 保存到win系统的"启动"目录. (win+R输入"shell:startup"回车)
rem 启动debian子系统的服务
Set ws = CreateObject("Wscript.Shell")
ws.run "wsl -d debian -u root /etc/rc.start.local", vbhide
  • 上面的 vbs 脚本最后一行用 ws.run "wsl -d debian -u root /etc/rc.start.local", vbhide
    要求 chmod 755 /etc/rc.start.local
  • 或者用 ws.run "wsl -d debian -u root bash /etc/rc.start.local", vbhide
    则可以不执行 chmod。
  • 另外,debian_service_start.vbs 脚本除了放在 shell:startup目录中之外。
    也可以在计划任务中创建,选择触发条件为"启动时执行",选择"无论是否登录都执行"。
    (但不要两个地方都配置,二选一即可)
#!/bin/bash
# file "/etc/rc.start.local"
#为了可以看到ssh,cron的log记录。不启动rsyslog不影响ssh,cron,只是看不到log
/usr/sbin/service rsyslog  start
/usr/sbin/service ssh    start
/usr/sbin/service cron   start
/usr/sbin/service nginx  start

  • WSL 中/etc/init.d/ 中的服务脚本,在 WSL 启动/关闭时,WSL不会主动(用start/stop参数)调用它们。
  • WSL2 , 对比真正的虚拟机,还是少了systemd进程。
    所以,要设置WSL2开机启动的服务,用WSL完全相同做法,写两个脚本。

WSL, uwsgi loop/socket

  • 以下 uwsgi 和 nginx 的两种配置
    • uwsgi socket=/tmp/myuwsgi.sock , nginx uwsgi_pass unix:///tmp/myuwsgi.sock;
      很正常,没有问题。
      升级到 debian 11 后,用socket方式,也会卡住。似乎 uwsgi 输出内容大于某个大小(好像是200k吧),就会卡住。找不到原因。
    • uwsgi socket=127.0.0.1:8888 , nginx uwsgi_pass 127.0.0.1:8888;
      网页访问非常慢,要等很久。nginx error.log中经常出现 upstream timed out (110: Connection timed out) while reading upstream, ... upstream: "uwsgi://127.0.0.1:8888",
      检查发现uwsgi返回页面数据后,tcp连接并没有断开(可能是受到win防火墙的干扰),最终导致超时。
  • WSL2中有没有这种问题,不清楚。
  • WSL1中uwsgi+flask,有时(一两周出现一次)会出现某个进程的一个线程一直占用CPU,似乎进入了死循环。
    reload uwsgi后能恢复。找不到原因。相同的配置,在centos8中,就未发现这个问题。
    • 因为网站访问量比较低。暂时在uwsgi配置中加入harakiri=3600参数。

WSL1 debian10 ,frpc

# frpc.ini
[common]
server_addr = server_ip
server_port = 1234
token = aab---------rr
user = clientName   #为了区别不同的客户端连接,可选项
log_file = /var/log/frpc.log
log_level = info
log_max_days = 3
tls_enable = true
# 启用tls,无需再设use_encryption重复加密

[ssh]
type = tcp
local_port = 22
local_ip = 127.0.0.1
remote_port = 2222
# use_encryption = true
use_compression = true

[my-http-p]
type = tcp
remote_port = 1090
plugin = http_proxy
plugin_http_user = abc
plugin_http_passwd = abc123
use_compression = true

[my-sock5]
type = tcp
remote_port = 1091
plugin = socks5
plugin_user = abc
plugin_passwd = abc123
use_compression = true
  • rc.local : /usr/bin/frpc -c /etc/frpc.ini >/dev/null 2>&1 &
# frps.ini 例子
[common]
#不写 server_addr,默认监听所有网口,ipv4和ipv6,
bind_port = 1234
max_pool_count = 5
token = aab---------rr
log_file = /var/log/frps.log
log_level = info
log_max_days = 3
# tls_only = true
#dashboard_addr =
dashboard_port = 37002
#dashboard_user = user
#dashboard_pwd = pw
#访问http://{host}/metrics
enable_prometheus = true
  • frps 支持 ws:// 不支持 wss://,ws的目录是固定的。frps无需配置,直接支持ws。仅需配置frpc客户端为protocol = websocket, 然后运行一下,错误信息会告诉你目录名。然后就可以配置nginx或caddy的反向代理到frps。
    目前是/~!frp。【issue#2345】,【issues#2024】,【pull#886】,【websocket.go
# nginx 的配置参考
location  /~!frp {
    allow all;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    if ($http_upgrade = "websocket" ){
       proxy_pass http://127.0.0.1:1234;
    }
}

定时检查 frpc,并启动它

#!/bin/bash
## filename: check-frpc.sh
prog=/usr/bin/frpc
conf_name=/home/myuser/frpc-aliyun.ini
selfpath=$(/usr/bin/dirname $(/bin/readlink -f -- $0))    #脚本所在的目录
a=$(/bin/ps ax|/bin/grep "[ ]${conf_name}")   #查找进程列表,frpc是否在执行
if [ -z "$a" ]; then     #如果没找到
        echo $(/bin/date '+%F %T') "run $prog -c $conf_name" >> ${selfpath}/log.start-frpc
        $prog -c $conf_name &        #启动frpc
fi

写入 crontab, 每十分钟检查一次
*/10 * * * * /bin/bash /home/myuser/check-frpc.sh

win10 开机、关机时,执行脚本

在 WSL 中执行关机命令

  • 执行 /mnt/c/Windows/System32/shutdown.exe /s /t 2 可以把机器关了。
  • 执行 /mnt/c/Windows/System32/shutdown.exe /r 机器会重启。但系统关了之后启动不起来。不知道为什么。

其他

  • frp win 客户端
    • frpmgr, 需要安装,好用(win10)。
      本身就支持大部分的配置参数。还支持手工加入额外参数。
      有的要求严格的防毒能扫出来,需要添加例外/信任。

参考:


转载注明来源: 本文链接 https://www.cnblogs.com/osnosn/p/13370207.html 来自osnosn的博客.

posted on 2020-07-24 11:36  osnosn  阅读(2233)  评论(3编辑  收藏  举报