Windows 系统在 WSL 中安装 Debian。

1、安装 WSL

在 Windows 10 或 Windows 11 系统中,打开 Windows Terminal (终端),如果没安装 WSL,使用以下命令安装 WSL:

1
wsl.exe --install

如果失败就多试几次,期间可能会显示需要提权,点击“是”或“允许”。

如果开始下载了就耐心等待,可能比较慢,下载完会自动安装。

安装完成后,需要重启系统才能开始使用。

2、WSL 安装 Debian

重启系统以后,打开 Windows Terminal,输入以下命令开始安装 Debian:

1
wsl --install Debian

安装完成后,会要求输入系统的用户名,输入 debian 即可,接着设置密码。

输入完密码,按回车,会再次让确认密码,再输一次后按回车。

安装完成。

3、启动 Debian

打开 Windows Terminal,输入以下命令启动 Debian:

1
wsl -d Debian

4、配置 Debian 系统

截止 2026年4月17日,WSL 中的 Debian 版本是 13。

输入以下命令打开 apt 源配置:

1
nano /etc/apt/sources.list

把文件中的 deb.debian.org 替换为 mirrors.tuna.tsinghua.edu.cn

替换前:

1
2
3
4
deb http://deb.debian.org/debian trixie main
deb http://deb.debian.org/debian trixie-updates main
deb http://security.debian.org/debian-security trixie-security main
deb http://ftp.debian.org/debian trixie-backports main

替换后:

1
2
3
4
deb http://mirrors.tuna.tsinghua.edu.cn/debian trixie main
deb http://mirrors.tuna.tsinghua.edu.cn/debian trixie-updates main
deb http://security.debian.org/debian-security trixie-security main
deb http://mirrors.tuna.tsinghua.edu.cn/debian trixie-backports main

替换完成后,按 Ctrl + X,再按 Y 保存,再按回车,即可实现 “保存 + 退出” nano 编辑器。

执行如下命令更新源:

1
apt update

安装 CA 证书:

1
apt install ca-certificates

安装完 CA 证书后,就可以把 /etc/apt/sources.list 中清华源镜像改为 https

如下所示:

1
2
3
4
deb https://mirrors.tuna.tsinghua.edu.cn/debian trixie main
deb https://mirrors.tuna.tsinghua.edu.cn/debian trixie-updates main
deb http://security.debian.org/debian-security trixie-security main
deb https://mirrors.tuna.tsinghua.edu.cn/debian trixie-backports main

OK,初步配置完成,后续就可以愉快的玩耍了。

5、Debian 安装 Docker

在 Debian 13 中安装 docker 就比较简单了,执行如下命令:

1
2
apt update
apt install docker.io

Docker 已安装就位。

到这里还不能直接用,因为 docker hub 总是喜欢颤抖。

执行命令:

1
2
mkdir -p /etc/docker
nano /etc/docker/daemon.json

把如下内容粘贴进去:

1
2
3
4
5
6
7
8
9
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"http://hub-mirror.c.163.com",
"https://docker.xuanyuan.me",
"https://docker.1ms.run",
"https://docker.m.daocloud.io"
]
}

按 Ctrl + X,再按 Y 保存,再按回车,即可退出。

6、WSL 安装 Debian 11

那如果要在 WSL 里安装 Debian 11 呢,装完 Docker 以后也简单了。

1
2
docker pull debian:11
# 或者 docker pull debian:bullseye

执行 docker images 查看 debian11 的 “IMAGE ID”。

1
2
3
4
5
6
7
8
root@notebook:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python 3.10-slim b6e62c8f810a 2 weeks ago 147MB
node 20-slim 35175cc350f3 2 weeks ago 222MB
debian 11 3faeb4bdf5c9 2 weeks ago 124MB
debian 12 3f011f36e5ef 2 weeks ago 117MB
redis 7-alpine 49898aa0365c 8 weeks ago 41.8MB
alpine latest 1ab49c19c53e 2 months ago 8.7MB

使用 “IMAGE ID” 来导出 rootfs:

1
2
3
4
5
# 请将 <IMAGE_ID> 替换为上一步找到的实际镜像ID
docker export $(docker create <IMAGE_ID>) > debian11-rootfs.tar

# 按上面的镜像ID, 实际执行
docker export $(docker create 3faeb4bdf5c9) > debian11-rootfs.tar

把这个文件导出到 Windows 系统D盘:

1
cp debian11-rootfs.tar /mnt/d/

在 Windows 下打开 Windows Terminal,输入如下命令,即可在 WSL 中安装 Debian 11:

1
2
3
4
5
c:
mkdir wsl

d:
wsl --import Debian11 C:\wsl\debian11 .\debian11-rootfs.tar --version 2

命令解释:

  • Debian11 → 发行版的名字(随便取)
  • C:\wsl\debian11 → 安装目录
  • .\debian11-rootfs.tar → 刚下载的 rootfs 文件
  • –version 2 → 指定使用 WSL2

7、WSL 安装 Debian 12

拉取镜像换成这个:

1
2
docker pull debian:12
# 或者 docker pull debian:bookworm

其他步骤跟安装 Debian 11 一样。