Skip to content
Go back

WSL 基本使用

Updated:  at  11:24

On this page

原文链接: https://blog.llweb.top/posts/linux/wsl-basic

安装

Microsoft Store 装安装

安装后首次启动会让你设置用户名,密码。更换默认用户为 root: https://superuser.com/questions/1566022/how-to-set-default-user-for-manually-installed-wsl-distro

.wslconfig 文件参考:https://learn.microsoft.com/zh-cn/windows/wsl/wsl-config#wslconfig

wsl cli 参考:https://learn.microsoft.com/zh-cn/windows/wsl/basic-commands

卸载

  1. wsl --unregister Ubuntu
  2. 利用 Bulk Crap Uninstaller 卸载干净

更换 Ubuntu apt-get 源

方法: https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/

源: http://mirrors.aliyun.com/ubuntu/

在 Ubuntu 24.04 之前,Ubuntu 的软件源配置文件使用传统的 One-Line-Style,路径为 /etc/apt/sources.list;从 Ubuntu 24.04 开始,Ubuntu 的软件源配置文件变更为 DEB822 格式,路径为 /etc/apt/sources.list.d/ubuntu.sources

安装 docker

https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository

docker 镜像

https://github.com/DaoCloud/public-image-mirror

https://status.daocloud.io/status/docker

sudo tee /etc/docker/daemon.json <<EOF
 
{
    "registry-mirrors": [
        "https://docker.m.daocloud.io"
    ]
}
EOF
 
sudo systemctl daemon-reload
sudo systemctl restart docker

镜像也慢,不如代理

proxy

使用代理: https://docs.docker.com/engine/daemon/proxy/

但 wsl 的网络模式影响,nat 不能访问主机代理,换成 mirrored

代理设置参考:https://george.betterde.com/cn/technology/20240608/

Docker Daemon Proxy

负责 docker run 和 docker build 阶段的代理配置

/etc/docker/daemon.json

{
  "proxies": {
    "no-proxy": "*.local,localhost,127.0.0.0/8",
    "http-proxy": "http://127.0.0.1:7890",
    "https-proxy": "http://127.0.0.1:7890"
  }
}

Docker Client Proxy

负责 docker run 和 docker build 阶段的代理配置

~/.docker/config.json

{
 "proxies": {
   "default": {
     "httpProxy": "http://127.0.0.1:7890",
     "httpsProxy": "http://127.0.0.1:7890",
     "noProxy": "*.local,localhost,127.0.0.0/8"
   }
 }
}

设置这个代理后 docker build 时,RUN npm install -g pnpm 报错,代理连不上

在 WSL 中访问 Windows 文件有几种便捷的方法

  1. 通过 /mnt 目录访问
  1. 通过符号链接创建快捷方式
# 在 home 目录下创建到 Windows Documents 文件夹的链接
ln -s /mnt/c/Users/YourName/Documents ~/Documents
# 快速访问
cd ~/windows_desktop

配置 /etc/wsl.conf 来自定义挂载点

需要注意的几点:

性能差异的原因:

若需频繁操作,建议将文件复制到 WSL 原生目录

# 在 Windows 文件系统中运行 npm install
cd /mnt/c/projects/my-node-app
npm install  # 较慢

# 在 Linux 文件系统中运行 npm install
cd ~/projects/my-node-app
npm install  # 更快
# 打开当前目录对应的 Windows 文件夹
explorer.exe .
# 将 Windows 路径转为 WSL 路径
wslpath "C:\Users\你的用户名\Downloads"
# 输出:/mnt/c/Users/你的用户名/Downloads

# 将 WSL 路径转为 Windows 路径
wslpath -w ~/documents
# 输出:\\wsl.localhost\Ubuntu\home\用户名\documents

备份 WSL

https://stackoverflow.com/questions/38779801/move-wsl-bash-on-windows-root-filesystem-to-another-hard-drive/51767786#51767786

troubleshooting

常见问题解答:https://learn.microsoft.com/en-us/windows/wsl/faq

wsl: 检测到 localhost 代理配置,但未镜像到 WSL。NAT 模式下的 WSL 不支持 localhost 代理。

https://github.com/microsoft/WSL/issues/10753#issuecomment-1814839310

https://github.com/microsoft/WSL/releases/tag/2.0.0

找不到被占用的端口

netsh interface ipv4 reset
netsh interface ipv6 reset
netsh winsock reset 

netsh int ipv4 add excludedportrange protocol=tcp startport=3000 numberofports=100

Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All

https://stackoverflow.com/questions/58216537/what-is-administered-port-exclusions-in-windows-10


Suggest Changes

Next Post
zustand vs jotai vs valtio

Most Related Posts

  • My Linux

    Updated:  at  11:31

    my linux

  • generative ai for beginners

    Published:  at  21:04

    generative ai for beginners

  • Linux Basic Command

    Updated:  at  00:20

    Linux Basic Command

  • codec in js

    Published:  at  17:00

    codec in js

  • low code

    Published:  at  20:30

    low code