常用操作

导出

1
wsl --export centos7 centos7.tar

导入

1
wsl --import centos7 C:\centos7 centos7.tar

启动指定发行版

1
wsl -d centos7

指定用户启动

1
wsl -d ubuntu -u root

开机自启

win+r 输入 shell:startup写一个wsl_start.bat内容如下

1
wsl -d Ubuntu -u root /etc/init.d/ssh start

sudo vim /etc/wsl.conf

1
2
[boot]
command = service docker start

修改默认用户为root

1
%localappdata%\Microsoft\WindowsApps\ubuntu.exe config --default-user root

根据你的发行版决定

sudo vim /etc/wsl.conf

1
2
[user]
default = root

安装想要的发行版

这里以centos为例

首先需要docker环境,执行

1
docker run --name centos7 centos:centos7

然后将容器导出

1
docker export centos7 > centos7.tar

然后在windows上导入

1
wsl --import centos7 C:\centos7 centos7.tar

启动发行版

1
wsl -d centos7

卸载发行版

1
wsl --unregister centos7

互相访问

wsl 里运行windows 程序

1
2
3
4
5
# 运行windows里的python
py.exe update_gitee_pages.py

# 打开资源管理器
explorer.exe .

windows里运行wsl程序

1
wsl -d ubuntu sudo bash -c "apt update"

wsl里访问windows路径

1
cd /mnt/c/Users/iuxt/Desktop

windows 里访问wsl文件

1
\\wsl$\Ubuntu\home\iuxt

快速在当前路径下打开wsl命令行

在资源管理器的地址栏直接输入wsl即可(输入cmd类似,是进入cmd命令行)

快速打开wsl命令行|572

bat脚本里调用wsl命令

1
wsl --shell-type login --cd ~/code/zahuifan -- code .

文件路径转换

1
2
3
4
5
6
7
# wsl路径转换为windows路径
❯ wslpath -w "/mnt/c/Program Files (x86)/Steam/steamapps"
C:\Program Files (x86)\Steam\steamapps

# windows路径转换为wsl路径
❯ wslpath "C:\Program Files (x86)\Steam\steamapps"
/mnt/c/Program Files (x86)/Steam/steamapps

wsl.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# https://learn.microsoft.com/zh-cn/windows/wsl/wsl-config

[automount]
enabled=true
mountFsTab=true
options="metadata,dmask=0022,fmask=0077,umask=0022"
root=/mnt/

[filesystem]
umask=0022

[interop]
enabled=true
appendWindowsPath=false # 不添加 Windows 环境变量 Path,防止路径变量污染带来的干扰

# 其它网络配置
[network]
generateHosts=true
generateResolvConf=true


# boot command 暂不支持 nohup 后台启动
# command=nohup service cron start >/dev/null 2>&1 &
[boot]
# command=/root/.start.sh
systemd=true