wsl2交互
1、 PowerShell或CMD中运行linux命令
D:\>wsl ls -l
2、WSL可直中运行的 Windows 可执行文件
gelare@archie:~$ ipconfig.exe /all
3、从windows访问linux文件
\\wsl$
4、WSL访问 Windows 文件
/mnt/c
5、Windows网络访问WSL
WSL下运行一个服务:
gelare@archie:~$ nc -l -p 9999
Windows访问:
D:\>telnet localhost 9999 Trying ::1... Connection failed: Connection refused Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. hello
6、WSL网络访问Windows
windows下运行一个服务:
D:\>socat tcp-l:9999 stdout
WSL下获得地址并访问:
gelare@archie:~$ ipconfig.exe | grep -n4 WSL | tail -n 1 | awk -F":" '{ print $2 }' | sed 's/^[ \r\n\t]*//;s/[ \r\n\t]
*$//'
172.23.208.1
gelare@archie:~$ telnet 172.23.208.1 9999
Trying 172.23.208.1...
Connected to 172.23.208.1.
Escape character is '^]'.
hello
*$//'
172.23.208.1
gelare@archie:~$ telnet 172.23.208.1 9999
Trying 172.23.208.1...
Connected to 172.23.208.1.
Escape character is '^]'.
hello
7、wsl2中打开图形界面
可以在windows下运行xming,并打开防火墙允许接入,然后wsl下运行xterm:
gelare@archie:/mnt/c/Users/gelare$ export DISPLAY=192.168.144.1:0.0 gelare@archie:/mnt/c/Users/gelare$ xterm
可能出现以下报错:
gelare@archie:/mnt/c/Users/gelare$ xterm No protocol specified xterm: Xt error: Can't open display: 192.168.144.1:0.0
如果在xming日志出现:Xming.exe: client 4 rejected from IP, 需要在Xming安装目录下X0.hosts中加入wsl中的网卡地址。
重起wsl的地址会发生变化,每次修改还是比较麻烦的。
8、可以把下段放到.bashrc里,自动完成DISPLAY设置
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0 export LIBGL_ALWAYS_INDIRECT=1 sudo /etc/init.d/dbus start &> /dev/null
WSL2详细内容:
https://docs.microsoft.com/zh-cn/windows/wsl/reference