win10_linux子系統_WSL_WSL2_debian10_frp


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, ...

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;
      很正常,沒有問題。
    • 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]
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
  • 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;
    }
}

win10 開機、關機時,執行腳本

在 WSL 中執行關機命令

  • 執行 /mnt/c/Windows/System32/shutdown.exe /s /t 2 可以把機器關了。
  • 執行 /mnt/c/Windows/System32/shutdown.exe /r 機器會重啟。但系統關了之后啟動不起來。不知道為什么。

參考:


轉載注明來源: 本文鏈接 來自osnosn的博客.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM