WSL 安装 Linux

Windows 上一条命令装好 WSL 2 与 Ubuntu,以及文件系统、网络、Docker 的注意事项。

Beginner
Key Pointswsl --install 一条命令装完;代码放 Linux 文件系统里(/home)才快,放 /mnt/c 会慢十倍。

安装

POWERSHELL
wsl --install                 # 默认装 Ubuntu
wsl --install -d Debian       # 指定发行版
wsl --list --online           # 可装的列表
wsl --set-default-version 2

常用命令

POWERSHELL
wsl -l -v                     # 列出已装发行版与版本
wsl --shutdown                # 关掉所有实例
wsl --update                  # 升级 WSL 本体
wsl --unregister Ubuntu       # 卸载(会删数据)

文件系统

Linux files live at \\wsl$\Ubuntu\home\you and are reachable from Windows; Windows drives appear under /mnt/c. Crossing the boundary has translation overhead, so keep project code on the Linux side.

Bash
cd ~/projects/app        # 快
cd /mnt/c/Users/me/app   # 慢,只适合临时看文件

网络与端口

Bash
ip addr show eth0 | grep inet      # WSL 的 IP
# Windows 里访问 WSL 服务:localhost:端口 通常直接可用(WSL 2 有端口转发)

在 WSL 里装 Docker

Bash
curl -fsSL https://get.docker.com | sudo sh
sudo usermod -aG docker $USER
sudo service docker start        # 或用 systemd
systemctl inside WSL requires [boot] systemd=true in /etc/wsl.conf and a wsl --shutdown. Without it, use service xxx start.

Friends & Partners