本文记录在 Windows 11 上安装 WSL2 以及调试相关问题的过程与经验。

开启 WSL 功能

打开 Windows 控制面板,依次点击程序 => 启用或关闭 Windows 功能


开启 Hyper-VVirtual Machine Platform适用于 Linux 的 Windows 子系统

然后重启计算机。

安装 WSL

以管理员身份运行 Windows 终端,然后执行 wsl --update

1
2
3
4
5
6
7
PS C:\Users\wgxls> wsl --update
正在下载: 适用于 Linux 的 Windows 子系统 2.6.3
正在安装: 适用于 Linux 的 Windows 子系统 2.6.3
已安装 适用于 Linux 的 Windows 子系统 2.6.3。
操作成功完成。
正在检查更新。
已安装最新版本的适用于 Linux 的 Windows 子系统。

使用 wsl --version 查看 wsl 版本。

1
2
3
4
5
6
7
8
PS C:\Users\wgxls> wsl --version
WSL 版本: 2.6.3.0
内核版本: 6.6.87.2-1
WSLg 版本: 1.0.71
MSRDC 版本: 1.2.6353
Direct3D 版本: 1.611.1-81528511
DXCore 版本: 10.0.26100.1-240331-1435.ge-release
Windows: 10.0.26100.1742

使用 wsl --install 安装 Linux(Ubuntu) 子系统。

输入两次密码,就完成了 Ubuntu 系统的初始化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
PS C:\Users\wgxls> wsl --install
正在下载: Ubuntu
正在安装: Ubuntu
已成功安装分发。可以通过 “wsl.exe -d Ubuntu” 启动它
正在启动 Ubuntu...
wsl: 检测到 localhost 代理配置,但未镜像到 WSL。NAT 模式下的 WSL 不支持 localhost 代理。
Provisioning the new WSL instance Ubuntu
This might take a while...
Create a default Unix user account: wgxls
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.

我的电脑中默认安装的是 Ubuntu 24.04.3 LTS

1
2
3
4
5
6
wgxls@DESKTOP-DMRH21F:/mnt/c/Users/wgxls$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04.3 LTS
Release: 24.04
Codename: noble

WSL 配置

WSL 存储位置迁移

WSL 的默认存储位置在 C 盘,随着 WSL 的使用,其磁盘会不断增大,占用大量 C 盘空间。可以通过以下步骤将 WSL 存储位置迁移到其他盘符。

以管理员身份运行 Windows 终端,执行以下命令查看已经安装的 WSL 发行版:

1
2
3
PS C:\Users\wgxls> wsl -l -v
NAME STATE VERSION
* Ubuntu Running 2

我这里目前只安装了一个 Ubuntu 发行版,并且正在运行。

首先关闭正在运行的 WSL 发行版:

1
2
3
4
PS C:\Users\wgxls> wsl --shutdown
PS C:\Users\wgxls> wsl -l -v
NAME STATE VERSION
* Ubuntu Stopped 2

以 vhd 格式导出 WSL 发行版,我这里指定导出到 D 盘的 D:\WSL\Ubuntu.vhdx 文件中:

1
2
3
4
PS C:\Users\wgxls> wsl --export Ubuntu D:\WSL\Ubuntu.vhdx --vhd
正在导出,这可能需要几分钟时间。 (1408 MB)

操作成功完成。

删除当前的 WSL 发行版:

1
2
3
4
5
6
7
8
9
10
PS C:\Users\wgxls> wsl --unregister Ubuntu
正在注销。
操作成功完成。

PS C:\Users\wgxls> wsl -l -v
适用于 Linux 的 Windows 子系统没有已安装的分发。
可通过安装包含以下说明的分发来解决此问题:

使用“wsl.exe --list --online' ”列出可用的分发
和 “wsl.exe --install <Distro>” 进行安装。

导入之前导出的 WSL 发行版到新的位置:

1
2
3
4
5
PS C:\Users\wgxls> wsl --import Ubuntu D:\WSL\Ubuntu\ D:\WSL\Ubuntu.vhdx --vhd
操作成功完成。
PS C:\Users\wgxls> wsl -l -v
NAME STATE VERSION
* Ubuntu Stopped 2

可以看到 WSL 发行版已经成功导入到新的位置,这里使用 ls 命令查看 D:\WSL\Ubuntu\ 目录:

1
2
3
4
5
6
7
8
9
PS C:\Users\wgxls> ls D:\WSL\Ubuntu\


目录: D:\WSL\Ubuntu


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2026/1/5 20:59 1801453568 ext4.vhdx

刚刚的 Ubuntu.vhdx 文件就可以删除了,释放空间。

可以使用 wsl --set-default Ubuntu 命令将新导入的 Ubuntu 发行版设置为默认发行版:

1
2
PS C:\Users\wgxls> wsl --set-default Ubuntu
操作成功完成。

WSL Settings

可以通过在 Windows 用户目录下创建 .wslconfig 文件来配置 WSL 的一些参数,例如内存限制、处理器数量等。

也可以通过 WSL Settings GUI 工具进行配置。

  1. 内存和 CPU 限制

我习惯将 WSL 的内存和处理器数量设置为和宿主机相同,以充分利用系统资源。毕竟 WSL 才是主要的工作环境。

使用如下命令获取以 MB 为单位的总内存大小:

1
2
3
4
5
PS C:\Users\wgxls> Get-CimInstance Win32_ComputerSystem | Select-Object @{Name="TotalPhysicalMemoryMB";Expression={"{0:N0}" -f ($_.TotalPhysicalMemory/1MB)}}

TotalPhysicalMemoryMB
---------------------
32,419

将获取到的内存大小填写到 WSL Settings 中:

  1. 文件系统

在文件系统选项中,可以设置 WSL 虚拟硬盘的最大大小,默认的设置是 1TB,根据需要可以调整,我这里保持默认。

  1. 网络

在网络选项中,我习惯使用镜像网络模式,并开启主机地址回环,这样可以更方便地在 Windows 和 WSL 之间进行网络通信。

  1. 可选功能 和 开发人员选项 我都保持了默认设置。

最终在 .wslconfig 文件中会生成如下内容:

1
2
3
4
5
[wsl2]
memory=33993785344
networkingMode=Mirrored
[experimental]
hostAddressLoopback=true

WSLg 设置

WSLg 是 WSL 的图形化支持组件,可以让我们在 WSL 中运行 Linux GUI 应用程序。

这里参考官方文档进行配置:

https://learn.microsoft.com/zh-cn/windows/wsl/tutorials/gui-apps

若要运行 Linux GUI 应用,应首先安装与以下系统匹配的驱动程序。 这样,便可以使用虚拟 GPU (vGPU),以便从硬件加速的 OpenGL 渲染中获益。

英特尔 GPU 驱动程序
AMD GPU 驱动程序
NVIDIA GPU 驱动程序

  1. 更新 Ubuntu 系统

在 Linux 系统中执行:

1
sudo apt update && sudo apt upgrade -y
  1. 安装 gedit 文本编辑器
1
sudo apt install gedit -y

安装完成之后,在 Windows 的开始菜单中可以看到 Linux 系统下的应用程序,并且可以直接点击运行。

也可以在 Linux 系统中使用终端命令开启 GUI 程序。

  1. 安装 Google Chrome for Linux

将目录更改为临时文件夹:

1
cd /tmp

使用 wget 下载它:

1
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
1
sudo apt install -f ./google-chrome-stable_current_amd64.deb

此选项 -f 用于修复安装过程中可能出现的损坏依赖项。 该 ./ 命令指定.deb文件所在的当前目录。 如果.deb文件位于其他目录中,则需要在命令中指定该文件的路径。

更多应用我就不再继续装了,毕竟 WSLg 并不是主要的使用场景。

Ubuntu 系统配置

Ubuntu 设置中文字体

让 WSL 直接使用 Windows 字体,WSL 会自动挂在 Windows 的字体目录 /mnt/c/Windows/Fonts,我们只需要指定字体即可。

1
sudo gedit /etc/fonts/local.conf

在打开的文件中添加如下内容:

1
2
3
4
5
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/mnt/c/Windows/Fonts</dir>
</fontconfig>

保存后,刷新字体缓存。

1
sudo fc-cache -fv

使用 fc-list 命令查看字体列表,确认 Windows 字体已经被识别。

1
2
3
4
5
6
7
8
9
10
$ fc-list :lang=zh | grep "Microsoft YaHei"
/mnt/c/Windows/Fonts/msyhbd.ttc: Microsoft YaHei,微软雅黑:style=Bold,Negreta,tučné,fed,Fett,Έντονα,Negrita,Lihavoitu,Gras,Félkövér,Grassetto,Vet,Halvfet,Pogrubiony,Negrito,Полужирный,Fet,Kalın,Krepko,Lodia
/mnt/c/Windows/Fonts/msyh.ttc: Microsoft YaHei UI:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/mnt/c/Windows/Fonts/msyh.ttc: Microsoft YaHei,微软雅黑:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/mnt/c/Windows/Fonts/msyhbd.ttc: Microsoft YaHei UI:style=Bold,Negreta,tučné,fed,Fett,Έντονα,Negrita,Lihavoitu,Gras,Félkövér,Grassetto,Vet,Halvfet,Pogrubiony,Negrito,Полужирный,Fet,Kalın,Krepko,Lodia
/mnt/c/Windows/Fonts/msyhl.ttc: Microsoft YaHei UI,Microsoft YaHei UI Light:style=Light,Regular
/mnt/c/Windows/Fonts/msyhl.ttc: Microsoft YaHei,微软雅黑,Microsoft YaHei Light,微软雅黑 Light:style=Light,Regular

$ fc-list :lang=zh | grep "SimHei"
/mnt/c/Windows/Fonts/simhei.ttf: SimHei,黑体:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta

安装 1Panel 面板

https://1panel.cn/

1
sudo bash -c "$(curl -sSL https://resource.fit2cloud.com/1panel/package/v2/quick_start.sh)"

安裝成功提示如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[1Panel 2026-01-05 22:35:44 install Log]: 1Panel 服务已成功启动,正在继续执行后续配置,请稍候...
[1Panel 2026-01-05 22:35:48 install Log]:
[1Panel 2026-01-05 22:35:48 install Log]: =================感谢您的耐心等待,安装已完成==================
[1Panel 2026-01-05 22:35:48 install Log]:
[1Panel 2026-01-05 22:35:48 install Log]: 请使用您的浏览器访问面板:
[1Panel 2026-01-05 22:35:48 install Log]: 外部地址: http://60.21.105.84:9999/0000
[1Panel 2026-01-05 22:35:48 install Log]: 内部地址: http://192.168.0.110:9999/0000
[1Panel 2026-01-05 22:35:48 install Log]: 面板用户: wgxls
[1Panel 2026-01-05 22:35:48 install Log]: 面板密码: Ww0121050272.
[1Panel 2026-01-05 22:35:48 install Log]:
[1Panel 2026-01-05 22:35:48 install Log]: 官方网站: https://1panel.cn
[1Panel 2026-01-05 22:35:48 install Log]: 项目文档: https://1panel.cn/docs
[1Panel 2026-01-05 22:35:48 install Log]: 代码仓库: https://github.com/1Panel-dev/1Panel
[1Panel 2026-01-05 22:35:48 install Log]: 前往 1Panel 官方论坛获取帮助: https://bbs.fit2cloud.com/c/1p/7
[1Panel 2026-01-05 22:35:48 install Log]:
[1Panel 2026-01-05 22:35:48 install Log]: 如果您使用的是云服务器,请在安全组中打开端口 9999
[1Panel 2026-01-05 22:35:48 install Log]:
[1Panel 2026-01-05 22:35:48 install Log]: 为了您的服务器安全,离开此屏幕后您将无法再次看到您的密码,请记住您的密码。
[1Panel 2026-01-05 22:35:48 install Log]:
[1Panel 2026-01-05 22:35:48 install Log]: ================================================================

这里我设置了端口 9999 进行访问。

安装完成后,使用 Windows 浏览器访问 http://localhost:9999/0000 或者 http://127.0.0.1:9999/0000 能够成功访问,表示我们在 WSL Settings 中配置的镜像网络模式和主机地址回环是正确的。

安装 miniconda

https://www.anaconda.com/docs/getting-started/miniconda/install

1
2
3
4
sudo mkdir /opt/miniconda3
cd /tmp
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
sudo bash Miniconda3-latest-Linux-x86_64.sh -b -u -p /opt/miniconda3

激活 conda 环境。

1
source /opt/miniconda3/bin/activate

初始化 conda 环境对于所有 shell 环境。

1
conda init --all

执行完以上命令之后,重新计入终端会默认进入到 conda 的 base 环境。

如果想要取消 conda 的自动激活,可以执行以下命令:

1
conda config --set auto_activate false

开启 SSH 服务

安装 OpenSSH 服务器:

1
sudo apt install openssh-server -y

开启 SSH 服务:

1
sudo systemctl start ssh

设置开机自启动:

1
sudo systemctl enable ssh

查看 SSH 服务状态:

1
sudo systemctl status ssh

输出内容显示 Active: active (running) 表示 SSH 服务已经成功启动。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
wgxls@DESKTOP-DMRH21F:~$ sudo systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/usr/lib/systemd/system/ssh.service; enabled; preset: enabled)
Active: active (running) since Tue 2026-01-06 20:48:22 CST; 48s ago
TriggeredBy: ● ssh.socket
Docs: man:sshd(8)
man:sshd_config(5)
Process: 2314 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Main PID: 2316 (sshd)
Tasks: 1 (limit: 38088)
Memory: 1.3M (peak: 1.6M)
CPU: 11ms
CGroup: /system.slice/ssh.service
└─2316 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"

Jan 06 20:48:22 DESKTOP-DMRH21F systemd[1]: Starting ssh.service - OpenBSD Secure Shell server...
Jan 06 20:48:22 DESKTOP-DMRH21F sshd[2316]: Server listening on 0.0.0.0 port 22.
Jan 06 20:48:22 DESKTOP-DMRH21F sshd[2316]: Server listening on :: port 22.
Jan 06 20:48:22 DESKTOP-DMRH21F systemd[1]: Started ssh.service - OpenBSD Secure Shell server.

如果你无法从外部设备通过 SSH 连接到 WSL,可能需要配置 Windows 防火墙允许端口 22 的入站连接。

打开 Wondows 安全中心,依次点击防火墙和网络保护 => 高级设置

点击 入站规则 => 新建规则…,选择端口,点击下一步

选择TCP,并指定本地端口为22,点击下一步

我这里选择所有本地端口,表示允许所有端口通过防火墙。

选择允许连接,点击下一步

选择专用公用,点击下一步

为规则命名,例如 IN_TCP_ALL,点击完成

可以通过重复的步骤为 UDP 协议创建入站规则或者出站规则。

SSH 允许 root 用户登录

我主要在局域网内使用 SSH 登录 WSL,为了方便,我通过 1Panel 面板设置了允许 root 用户通过 SSH 登录。

为 root 用户设置密码:

1
sudo passwd root
1
2
3
4
5
wgxls@DESKTOP-DMRH21F:~$ sudo passwd root
[sudo] password for wgxls: <-- 输入当前用户密码
New password: <-- 输入 root 用户新密码
Retype new password: <-- 再次输入 root 用户新密码
passwd: password updated successfully

允许普通用户免输 sudo 密码

编辑 sudoers 文件:

1
sudo visudo

在文件末尾添加如下内容:

1
wgxls ALL=(ALL) NOPASSWD: ALL

保存并退出。现在,用户 wgxls 在使用 sudo 命令时将不再需要输入密码。

修改 SSH 会话 PATH 环境变量

在 WSL 中,默认情况下,通过 SSH 登录时,PATH 环境变量可能与直接在 WSL 终端中登录时不同。这可能导致某些命令无法找到。

比如,在 WSL 终端中可以执行 nvidia-smi 命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
wgxls@DESKTOP-DMRH21F:~$ nvidia-smi
Tue Jan 6 22:06:31 2026
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 590.48.01 Driver Version: 591.59 CUDA Version: 13.1 |
+-----------------------------------------+------------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 NVIDIA GeForce RTX 5070 Ti On | 00000000:01:00.0 On | N/A |
| 30% 39C P0 31W / 300W | 1867MiB / 16303MiB | 0% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| No running processes found |
+-----------------------------------------------------------------------------------------+
wgxls@DESKTOP-DMRH21F:~$ echo $PATH
/opt/miniconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Users/wgxls/AppData/Local/Microsoft/WindowsApps:/mnt/d/Microsoft VS Code/bin:/snap/bin

但是通过 SSH 登录时,执行 nvidia-smi 命令会提示找不到命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
wgxls@DESKTOP-DMRH21F:~$ echo $PATH
/opt/miniconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

wgxls@DESKTOP-DMRH21F:~$ nvidia-smi
Command 'nvidia-smi' not found, but can be installed with:
sudo apt install nvidia-utils-470 # version 470.256.02-0ubuntu0.24.04.1, or
sudo apt install nvidia-utils-470-server # version 470.256.02-0ubuntu0.24.04.1
sudo apt install nvidia-utils-535 # version 535.274.02-0ubuntu0.24.04.2
sudo apt install nvidia-utils-535-server # version 535.274.02-0ubuntu0.24.04.2
sudo apt install nvidia-utils-570 # version 570.195.03-0ubuntu0.24.04.1
sudo apt install nvidia-utils-525 # version 525.147.05-0ubuntu1
sudo apt install nvidia-utils-525-server # version 525.147.05-0ubuntu1
sudo apt install nvidia-utils-565-server # version 565.57.01-0ubuntu0.24.04.3
sudo apt install nvidia-utils-570-server # version 570.195.03-0ubuntu0.24.04.3
sudo apt install nvidia-utils-580 # version 580.95.05-0ubuntu0.24.04.3
sudo apt install nvidia-utils-580-server # version 580.95.05-0ubuntu0.24.04.3
sudo apt install nvidia-utils-550-server # version 550.163.01-0ubuntu0.24.04.1

wgxls@DESKTOP-DMRH21F:~$ /usr/lib/wsl/lib/nvidia-smi
Tue Jan 6 22:09:43 2026
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 590.48.01 Driver Version: 591.59 CUDA Version: 13.1 |
+-----------------------------------------+------------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 NVIDIA GeForce RTX 5070 Ti On | 00000000:01:00.0 On | N/A |
| 0% 42C P0 29W / 300W | 1787MiB / 16303MiB | 0% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| No running processes found |
+-----------------------------------------------------------------------------------------+

可以看到,通过 SSH 登录时,PATH 环境变量比 WSL 终端中少了很多路径,并提示找不到 nvidia-smi 命令。

而实际上 nvidia-smi 命令位于 /usr/lib/wsl/lib/ 目录下。

为了解决这个问题,我们可以配置文件 /etc/environment,将缺失的路径添加进去。

这个修改会影响所有通过 SSH 登录的用户。

1
sudo vim /etc/environment

这里我们不过多添加其他路径,把所有 /mnt 开头的路径都删除掉,只保留 /usr/lib/wsl/lib 路径。

修改前:

1
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"

修改后:

1
PATH="/opt/miniconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/snap/bin"

重新 SSH 登录, nvidia-smi 命令就可以正常使用了。

修改 systemd 环境变量

现在 systemd 启动的服务(例如 1Panel 面板服务)也无法识别 /usr/lib/wsl/lib 路径下的命令。

为了让 systemd 服务也能识别该路径下的命令,我们需要修改 systemd 的环境变量配置文件。

1
sudo vim /etc/systemd/system.conf

在文件中找到 #DefaultEnvironment= 行,去掉注释并添加我们需要的路径。

修改前:

1
#DefaultEnvironment=

修改后:

1
DefaultEnvironment=PATH="/opt/miniconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/snap/bin"

保存并退出。

重新启动 systemd:

1
sudo systemctl daemon-reexec

可以使用 systemctl show-environment 命令查看 systemd 的环境变量,确认修改已经生效。

1
2
3
wgxls@DESKTOP-DMRH21F:~$ systemctl show-environment
LANG=C.UTF-8
PATH=/opt/miniconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/snap/bin

重新启动 1Panel 服务:

1
sudo 1pctl restart

可以看到 1Panel 服务已经成功启动,并且可以正常使用 nvidia-smi 命令了。

WSL 挂在 ext4 磁盘

Windows 系统是无法直接访问 ext4 文件系统的,如果磁盘格式已经是 ext4,可以通过 WSL 来访问。

因为我的机械硬盘之前用在了 Linux 系统中,格式化为了 ext4 文件系统,现在想在 WSL 中挂载使用。

此时我需要参考的是官方文档中关于挂载已分区磁盘的内容:

https://learn.microsoft.com/zh-cn/windows/wsl/wsl2-mount-disk#mounting-a-partitioned-disk

首先以管理员身份运行 Windows 终端,使用 Get-CimInstance -Query "SELECT * from Win32_DiskDrive" 列出所有磁盘。

我这里有一个 4TB 的机械硬盘,ID 是 \\.\PHYSICALDRIVE0

1
2
3
4
5
6
PS C:\Users\wgxls> Get-CimInstance -Query "SELECT * from Win32_DiskDrive"

DeviceID Caption Partitions Size Model
-------- ------- ---------- ---- -----
\\.\PHYSICALDRIVE0 WDC WD43PURZ-74BWPY0 1 4000784417280 WDC WD43PURZ-74BWPY0
\\.\PHYSICALDRIVE1 Fanxiang S790 2TB 3 2000396321280 Fanxiang S790 2TB

执行 wsl.exe --mount <Disk> --bare 命令挂载该磁盘:

1
wsl --mount \\.\PHYSICALDRIVE0 --bare

然后进入 WSL 系统,使用 lsblk 命令查看挂载的磁盘:

可以看到我这里的 4TB 硬盘已经成功挂载为 sde 磁盘,并且有一个分区 sde1

1
2
3
4
5
6
7
8
9
10
wgxls@DESKTOP-DMRH21F:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 388.6M 1 disk
sdb 8:16 0 186M 1 disk
sdc 8:32 0 8G 0 disk [SWAP]
sdd 8:48 0 1T 0 disk /var/lib/docker
/mnt/wslg/distro
/
sde 8:64 0 3.6T 0 disk
└─sde1 8:65 0 3.6T 0 part

下面通过修改 /etc/fstab 文件实现开机自动挂载这块磁盘。

获取该分区的 UUID:

1
2
wgxls@DESKTOP-DMRH21F:~$ sudo blkid /dev/sde1
/dev/sde1: UUID="dceaaac0-3867-4b2c-86e2-2ddc4af28807" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="5a7936cd-5b8e-463d-a155-e6c15317f1c1"

编辑 /etc/fstab 文件:

1
sudo vim /etc/fstab

在文件末尾添加如下内容:

1
UUID=dceaaac0-3867-4b2c-86e2-2ddc4af28807 /mnt/4tb_disk ext4 defaults,nofail 0 2

保存并退出。

创建挂载点目录:

1
sudo mkdir /mnt/4tb_disk

重新启动 WSL:

1
wsl --shutdown

重新进入 WSL 系统,使用 df -h 命令查看挂载情况:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
wgxls@DESKTOP-DMRH21F:~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sdd 1007G 5.2G 951G 1% /
rootfs 16G 2.7M 16G 1% /init
none 16G 0 16G 0% /dev/shm
none 16G 520K 16G 1% /run
none 16G 0 16G 0% /run/lock
/dev/sde1 3.6T 1.4T 2.1T 40% /mnt/4tb_disk
drivers 301G 50G 251G 17% /usr/lib/wsl/drivers
none 16G 0 16G 0% /usr/lib/wsl/lib
none 16G 4.0K 16G 1% /mnt/wsl
none 16G 84K 16G 1% /mnt/wslg
none 16G 0 16G 0% /usr/lib/modules/6.6.87.2-microsoft-standard-WSL2
C:\ 301G 50G 251G 17% /mnt/c
D:\ 1.6T 11G 1.6T 1% /mnt/d

可以看到 4TB 硬盘已经成功挂载到 /mnt/4tb_disk 目录下。

WSL 开机自启并保持运行

默认情况下,WSL 发行版在不使用时会自动关闭,以节省系统资源。但是,有时我们希望 WSL 在系统启动时自动启动,并保持运行状态,尤其是当我们在 WSL 中运行服务器或后台服务时。

可以通过创建一个计划任务来实现这一点。

  1. 打开任务计划程序

Win + R,输入 taskschd.msc 并按回车,打开任务计划程序。

  1. 点击 “创建任务…”

  1. 在 “常规” 选项卡中,填写任务名称,例如 “wsl-mount” ,并选择 “使用最高权限运行”。

  1. 在 “触发器” 选项卡中,点击 “新建…”,选择 “启动时” 作为触发器,选择 “延迟任务” 选项,设置延迟时间为 30 秒,点击 “确定”。

  1. 在 “操作” 选项卡中,点击 “新建…”,选择 “启动程序” 作为操作,程序或脚本填写 C:\Windows\System32\wsl.exe,添加参数填写 --mount \\.\PHYSICALDRIVE0 --bare,起始于填写 C:\Windows\System32 或者留空,点击 “确定”。

第一个操作是挂载硬盘到 WSL。

再添加一个 shell 会话来保持 WSL 运行。

  1. 在 “条件” 选项卡中,保持默认设置即可。如果你是笔记本电脑用户,可以取消 “仅在使用交流电源时启动此任务” 选项。

  2. 在 “设置” 选项卡中,取消勾选 如果任务超时则停止任务 选项,保持其他默认设置。

最后点击确定保存任务。

重启电脑后,可以看到 WSL 发行版已经自动启动并且会保持运行状态。

⚠️

在我这样设置之后,WSLg 似乎无法正常工作,图形界面应用无法启动。如果你需要使用 WSLg,需要先执行 wsl --shutdown 命令关闭 WSL,然后再重新打开 WSL 发行版。

一旦执行了 wsl --shutdown 命令,计划任务启动的 WSL 会话将会被关闭。WSL 无法保持持续运行状态。

This article documents the process and experience of installing WSL2 on Windows 11 and debugging related issues.

Enabling the WSL Feature

Open the Windows Control Panel, then click Programs => Turn Windows features on or off


Enable Hyper-V, Virtual Machine Platform, and Windows Subsystem for Linux.

Then restart the computer.

Installing WSL

Run Windows Terminal as administrator, then execute wsl --update

1
2
3
4
5
6
7
PS C:\Users\wgxls> wsl --update
正在下载: 适用于 Linux 的 Windows 子系统 2.6.3
正在安装: 适用于 Linux 的 Windows 子系统 2.6.3
已安装 适用于 Linux 的 Windows 子系统 2.6.3。
操作成功完成。
正在检查更新。
已安装最新版本的适用于 Linux 的 Windows 子系统。

Use wsl --version to check the WSL version.

1
2
3
4
5
6
7
8
PS C:\Users\wgxls> wsl --version
WSL 版本: 2.6.3.0
内核版本: 6.6.87.2-1
WSLg 版本: 1.0.71
MSRDC 版本: 1.2.6353
Direct3D 版本: 1.611.1-81528511
DXCore 版本: 10.0.26100.1-240331-1435.ge-release
Windows: 10.0.26100.1742

Use wsl --install to install the Linux (Ubuntu) subsystem.

Enter the password twice, and the Ubuntu system initialization is complete.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
PS C:\Users\wgxls> wsl --install
正在下载: Ubuntu
正在安装: Ubuntu
已成功安装分发。可以通过 “wsl.exe -d Ubuntu” 启动它
正在启动 Ubuntu...
wsl: 检测到 localhost 代理配置,但未镜像到 WSL。NAT 模式下的 WSL 不支持 localhost 代理。
Provisioning the new WSL instance Ubuntu
This might take a while...
Create a default Unix user account: wgxls
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.

The default version installed on my computer is Ubuntu 24.04.3 LTS

1
2
3
4
5
6
wgxls@DESKTOP-DMRH21F:/mnt/c/Users/wgxls$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04.3 LTS
Release: 24.04
Codename: noble

WSL Configuration

Migrating the WSL Storage Location

WSL’s default storage location is on the C: drive. As you use WSL, its virtual disk keeps growing and takes up a lot of C: drive space. You can migrate the WSL storage location to another drive with the following steps.

Run Windows Terminal as administrator and execute the following command to view the installed WSL distributions:

1
2
3
PS C:\Users\wgxls> wsl -l -v
NAME STATE VERSION
* Ubuntu Running 2

I currently have only one Ubuntu distribution installed here, and it’s running.

First, shut down the running WSL distribution:

1
2
3
4
PS C:\Users\wgxls> wsl --shutdown
PS C:\Users\wgxls> wsl -l -v
NAME STATE VERSION
* Ubuntu Stopped 2

Export the WSL distribution in vhd format; here I specify exporting to the D:\WSL\Ubuntu.vhdx file on the D: drive:

1
2
3
4
PS C:\Users\wgxls> wsl --export Ubuntu D:\WSL\Ubuntu.vhdx --vhd
正在导出,这可能需要几分钟时间。 (1408 MB)

操作成功完成。

Unregister the current WSL distribution:

1
2
3
4
5
6
7
8
9
10
PS C:\Users\wgxls> wsl --unregister Ubuntu
正在注销。
操作成功完成。

PS C:\Users\wgxls> wsl -l -v
适用于 Linux 的 Windows 子系统没有已安装的分发。
可通过安装包含以下说明的分发来解决此问题:

使用“wsl.exe --list --online' ”列出可用的分发
和 “wsl.exe --install <Distro>” 进行安装。

Import the previously exported WSL distribution to the new location:

1
2
3
4
5
PS C:\Users\wgxls> wsl --import Ubuntu D:\WSL\Ubuntu\ D:\WSL\Ubuntu.vhdx --vhd
操作成功完成。
PS C:\Users\wgxls> wsl -l -v
NAME STATE VERSION
* Ubuntu Stopped 2

You can see the WSL distribution has been successfully imported to the new location. Here I use the ls command to view the D:\WSL\Ubuntu\ directory:

1
2
3
4
5
6
7
8
9
PS C:\Users\wgxls> ls D:\WSL\Ubuntu\


目录: D:\WSL\Ubuntu


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2026/1/5 20:59 1801453568 ext4.vhdx

The Ubuntu.vhdx file from earlier can now be deleted to free up space.

You can use the wsl --set-default Ubuntu command to set the newly imported Ubuntu distribution as the default:

1
2
PS C:\Users\wgxls> wsl --set-default Ubuntu
操作成功完成。

WSL Settings

You can configure some WSL parameters, such as memory limits and processor count, by creating a .wslconfig file in the Windows user directory.

You can also configure them through the WSL Settings GUI tool.

  1. Memory and CPU limits

I prefer to set WSL’s memory and processor count to the same values as the host machine to make full use of system resources. After all, WSL is my main working environment.

Use the following command to get the total memory size in MB:

1
2
3
4
5
PS C:\Users\wgxls> Get-CimInstance Win32_ComputerSystem | Select-Object @{Name="TotalPhysicalMemoryMB";Expression={"{0:N0}" -f ($_.TotalPhysicalMemory/1MB)}}

TotalPhysicalMemoryMB
---------------------
32,419

Fill the obtained memory size into WSL Settings:

  1. File system

In the file system options, you can set the maximum size of the WSL virtual hard disk. The default setting is 1TB, and you can adjust it as needed — I keep the default here.

  1. Network

In the network options, I prefer to use Mirrored networking mode and enable Host address loopback, which makes network communication between Windows and WSL more convenient.

  1. For Optional features and Developer options, I kept the default settings.

In the end, the following content is generated in the .wslconfig file:

1
2
3
4
5
[wsl2]
memory=33993785344
networkingMode=Mirrored
[experimental]
hostAddressLoopback=true

WSLg Settings

WSLg is WSL’s graphical support component that lets us run Linux GUI applications in WSL.

Here I follow the official documentation for configuration:

https://learn.microsoft.com/zh-cn/windows/wsl/tutorials/gui-apps

To run Linux GUI applications, you should first install drivers that match the following systems. This way, you can use the virtual GPU (vGPU) to benefit from hardware-accelerated OpenGL rendering.

Intel GPU drivers
AMD GPU drivers
NVIDIA GPU drivers

  1. Update the Ubuntu system

Run the following in the Linux system:

1
sudo apt update && sudo apt upgrade -y
  1. Install the gedit text editor
1
sudo apt install gedit -y

After installation, you can see the Linux applications in the Windows Start menu and run them directly by clicking.

You can also launch GUI programs with terminal commands in the Linux system.

  1. Install Google Chrome for Linux

Change the directory to the temporary folder:

1
cd /tmp

Use wget to download it:

1
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
1
sudo apt install -f ./google-chrome-stable_current_amd64.deb

The -f option is used to fix broken dependencies that may occur during installation. The ./ prefix specifies the current directory where the .deb file is located. If the .deb file is in another directory, you need to specify the path to that file in the command.

I won’t install more applications — after all, WSLg isn’t my main use case.

Ubuntu System Configuration

Setting Up Chinese Fonts in Ubuntu

To let WSL directly use Windows fonts: WSL automatically mounts the Windows font directory at /mnt/c/Windows/Fonts; we just need to specify the fonts.

1
sudo gedit /etc/fonts/local.conf

Add the following content to the opened file:

1
2
3
4
5
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/mnt/c/Windows/Fonts</dir>
</fontconfig>

After saving, refresh the font cache.

1
sudo fc-cache -fv

Use the fc-list command to view the font list and confirm that Windows fonts are recognized.

1
2
3
4
5
6
7
8
9
10
$ fc-list :lang=zh | grep "Microsoft YaHei"
/mnt/c/Windows/Fonts/msyhbd.ttc: Microsoft YaHei,微软雅黑:style=Bold,Negreta,tučné,fed,Fett,Έντονα,Negrita,Lihavoitu,Gras,Félkövér,Grassetto,Vet,Halvfet,Pogrubiony,Negrito,Полужирный,Fet,Kalın,Krepko,Lodia
/mnt/c/Windows/Fonts/msyh.ttc: Microsoft YaHei UI:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/mnt/c/Windows/Fonts/msyh.ttc: Microsoft YaHei,微软雅黑:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/mnt/c/Windows/Fonts/msyhbd.ttc: Microsoft YaHei UI:style=Bold,Negreta,tučné,fed,Fett,Έντονα,Negrita,Lihavoitu,Gras,Félkövér,Grassetto,Vet,Halvfet,Pogrubiony,Negrito,Полужирный,Fet,Kalın,Krepko,Lodia
/mnt/c/Windows/Fonts/msyhl.ttc: Microsoft YaHei UI,Microsoft YaHei UI Light:style=Light,Regular
/mnt/c/Windows/Fonts/msyhl.ttc: Microsoft YaHei,微软雅黑,Microsoft YaHei Light,微软雅黑 Light:style=Light,Regular

$ fc-list :lang=zh | grep "SimHei"
/mnt/c/Windows/Fonts/simhei.ttf: SimHei,黑体:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta

Installing the 1Panel Panel

https://1panel.cn/

1
sudo bash -c "$(curl -sSL https://resource.fit2cloud.com/1panel/package/v2/quick_start.sh)"

The success message is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[1Panel 2026-01-05 22:35:44 install Log]: 1Panel 服务已成功启动,正在继续执行后续配置,请稍候...
[1Panel 2026-01-05 22:35:48 install Log]:
[1Panel 2026-01-05 22:35:48 install Log]: =================感谢您的耐心等待,安装已完成==================
[1Panel 2026-01-05 22:35:48 install Log]:
[1Panel 2026-01-05 22:35:48 install Log]: 请使用您的浏览器访问面板:
[1Panel 2026-01-05 22:35:48 install Log]: 外部地址: http://60.21.105.84:9999/0000
[1Panel 2026-01-05 22:35:48 install Log]: 内部地址: http://192.168.0.110:9999/0000
[1Panel 2026-01-05 22:35:48 install Log]: 面板用户: wgxls
[1Panel 2026-01-05 22:35:48 install Log]: 面板密码: Ww0121050272.
[1Panel 2026-01-05 22:35:48 install Log]:
[1Panel 2026-01-05 22:35:48 install Log]: 官方网站: https://1panel.cn
[1Panel 2026-01-05 22:35:48 install Log]: 项目文档: https://1panel.cn/docs
[1Panel 2026-01-05 22:35:48 install Log]: 代码仓库: https://github.com/1Panel-dev/1Panel
[1Panel 2026-01-05 22:35:48 install Log]: 前往 1Panel 官方论坛获取帮助: https://bbs.fit2cloud.com/c/1p/7
[1Panel 2026-01-05 22:35:48 install Log]:
[1Panel 2026-01-05 22:35:48 install Log]: 如果您使用的是云服务器,请在安全组中打开端口 9999
[1Panel 2026-01-05 22:35:48 install Log]:
[1Panel 2026-01-05 22:35:48 install Log]: 为了您的服务器安全,离开此屏幕后您将无法再次看到您的密码,请记住您的密码。
[1Panel 2026-01-05 22:35:48 install Log]:
[1Panel 2026-01-05 22:35:48 install Log]: ================================================================

Here I set port 9999 for access.

After installation, if you can successfully access http://localhost:9999/0000 or http://127.0.0.1:9999/0000 from the Windows browser, it means the Mirrored networking mode and Host address loopback configured in WSL Settings are correct.

Installing Miniconda

https://www.anaconda.com/docs/getting-started/miniconda/install

1
2
3
4
sudo mkdir /opt/miniconda3
cd /tmp
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
sudo bash Miniconda3-latest-Linux-x86_64.sh -b -u -p /opt/miniconda3

Activate the conda environment.

1
source /opt/miniconda3/bin/activate

Initialize conda for all shell environments.

1
conda init --all

After running the above commands, reopening the terminal will default to conda’s base environment.

If you want to disable conda’s auto-activation, run the following command:

1
conda config --set auto_activate false

Enabling the SSH Service

Install the OpenSSH server:

1
sudo apt install openssh-server -y

Start the SSH service:

1
sudo systemctl start ssh

Enable it to start automatically at boot:

1
sudo systemctl enable ssh

Check the SSH service status:

1
sudo systemctl status ssh

If the output shows Active: active (running), the SSH service has started successfully.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
wgxls@DESKTOP-DMRH21F:~$ sudo systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/usr/lib/systemd/system/ssh.service; enabled; preset: enabled)
Active: active (running) since Tue 2026-01-06 20:48:22 CST; 48s ago
TriggeredBy: ● ssh.socket
Docs: man:sshd(8)
man:sshd_config(5)
Process: 2314 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Main PID: 2316 (sshd)
Tasks: 1 (limit: 38088)
Memory: 1.3M (peak: 1.6M)
CPU: 11ms
CGroup: /system.slice/ssh.service
└─2316 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"

Jan 06 20:48:22 DESKTOP-DMRH21F systemd[1]: Starting ssh.service - OpenBSD Secure Shell server...
Jan 06 20:48:22 DESKTOP-DMRH21F sshd[2316]: Server listening on 0.0.0.0 port 22.
Jan 06 20:48:22 DESKTOP-DMRH21F sshd[2316]: Server listening on :: port 22.
Jan 06 20:48:22 DESKTOP-DMRH21F systemd[1]: Started ssh.service - OpenBSD Secure Shell server.

If you can’t connect to WSL via SSH from an external device, you may need to configure the Windows Firewall to allow inbound connections on port 22.

Open Windows Security, then click Firewall & network protection => Advanced settings

Click Inbound Rules => New Rule…, select Port, and click Next.

Select TCP, specify local port 22, and click Next.

Here I chose All local ports, which allows all ports through the firewall.

Select Allow the connection and click Next.

Check Domain, Private, and Public, then click Next.

Name the rule, e.g. IN_TCP_ALL, and click Finish.

You can repeat the steps to create inbound or outbound rules for the UDP protocol.

Allowing Root Login via SSH

I mainly use SSH to log into WSL within my LAN. For convenience, I configured the 1Panel panel to allow root users to log in via SSH.

Set a password for the root user:

1
sudo passwd root
1
2
3
4
5
wgxls@DESKTOP-DMRH21F:~$ sudo passwd root
[sudo] password for wgxls: <-- enter the current user's password
New password: <-- enter the new password for root
Retype new password: <-- re-enter the new password for root
passwd: password updated successfully

Allowing a Regular User to Run sudo Without a Password

Edit the sudoers file:

1
sudo visudo

Add the following content at the end of the file:

1
wgxls ALL=(ALL) NOPASSWD: ALL

Save and exit. Now user wgxls will no longer be prompted for a password when using the sudo command.

Modifying the PATH Environment Variable for SSH Sessions

In WSL, by default, the PATH environment variable when logging in via SSH may differ from when logging in directly in the WSL terminal. This can cause some commands to be unfindable.

For example, the nvidia-smi command works in the WSL terminal:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
wgxls@DESKTOP-DMRH21F:~$ nvidia-smi
Tue Jan 6 22:06:31 2026
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 590.48.01 Driver Version: 591.59 CUDA Version: 13.1 |
+-----------------------------------------+------------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 NVIDIA GeForce RTX 5070 Ti On | 00000000:01:00.0 On | N/A |
| 30% 39C P0 31W / 300W | 1867MiB / 16303MiB | 0% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| No running processes found |
+-----------------------------------------------------------------------------------------+
wgxls@DESKTOP-DMRH21F:~$ echo $PATH
/opt/miniconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Users/wgxls/AppData/Local/Microsoft/WindowsApps:/mnt/d/Microsoft VS Code/bin:/snap/bin

But when logging in via SSH, running the nvidia-smi command reports that the command can’t be found:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
wgxls@DESKTOP-DMRH21F:~$ echo $PATH
/opt/miniconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

wgxls@DESKTOP-DMRH21F:~$ nvidia-smi
Command 'nvidia-smi' not found, but can be installed with:
sudo apt install nvidia-utils-470 # version 470.256.02-0ubuntu0.24.04.1, or
sudo apt install nvidia-utils-470-server # version 470.256.02-0ubuntu0.24.04.1
sudo apt install nvidia-utils-535 # version 535.274.02-0ubuntu0.24.04.2
sudo apt install nvidia-utils-535-server # version 535.274.02-0ubuntu0.24.04.2
sudo apt install nvidia-utils-570 # version 570.195.03-0ubuntu0.24.04.1
sudo apt install nvidia-utils-525 # version 525.147.05-0ubuntu1
sudo apt install nvidia-utils-525-server # version 525.147.05-0ubuntu1
sudo apt install nvidia-utils-565-server # version 565.57.01-0ubuntu0.24.04.3
sudo apt install nvidia-utils-570-server # version 570.195.03-0ubuntu0.24.04.3
sudo apt install nvidia-utils-580 # version 580.95.05-0ubuntu0.24.04.3
sudo apt install nvidia-utils-580-server # version 580.95.05-0ubuntu0.24.04.3
sudo apt install nvidia-utils-550-server # version 550.163.01-0ubuntu0.24.04.1

wgxls@DESKTOP-DMRH21F:~$ /usr/lib/wsl/lib/nvidia-smi
Tue Jan 6 22:09:43 2026
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 590.48.01 Driver Version: 591.59 CUDA Version: 13.1 |
+-----------------------------------------+------------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 NVIDIA GeForce RTX 5070 Ti On | 00000000:01:00.0 On | N/A |
| 0% 42C P0 29W / 300W | 1787MiB / 16303MiB | 0% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| No running processes found |
+-----------------------------------------------------------------------------------------+

You can see that when logging in via SSH, the PATH environment variable has many fewer paths than in the WSL terminal, and it reports that the nvidia-smi command can’t be found.

In fact, the nvidia-smi command is located in the /usr/lib/wsl/lib/ directory.

To solve this problem, we can configure the /etc/environment file and add the missing paths.

This change affects all users logging in via SSH.

1
sudo vim /etc/environment

Here I won’t add too many other paths — I’ll remove all paths starting with /mnt and keep only the /usr/lib/wsl/lib path.

Before:

1
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"

After:

1
PATH="/opt/miniconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/snap/bin"

After re-logging in via SSH, the nvidia-smi command works properly.

Modifying systemd Environment Variables

Now services started by systemd (such as the 1Panel panel service) also can’t find commands under the /usr/lib/wsl/lib path.

To let systemd services also find commands under that path, we need to modify systemd’s environment variable configuration file.

1
sudo vim /etc/systemd/system.conf

Find the #DefaultEnvironment= line in the file, uncomment it, and add the paths we need.

Before:

1
#DefaultEnvironment=

After:

1
DefaultEnvironment=PATH="/opt/miniconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/snap/bin"

Save and exit.

Restart systemd:

1
sudo systemctl daemon-reexec

You can use the systemctl show-environment command to view systemd’s environment variables and confirm the change has taken effect.

1
2
3
wgxls@DESKTOP-DMRH21F:~$ systemctl show-environment
LANG=C.UTF-8
PATH=/opt/miniconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/snap/bin

Restart the 1Panel service:

1
sudo 1pctl restart

You can see that the 1Panel service has started successfully and the nvidia-smi command works properly.

Mounting an ext4 Disk in WSL

Windows can’t directly access the ext4 file system. If a disk is already formatted as ext4, you can access it through WSL.

Because my mechanical hard drive was previously used in a Linux system and formatted as ext4, I now want to mount and use it in WSL.

At this point, I need to refer to the official documentation on mounting partitioned disks:

https://learn.microsoft.com/zh-cn/windows/wsl/wsl2-mount-disk#mounting-a-partitioned-disk

First, run Windows Terminal as administrator and use Get-CimInstance -Query "SELECT * from Win32_DiskDrive" to list all disks.

I have a 4TB mechanical hard drive here, with ID \\.\PHYSICALDRIVE0.

1
2
3
4
5
6
PS C:\Users\wgxls> Get-CimInstance -Query "SELECT * from Win32_DiskDrive"

DeviceID Caption Partitions Size Model
-------- ------- ---------- ---- -----
\\.\PHYSICALDRIVE0 WDC WD43PURZ-74BWPY0 1 4000784417280 WDC WD43PURZ-74BWPY0
\\.\PHYSICALDRIVE1 Fanxiang S790 2TB 3 2000396321280 Fanxiang S790 2TB

Run the wsl.exe --mount <Disk> --bare command to mount the disk:

1
wsl --mount \\.\PHYSICALDRIVE0 --bare

Then enter the WSL system and use the lsblk command to view the mounted disks:

You can see that my 4TB drive has been successfully mounted as disk sde, with one partition sde1.

1
2
3
4
5
6
7
8
9
10
wgxls@DESKTOP-DMRH21F:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 388.6M 1 disk
sdb 8:16 0 186M 1 disk
sdc 8:32 0 8G 0 disk [SWAP]
sdd 8:48 0 1T 0 disk /var/lib/docker
/mnt/wslg/distro
/
sde 8:64 0 3.6T 0 disk
└─sde1 8:65 0 3.6T 0 part

Next, we’ll set up automatic mounting of this disk at boot by modifying the /etc/fstab file.

Get the partition’s UUID:

1
2
wgxls@DESKTOP-DMRH21F:~$ sudo blkid /dev/sde1
/dev/sde1: UUID="dceaaac0-3867-4b2c-86e2-2ddc4af28807" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="5a7936cd-5b8e-463d-a155-e6c15317f1c1"

Edit the /etc/fstab file:

1
sudo vim /etc/fstab

Add the following content at the end of the file:

1
UUID=dceaaac0-3867-4b2c-86e2-2ddc4af28807 /mnt/4tb_disk ext4 defaults,nofail 0 2

Save and exit.

Create the mount point directory:

1
sudo mkdir /mnt/4tb_disk

Restart WSL:

1
wsl --shutdown

Re-enter the WSL system and use the df -h command to check the mount status:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
wgxls@DESKTOP-DMRH21F:~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sdd 1007G 5.2G 951G 1% /
rootfs 16G 2.7M 16G 1% /init
none 16G 0 16G 0% /dev/shm
none 16G 520K 16G 1% /run
none 16G 0 16G 0% /run/lock
/dev/sde1 3.6T 1.4T 2.1T 40% /mnt/4tb_disk
drivers 301G 50G 251G 17% /usr/lib/wsl/drivers
none 16G 0 16G 0% /usr/lib/wsl/lib
none 16G 4.0K 16G 1% /mnt/wsl
none 16G 84K 16G 1% /mnt/wslg
none 16G 0 16G 0% /usr/lib/modules/6.6.87.2-microsoft-standard-WSL2
C:\ 301G 50G 251G 17% /mnt/c
D:\ 1.6T 11G 1.6T 1% /mnt/d

You can see the 4TB drive has been successfully mounted to the /mnt/4tb_disk directory.

Auto-Starting WSL at Boot and Keeping It Running

By default, WSL distributions shut down automatically when unused to save system resources. However, sometimes we want WSL to start automatically at system boot and stay running, especially when we run servers or background services in WSL.

This can be achieved by creating a scheduled task.

  1. Open Task Scheduler

Press Win + R, type taskschd.msc and press Enter to open Task Scheduler.

  1. Click “Create Task…”

  1. In the “General” tab, enter a task name, e.g. “wsl-mount”, and select “Run with the highest privileges”.

  1. In the “Triggers” tab, click “New…”, choose “At startup” as the trigger, check the “Delay task for” option, set the delay to 30 seconds, and click “OK”.

  1. In the “Actions” tab, click “New…”, choose “Start a program” as the action, enter C:\Windows\System32\wsl.exe in Program/script, enter --mount \\.\PHYSICALDRIVE0 --bare in Add arguments, enter C:\Windows\System32 in Start in (or leave it blank), and click “OK”.

The first action mounts the hard drive into WSL.

Then add a shell session to keep WSL running.

  1. In the “Conditions” tab, keep the default settings. If you’re a laptop user, you can uncheck “Start the task only if the computer is on AC power”.

  2. In the “Settings” tab, uncheck the “Stop the task if it runs longer than” option and keep the other default settings.

Finally, click OK to save the task.

After restarting the computer, you can see that the WSL distribution has started automatically and stays running.

⚠️

After configuring it this way, WSLg seems to stop working properly — GUI applications can’t start. If you need to use WSLg, you’ll have to run wsl --shutdown first to shut down WSL, then reopen the WSL distribution.

Once you run wsl --shutdown, the WSL session started by the scheduled task will be terminated. WSL can no longer stay running continuously.