F
章節導航

在WSL2中安裝Ubuntu22.04並配置網路代理

Elon Woo3 分鐘閱讀
核心精華與要旨 (TL;DR)

在這篇文章中,我將分享我在WSL2中安裝Ubuntu22.04並配置網路代理的過程。通過更換APT源和配置區域網代理,我成功地提升了軟體包更新速度,並實現了在區域網內的網路訪問。希望這篇文章能幫助到需要在WSL2中使用Ubuntu的朋友們。

The Astro logo on a dark background with a pink glow.

安裝WSL2並配置Ubuntu22.04

更換APT源

在Ubuntu 系統中,將 APT 源設定為國內映象源可以提高軟體包更新和下載速度,這裡以阿里源為例。

1. 備份現有的源列表:

cd /etc/apt/
ls  // etc/apt/目錄有個名為sources.list的檔案
sudo cp sources.list sources.list.bak   // 備份
ls  // etc/apt/目錄有個名為sources.list.bak的檔案

2. 編輯 sources.list 檔案:

使用以下命令開啟 sources.list 檔案:

sudo vim sources.list

3. 替換內容為阿里源:

刪除檔案原有內容

dG

根據Ubuntu 版本,將內容替換為對應的阿里源。以下是常見版本的源地址:

deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

4. 更新軟體包索引:

// todo: 解釋兩個命令的意義 使用以下命令更新軟體包索引並更新,定期執行該命令。

sudo apt update & sudo apt upgrade

令WSL2能夠使用宿主機的代理

方法一:配置 HTTP(S)/SOCKS 代理

  1. 開啟Clash的LAN訪問。
  2. 在Windows防火牆中放行Clash代理埠:設定=>防火牆=>允許應用通過防火牆=>開啟Clash相關的專用網。
  3. 為WSL2設定代理。

方法二:配置映象代理

這是一種更簡便的網路代理方法。WSL2預設使用NAT網路,通過Hyper-V虛擬交換機連線宿主機,網路是隔離的,IP並不相同。

在啟用 WSL2 的映象模式(Mirrored Mode)後,宿主機和WSL2子系統將:

  • 共享 IP 地址:WSL2 子系統和宿主機將共享相同的 IP 地址(如 192.168.x.x),實現網路直通。
  • 共享網路配置:包括 DNS、路由和代理設定,WSL2 將直接繼承宿主機的網路配置。
  • 代理繼承:通過設定 autoProxy=true,WSL2 可以自動繼承宿主機的代理設定,無需手動配置。
  1. 啟用映象模式

在Windows根目錄編輯.wslconfig檔案,如果沒有就建立該檔案並編輯。

[wsl2]
networkingMode=mirrored
dnsTunneling=true
autoProxy=true

[experimental]
# requires dnsTunneling but are also OPTIONAL
bestEffortDnsParsing=true
  1. 重啟WSL2以使配置生效
wsl --shutdown // 關機後要重啟
  1. 測試網路連通性
 curl -I https://www.google.com

由於宿主機和WSL2子系統的網路棧隔離和埠複用,加之作業系統設計了機制來處理共享IP的情況確保網路通訊的正常進行,啟用映象模式後,二者共享相同的 IP 地址,但這並不會導致衝突。

此時WSL的IP地址、DNS配置和路由表應與宿主機相同。

ip addr show eth0
cat /etc/resolv.conf
ip route

方法三:配置 TUN 模式代理

  1. 下載服務模式,下載成功後Clash會自動重啟。
  2. 配置Clash Core使得盾牌變亮。
  3. 開啟Clash的TUN模式。
  4. 配置UWP Loopback(需要 Clash 以管理員許可權啟動)放行 Windows子系統,在開啟的介面中勾選Ubuntu22.04、 適用於Linux的Windows子系統後點擊Save Change
  5. 重啟電腦後,WSL2內便全域性代理配置成功。

配置區域網代理

使啟動clash系統代理的裝置作為伺服器,為區域網其他裝置提供代理服務。

準備工作(伺服器)

  • 允許Clash通過Defender防火牆。
  • 開啟Clash的允許區域網連線(Allow LAN)和系統代理(System Proxy)。
  • 點選Clash的Profiles,編輯啟用的.yaml檔案,將allow-lan改為true。

設定代理(其它裝置)

  • 其它裝置連線到同一區域網中,進入該WI-FI的配置頁面。
    • 安卓/蘋果手機或平板:WIFI設定;
    • windows:代理伺服器設定;
    • WSL2:/etc/resolv.conf;
  • 將Clash中顯示的伺服器IP和Port輸入進去。
代理(Proxy):手動(Manual),
主機名(HostName):      // 啟動系統代理的裝置的IP地址
埠(Port):7890       // 一般是7890,也可以自定義
  • 測試網路連線。
宸極
宸極 先生

專精於太乙神數與高階術數之學。致力於以條理化與邏輯化的學術視角,為高階人士與企業主提供天時大運與局勢研判。