"WSL" 是 Windows Subsystem for Linux(Windows 的 Linux 子系統(tǒng))的縮寫,是微軟在 Windows 上運行原生 Linux 二進(jìn)制可執(zhí)行文件的兼容層。
主要功能:
允許用戶在 Windows 上直接運行 Linux 命令行工具(如 Bash、apt、grep 等)。
支持多個 Linux 發(fā)行版(如 Ubuntu、Debian、Kali Linux 等)。
適用于開發(fā)、測試和系統(tǒng)管理任務(wù)。
兩種版本:
WSL 2(基于輕量級虛擬機(jī),性能更高,支持完整 Linux 內(nèi)核)
常見用途:
- 運行 Linux 開發(fā)環(huán)境(如 Python、C/C++、Node.js)。
1. WSL 安裝方法
1.1系統(tǒng)要求
- Windows 10(版本 2004 或更高) 或 Windows 11
- 支持 虛擬化技術(shù)(VT-x/AMD-V)(BIOS/UEFI 中需啟用)
1.2安裝步驟
方法 1:一鍵安裝(推薦)
wsl --install
方法 2:手動安裝
1)啟用 WSL 功能(管理員 PowerShell):
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
2)啟用虛擬機(jī)平臺(WSL 2 需要):
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
3)重啟電腦
4)設(shè)置 WSL 2 為默認(rèn)版本:
wsl --set-default-version 2
5)安裝 Linux 發(fā)行版(Microsoft Store 或命令行):
- 從 Microsoft Store 下載(如 Ubuntu、Debian、Kali Linux)
wsl --install -d <發(fā)行版名稱>
# 例如:wsl --install -d Ubuntu-22.04
2. WSL 使用技巧
2.1基本命令
| 命令 | 說明 |
|---|
wsl | |
wsl -d <發(fā)行版> | |
wsl --list --online | |
wsl --list --verbose | 查看已安裝的發(fā)行版及版本(WSL 1/WSL 2) |
wsl --shutdown | |
wsl --terminate <發(fā)行版> | |
wsl --export <發(fā)行版> <文件名.tar> | |
wsl --import <新發(fā)行版> <安裝路徑> <備份文件> | |
2.2文件系統(tǒng)交互
\\wsl$\<發(fā)行版名稱>\(如 \\wsl$\Ubuntu\home\user)
/mnt/c/(對應(yīng) C:\)
/mnt/d/(對應(yīng) D:\)
2.3配置默認(rèn)用戶
<發(fā)行版> config --default-user <用戶名>
例如(Ubuntu):
ubuntu config --default-user root # 默認(rèn)以 root 登錄
2.4升級 WSL 2 內(nèi)核
wsl --update
2.5運行 GUI 應(yīng)用(WSLg)
- 支持 Linux GUI 程序(如 GIMP、VS Code)
gedit
或
firefox、nautilus 等
3. 高級用法
3.1 Docker + WSL 2
1)安裝 Docker Desktop 并啟用 WSL 2 后端
2)在 WSL 中直接使用 docker 命令
3.2 VS Code 遠(yuǎn)程開發(fā)
1)安裝 VS Code 和 Remote - WSL 擴(kuò)展
2)在 WSL 終端輸入:
code .
即可在 Windows 的 VS Code 中編輯 Linux 文件
3.3 Zsh / Oh My Zsh 美化終端
sudo apt install zsh
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
4. 常見問題
4.1 WSL 2 網(wǎng)絡(luò)問題
Windows 訪問 WSL 2 服務(wù)(如 localhost:3000):
hostname -I # 查看 WSL 2 的 IP
WSL 2 訪問 Windows 服務(wù):
cat /etc/resolv.conf # 查看 Windows 的 IP(nameserver)
4.2性能優(yōu)化
關(guān)閉 WSL 2 的虛擬機(jī)(節(jié)省資源):
wsl --shutdown
限制內(nèi)存使用(在 %USERPROFILE%\.wslconfig 中配置):
[wsl2]
memory=4GB # 限制最大內(nèi)存
processors=2 # 限制 CPU 核心數(shù)
5. 總結(jié)
? 適合人群:開發(fā)者、Linux 學(xué)習(xí)者、DevOps
? 優(yōu)勢:輕量級、高性能、與 Windows 無縫集成
? 推薦版本:WSL 2(支持完整 Linux 內(nèi)核)
附:WSL 命令速查表
| 命令 | 描述 |
|---|
wsl --list --online | |
wsl --install | |
wsl --install --distribution <dist_name> | |
wsl --list --verbose | |
wsl -d <dist_name> | |
wsl -d <dist_name> --user <username> | |
wsl ~ -d <dist_name> | |
wsl --set-default <dist_name> | |
wsl --terminate <dist_name> | |
wsl --shutdown | 關(guān)閉 WSL 及相關(guān)系統(tǒng) |
wsl --version | |
wsl --status | |
wsl --set-version <dist_name> <ver> | |
wsl --export <dist_name> <file.tar> | 將發(fā)行版導(dǎo)出為指定名稱的 tar 文件 |
wsl --import <dist_name> <import location> <exported file> | |
wsl --unregister <DistributionName> | |
wsl --mount <diskpath> | |
wsl --update | |
wsl --help | |
注:
<dist_name> 表示發(fā)行版名稱(如 Ubuntu、Debian 等)。<file.tar> 表示導(dǎo)出的 tar 文件名。
閱讀原文:原文鏈接
該文章在 2025/7/21 10:58:58 編輯過