目录
1. WSL概念
2. WSL安装
2.1 window系统要求
2.2 WSL使用命令
2.2.1 列出可用的 Linux 分发版
2.2.2 将 WSL 版本设置为 1 或 2
2.2.3 安装WSL 和 Linux
2.2.4 列出已安装的 Linux 分发版
2.2.5 设置默认 Linux 分发版
2.2.6 从 PowerShell 或 CMD 运行特定的 Linux 分发版
更新 WSL
检查 WSL 状态
检查 WSL 版本
Help 命令
以特定用户身份运行
2.2.7 更多命令
3. 基于WSL安装docker
3.1 直接window下安装docker-destop
3.2 在WSL中Ubuntu中安装docker
1. WSL概念
适用于 Linux 的 Windows 子系统(WSL)允许开发人员直接在 Windows 上运行 GNU/Linux 环境(包括大多数命令行工具、实用工具和应用程序),无需传统虚拟机或双启动设置的开销。
简而言之:window说,别装虚拟机了,直接用我的WSL吧
2. WSL安装
2.1 window系统要求
Windows 10 版本 2004 及更高版本(内部版本 19041 及更高版本)或 Windows 11 才能使用以下命令。
2.2 WSL使用命令
2.2.1 列出可用的 Linux 分发版
查询有效的Linux 分发版发行版名称列表,查询的是Microsoft Store上的
wsl --list --online
C:\Windows\System32>wsl --list --online
以下是可安装的有效分发的列表。
使用“wsl.exe --install <Distro>”安装。NAME FRIENDLY NAME
AlmaLinux-8 AlmaLinux OS 8
AlmaLinux-9 AlmaLinux OS 9
AlmaLinux-Kitten-10 AlmaLinux OS Kitten 10
AlmaLinux-10 AlmaLinux OS 10
Debian Debian GNU/Linux
FedoraLinux-42 Fedora Linux 42
SUSE-Linux-Enterprise-15-SP5 SUSE Linux Enterprise 15 SP5
SUSE-Linux-Enterprise-15-SP6 SUSE Linux Enterprise 15 SP6
Ubuntu Ubuntu
Ubuntu-24.04 Ubuntu 24.04 LTS
archlinux Arch Linux
kali-linux Kali Linux Rolling
openSUSE-Tumbleweed openSUSE Tumbleweed
openSUSE-Leap-15.6 openSUSE Leap 15.6
Ubuntu-18.04 Ubuntu 18.04 LTS
Ubuntu-20.04 Ubuntu 20.04 LTS
Ubuntu-22.04 Ubuntu 22.04 LTS
OracleLinux_7_9 Oracle Linux 7.9
OracleLinux_8_7 Oracle Linux 8.7
OracleLinux_9_1 Oracle Linux 9.1
选项包括:
--distribution
:指定要安装的 Linux 分发版。 可以通过运行wsl --list --online
来查找可用的分发版。--no-launch
:安装 Linux 分发版,但不自动启动它。--web-download
:从联机源安装,而不是使用 Microsoft Store。
2.2.2 将 WSL 版本设置为 1 或 2
wsl --set-version <distribution name> <versionNumber>
wsl --set-default-version 2
WSL 2 仅在 Windows 11 或 Windows 10 版本 1903 版本 18362 或更高版本中可用。
在 WSL 1 和 WSL 2 之间切换可能非常耗时,并且由于两种体系结构之间的差异而导致失败。 对于包含大型项目的分发版,建议在尝试转换之前备份文件。
2.2.3 安装WSL 和 Linux
- 安装 WSL 和 Linux 的默认 Ubuntu 分发版
wsl --install
- 指定要安装的 Linux 分发版
wsl --install <Distribution Name>
默认国内访问不到
C:\Windows\System32>wsl --install Ubuntu-24.04
无法从“https://raw.githubusercontent.com/microsoft/WSL/master/distributions/DistributionInfo.json”提取列表分发。操作超时
错误代码: Wsl/InstallDistro/0x80072ee2
开启上网工具
C:\Windows\System32>wsl --install Ubuntu-24.04
正在下载: Ubuntu 24.04 LTS
正在安装: Ubuntu 24.04 LTS
sl: wsl2.pageReporting:C:\Users\zhaohao\.wslconfig 中的键“15”未知
wsl: wsl2.pageReporting:C:\Users\zhaohao\.wslconfig 中的键“15”未知已成功安装分发。可以通过 “wsl.exe -d Ubuntu-24.04” 启动它
正在启动 Ubuntu-24.04...===100.0%==========================]
wsl: 检测到 localhost 代理配置,但未镜像到 WSL。NAT 模式下的 WSL 不支持 localhost 代理。
Provisioning the new WSL instance Ubuntu-24.04
This might take a while...
Create a default Unix user account: zhaohao
- 默认创建一个用户
C:\Windows\System32>wsl --install Ubuntu-24.04
正在下载: Ubuntu 24.04 LTS
正在安装: Ubuntu 24.04 LTS
sl: wsl2.pageReporting:C:\Users\zhaohao\.wslconfig 中的键“15”未知
wsl: wsl2.pageReporting:C:\Users\zhaohao\.wslconfig 中的键“15”未知已成功安装分发。可以通过 “wsl.exe -d Ubuntu-24.04” 启动它
正在启动 Ubuntu-24.04...===100.0%==========================]
wsl: 检测到 localhost 代理配置,但未镜像到 WSL。NAT 模式下的 WSL 不支持 localhost 代理。
Provisioning the new WSL instance Ubuntu-24.04
This might take a while...
Create a default Unix user account: zhaohao
New password:
Retype new password:
Sorry, passwords do not match.
passwd: Authentication token manipulation error
passwd: password unchanged
Try again? [y/N] y
New password:
Retype new password:
passwd: password updated successfully
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.zhaohao@huawei-matebook:/mnt/c/Windows/System32$
- 安装成功后
以上安装成功后,进入到Ubuntu的命令行。如果要使用高级命令 sudo su
2.2.4 列出已安装的 Linux 分发版
wsl --list --verbose
C:\Windows\System32>wsl --list --verboseNAME STATE VERSION
* docker-desktop Stopped 2Ubuntu-24.04 Running 2
2.2.5 设置默认 Linux 分发版
wsl --set-default <Distribution Name>C:\Windows\System32>wsl --set-default Ubuntu-24.04
操作成功完成。C:\Windows\System32>wsl --list --verboseNAME STATE VERSION
* Ubuntu-24.04 Running 2docker-desktop Stopped 2
2.2.6 从 PowerShell 或 CMD 运行特定的 Linux 分发版
更新 WSL
PowerShell复制
wsl --update
将 WSL 版本更新为最新版本。 选项包括:
--web-download
:从 GitHub 下载最新更新,而不是 Microsoft 商店。
检查 WSL 状态
PowerShell复制
wsl --status
请参阅有关 WSL 配置的常规信息,例如默认分发类型、默认分发和内核版本。
检查 WSL 版本
PowerShell复制
wsl --version
检查有关 WSL 及其组件的版本信息。
Help 命令
PowerShell复制
wsl --help
请参阅 WSL 可用的选项和命令列表。
以特定用户身份运行
PowerShell复制
wsl --user <Username>
为以指定用户身份运行 WSL,请将 <Username>
替换为 WSL 分发中存在的用户的名称。
C:\Windows\System32>wsl --user zhaohao
zhaohao@huawei-matebook:/mnt/c/Windows/System32$
2.2.7 更多命令
WSL 的基本命令 | Microsoft LearnWindows Subsystem for Linux (WSL) 随附的基本命令参考。https://learn.microsoft.com/zh-cn/windows/wsl/basic-commands#install
3. 基于WSL安装docker
3.1 直接window下安装docker-destop
Docker: Accelerated Container Application DevelopmentDocker is a platform designed to help developers build, share, and run container applications. We handle the tedious setup, so you can focus on the code.https://www.docker.com/
3.2 在WSL中Ubuntu中安装docker
需要魔法上网工具
非常建议安装window下的docker,而不是在Ubuntu中在安装docker
3.2.1 官方文档
Ubuntu | Docker DocsLearn how to install, launch, and upgrade Docker Desktop on Ubuntu. This quick guide will cover prerequisites, installation methods, and more.https://docs.docker.com/desktop/setup/install/linux/ubuntu/3.2.2 安装apt
sudo apt-get update
sudo apt-get install ./docker-desktop-amd64.deb
root@huawei-matebook:/# sudo apt-get update
sudo apt-get install ./docker-desktop-amd64.deb
Hit:1 http://archive.ubuntu.com/ubuntu noble InRelease
Get:2 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]
Get:3 http://archive.ubuntu.com/ubuntu noble-updates InRelease [126 kB]
Get:4 http://archive.ubuntu.com/ubuntu noble-backports InRelease [126 kB]
Get:5 http://security.ubuntu.com/ubuntu noble-security/main amd64 Packages [868 kB]
Get:6 http://archive.ubuntu.com/ubuntu noble/universe amd64 Packages [15.0 MB]
Get:7 http://security.ubuntu.com/ubuntu noble-security/main Translation-en [160 kB]
Get:8 http://security.ubuntu.com/ubuntu noble-security/main amd64 Components [21.5 kB]
Get:9 http://security.ubuntu.com/ubuntu noble-security/main amd64 c-n-f Metadata [7068 B]
Get:10 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Packages [849 kB]
Get:11 http://security.ubuntu.com/ubuntu noble-security/universe Translation-en [187 kB]
Get:12 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Components [52.3 kB]
Get:13 http://security.ubuntu.com/ubuntu noble-security/universe amd64 c-n-f Metadata [17.0 kB]
Get:14 http://security.ubuntu.com/ubuntu noble-security/restricted amd64 Packages [1138 kB]
Get:15 http://archive.ubuntu.com/ubuntu noble/universe Translation-en [5982 kB]
Get:16 http://archive.ubuntu.com/ubuntu noble/universe amd64 Components [3871 kB]
Get:17 http://security.ubuntu.com/ubuntu noble-security/restricted Translation-en [236 kB]
Get:18 http://security.ubuntu.com/ubuntu noble-security/restricted amd64 Components [212 B]
Get:19 http://security.ubuntu.com/ubuntu noble-security/restricted amd64 c-n-f Metadata [468 B]
Get:20 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 Packages [17.7 kB]
Get:21 http://security.ubuntu.com/ubuntu noble-security/multiverse Translation-en [3792 B]
Get:22 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 Components [208 B]
Get:23 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 c-n-f Metadata [380 B]
Get:24 http://archive.ubuntu.com/ubuntu noble/universe amd64 c-n-f Metadata [301 kB]
Get:25 http://archive.ubuntu.com/ubuntu noble/multiverse amd64 Packages [269 kB]
Get:26 http://archive.ubuntu.com/ubuntu noble/multiverse Translation-en [118 kB]
Get:27 http://archive.ubuntu.com/ubuntu noble/multiverse amd64 Components [35.0 kB]
Get:28 http://archive.ubuntu.com/ubuntu noble/multiverse amd64 c-n-f Metadata [8328 B]
Get:29 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 Packages [1118 kB]
Get:30 http://archive.ubuntu.com/ubuntu noble-updates/main Translation-en [238 kB]
Get:31 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 Components [161 kB]
Get:32 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 c-n-f Metadata [13.5 kB]
Get:33 http://archive.ubuntu.com/ubuntu noble-updates/universe amd64 Packages [1078 kB]
Get:34 http://archive.ubuntu.com/ubuntu noble-updates/universe Translation-en [274 kB]
Get:35 http://archive.ubuntu.com/ubuntu noble-updates/universe amd64 Components [376 kB]
Get:36 http://archive.ubuntu.com/ubuntu noble-updates/universe amd64 c-n-f Metadata [26.0 kB]
Get:37 http://archive.ubuntu.com/ubuntu noble-updates/restricted amd64 Packages [1179 kB]
95% [37 Packages 851 kB/1179 kB 72%] 291 kB/s 2s
sudo apt-get update
sudo apt-get install ./docker-desktop-amd64.deb