WSL User Guide

WSL 用户指南

常用目录

内容 位置
WSL 2 磁盘位置 %LOCALAPPDATA%\Packages\<PackageFamilyName>\LocalState\<disk>.vhdx
WSL 2 虚机文件 %userprofile%/AppData/Local/Packages
WSL 2 文件系统 \\wsl$\

常用命令

命令 作用
wsl -l 列出安装的 Linux 版本
wsl -s Ubuntu-16.04 设置默认 Linux 版本
wsl -d docker-desktop 打开指定 Linux 的 Console
wsl -t Ubuntu-20.04 关闭 Ubuntu-20.04
wsl --export Ubuntu-20.04 TAR_FILE_PATH 备份 WSL Ubuntu-20.04
wsl --import Ubuntu-20.04 WSL_PATH TAR_FILE_PATH  恢复 WSL Ubuntu-20.04

WSL2

# 激活 WSL2 支持
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
wsl --set-default-version 2
# 转换已安装 Linux 发行版为 WSL2
wsl --set-version <Distro> 2
wsl --list --verbose

常用配置

Oh My Zsh

ZSH_THEME="ys"
VIRTUAL_ENV=(%B%F{77}'['$WSL_DISTRO_NAME']'%f%b)
# The starting script for SystemD
# /etc/profile.d/00-wsl2-systemd.sh

SYSTEMD_PID=$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.target$' | grep -v unshare | awk '{print $2}')

if [ -z "$SYSTEMD_PID" ]; then
   sudo /usr/bin/daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
   SYSTEMD_PID=$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.target$' | grep -v unshare | awk '{print $2}')
fi

if [ -n "$SYSTEMD_PID" ] && [ "$SYSTEMD_PID" != "1" ]; then
    exec sudo /usr/bin/nsenter -t $SYSTEMD_PID -a su - $LOGNAME
fi
# 禁用退格键空按声音提醒
# 默认配置,空按退格键或删除键会有嘟嘟嘟的提示音

vim ~/.inputrc
# 保存以下设置
set bell-style none
# 挂载 U 盘,想定 U 盘盘符为 D
sudo mount -t drvfs d: /mnt/d
# 设置 root 为起始用户
ubuntu1604 config --default-user root
ubuntu2004 config --default-user root
# WSL 2 配置
# Windows %USERPROFILE%/.wslconfig
[wsl2]
memory=6GB      # Limits VM memory in WSL 2 to 4 GB
processors=2    # Makes the WSL 2 VM use two virtual processors
# https://docs.microsoft.com/en-us/windows/wsl/wsl-config#configure-global-options-with-wslconfig

运行图形工具

安装 Window-X 服务:VcXsrv。在用户的 .bashrc 文件中配置 X-Window 服务地址。

# 配置图形界面使用 host 的 xWindow 服务器
vim ~/.bashrc
export DISPLAY=localhost:0.0
# 常用图形工具
sudo apt-get install chromium-browser
# Windows 下图形工具快捷方式
C:\Windows\System32\bash.exe -c "export DISPLAY=:0.0;/usr/bin/code;"
# 一些必要的共享库及工具包
sudo apt-get install -y libxss1
sudo apt-get install -y libasound2
sudo apt-get install -y libgtk2.0-0
sudo apt-get install -y dbus-x11
sudo apt-get install -y geany geany-plugins # 
sudo apt-get install -y lxappearance        # GTK+ theme switcher
sudo apt-get install -y pcmanfm lxterminal  # GTK+ based File Manager tool
# 相关的配置
# 替换 terminal 抑制 PCManFM 报警信息
vim ~/.config/libfm/libfm.conf

# 找到
terminal=x-terminal-emulator %s

# 改成
terminal=lxterminal %s

常见错误

VSCode IPv4 无应答

[2021-09-27 04:23:58.496] *
[2021-09-27 04:23:58.496] * Visual Studio Code Server
[2021-09-27 04:23:58.496] *
[2021-09-27 04:23:58.496] * Reminder: You may only use this software with Visual Studio family products,
[2021-09-27 04:23:58.496] * as described in the license https://aka.ms/vscode-remote/license
[2021-09-27 04:23:58.496] *
[2021-09-27 04:23:58.496] 
[2021-09-27 04:23:58.496] IP Address: 172.27.246.35
[2021-09-27 04:23:58.496] Extension host agent listening on 43043
[2021-09-27 04:23:58.496] 
[2021-09-27 04:23:58.496] [12:23:58] Extension host agent started.
[2021-09-27 04:23:58.504] Pinging 172.27.246.35:43043...
[2021-09-27 04:25:19.686] 172.27.246.35:43043 no response
[2021-09-27 04:25:19.686] WSL resolver response: ::1:43043
[2021-09-27 04:25:19.686] To debug connection issues, open a local browser on http://[::1]:43043/version

VSCode 参考 Windows 代理,配置时需绕过本地域名或主机,或使用 "remote.WSL.server.connectThroughLocalhost": true 配置项

localhost;172.27.246.35;172.*.*.*;127.0.0.1;192.168.1.*

安装后无法运行

Installing, this may take a few minutes...
WslRegisterDistribution failed with error: 0x8007019e
The Windows Subsystem for Linux optional component is not enabled. Please enable it and try again.
See https://aka.ms/wslinstall for details.
Press any key to continue...

# Install WSL 2
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

启动 SSHd 报错

Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_dsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key

重新生成服务端公钥: 

sudo dpkg-reconfigure openssh-server
Author: njun
njun's picture
Updated: 2021/09/27