Skip to content

Instantly share code, notes, and snippets.

@morlay
Last active April 14, 2021 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morlay/db56c60c9d12f4f18b89c8b4bc92babe to your computer and use it in GitHub Desktop.
Save morlay/db56c60c9d12f4f18b89c8b4bc92babe to your computer and use it in GitHub Desktop.
# WSL 初探

WSL 1 的坑已经不修了,WSL 2 还是走向了虚拟化。 因此,WSL 是另一个系统,并且和纯 Linux 有所差异

文件

Windows in WSL

通过 /mnt/c/mnt/d 等路径,可以在 WSL 直接访问到 Windows 中的文件。 甚至是调用 Windows 的命令,添加 PATH 即可:

export PATH=$PATH:/mnt/c/Windows/System32

export PATH=$PATH:/mnt/c/Program\ Files/JetBrains/IntelliJ\ IDEA\ 2019.3.1/bin
alias idea="idea64.exe $@"

但有些 IO 性能问题,microsoft/WSL#4197 因此,目前需要将代码放在 Linux 的文件系统中,不建议在 /mnt

WSL in Windows(以 ubuntu 为例)

在 Windows 中,通过网络路径 \\wsl$\ubuntu\* 可以访问到访问 WSL 文件 对于 WSL 中的命令,部分可用 \\wsl$\ubuntu\bin\*,但绝大多数,应该使用 "ubuntu" run * 代替 (在 IDE 中,如 IntelliJ IDEA,可这样配置。不过由于文件路径的不兼容,当前不建议在 IDE 中开发,自虐,IDE 的 git UI 不得不使用 wslgit 这类三方黑魔法)

网络

在 Windows 中, localhost/127.0.0.1:PORT 可直接访问到 WSL 暴露地址。 但反过来,暂时需要通过宿主机的 wlan ip 访问,可如下配置

export PATH=$PATH:/mnt/c/Windows/System32
export WINDOWS_HOST=`ipconfig.exe | grep -n4 WLAN  | tail -n 1 | awk -F":" '{ print $2 }'  | sed 's/^[ \r\n\t]*//;s/[ \r\n\t]*$//'`

alias proxy4="http_proxy=http://$WINDOWS_HOST:1087 https_proxy=http://$WINDOWS_HOST:1087 $@"

VPN

需要先启动 VPN,再启动 WSL,外部 VPN 才可被 WSL 生效

杂项

Systemd

暂未支持

因此 sudo systemctl enable docker 是没有办法生效的

三方黑魔法:https://github.com/arkane-systems/genie

结论

Windows 暂时无法作为主力开发环境,但未来可期

@Veitor
Copy link

Veitor commented Apr 13, 2021

之前在windows上用docker开发的好好的,啥文件也能挂载到容器中运行。。用了WSL后,直接在windows上挂载文件慢成狗。。。网上看了说要把代码放到Linux子系统里?那我IDE能打开文件吗?git能推拉代码吗?不知道这wsl下怎么开发了。。大神有推荐的思路没

@morlay
Copy link
Author

morlay commented Apr 14, 2021

@Veitor
IDE 支持打开 WSL 路径的 \\wsl$\ubuntu\

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment