WSL学习笔记

资料

  1. 手动下载适用于 Linux 的 Windows 子系统发行版包
  2. wsl2-backup-and-restore-images-using-import-and-export
  3. 解决删除文件后 WSL2 磁盘空间不释放的问题
  4. https://github.com/JetBrains/clion-wsl
  5. 使用WSL配置mmdetection环境

SSH

1
2
3
4
5
sudo sed -i '/Port /c Port 23' /etc/ssh/sshd_config
sudo sed -i '/ListenAddress 0.0.0.0/c ListenAddress 0.0.0.0' /etc/ssh/sshd_config
sudo sed -i '/PasswordAuthentication /c PasswordAuthentication yes' /etc/ssh/sshd_config
sudo sed -i '/PermitRootLogin /c PermitRootLogin yes' /etc/ssh/sshd_config
sudo service ssh restart

也可以使用Jetbrains的 https://github.com/JetBrains/clion-wsl

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
set -e

SSHD_LISTEN_ADDRESS=127.0.0.1

SSHD_PORT=2222
SSHD_FILE=/etc/ssh/sshd_config
SUDOERS_FILE=/etc/sudoers

# 0. update package lists
sudo apt-get update

# 0.1. reinstall sshd (workaround for initial version of WSL)
sudo apt remove -y --purge openssh-server
sudo apt install -y openssh-server

# 0.2. install basic dependencies
sudo apt install -y cmake ninja-build gcc clang gdb valgrind build-essential

# 1.1. configure sshd
sudo cp $SSHD_FILE ${SSHD_FILE}.`date '+%Y-%m-%d_%H-%M-%S'`.back
sudo sed -i '/^Port/ d' $SSHD_FILE
sudo sed -i '/^ListenAddress/ d' $SSHD_FILE
sudo sed -i '/^UsePrivilegeSeparation/ d' $SSHD_FILE
sudo sed -i '/^PasswordAuthentication/ d' $SSHD_FILE
echo "# configured by CLion" | sudo tee -a $SSHD_FILE
echo "ListenAddress ${SSHD_LISTEN_ADDRESS}" | sudo tee -a $SSHD_FILE
echo "Port ${SSHD_PORT}" | sudo tee -a $SSHD_FILE
echo "UsePrivilegeSeparation no" | sudo tee -a $SSHD_FILE
echo "PasswordAuthentication yes" | sudo tee -a $SSHD_FILE
# 1.2. apply new settings
sudo service ssh --full-restart

# 2. autostart: run sshd
sed -i '/^sudo service ssh --full-restart/ d' ~/.bashrc
echo "%sudo ALL=(ALL) NOPASSWD: /usr/sbin/service ssh --full-restart" | sudo tee -a $SUDOERS_FILE
cat << 'EOF' >> ~/.bashrc
sshd_status=$(service ssh status)
if [[ $sshd_status = *"is not running"* ]]; then
sudo service ssh --full-restart
fi
EOF


# summary: SSHD config info
echo
echo "SSH server parameters ($SSHD_FILE):"
echo "ListenAddress ${SSHD_LISTEN_ADDRESS}"
echo "Port ${SSHD_PORT}"
echo "UsePrivilegeSeparation no"
echo "PasswordAuthentication yes"

32位支持

1
sudo dpkg --add-architecture i386

以下是wsl1,不是wsl2

Exec format error 32-bit executable Windows Subsystem for Linux?

Install qemu and binfmt config:

1
2
sudo apt install qemu-user-static
sudo update-binfmts --install i386 /usr/bin/qemu-i386-static --magic '\x7fELF\x01\x01\x01\x03\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x03\x00\x01\x00\x00\x00' --mask '\xff\xff\xff\xff\xff\xff\xff\xfc\xff\xff\xff\xff\xff\xff\xff\xff\xf8\xff\xff\xff\xff\xff\xff\xff'

You’ll need to reactivate binfmt support every time you start WSL:

1
sudo service binfmt-support start

Enable i386 architecture packages:

1
2
3
sudo dpkg --add-architecture i386
sudo apt update
sudo apt install gcc:i386

Try it out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ file /usr/bin/gcc-5
/usr/bin/gcc-5: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=2637bb7cb85f8f12b40f03cd015d404930c3c790, stripped

$ /usr/bin/gcc-5 --version
gcc-5 (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc helloworld.c -o helloworld

$ ./helloworld
Hello, world!

$ file helloworld
helloworld: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=3a0c7be5c6a8d45613e4ef2b7b3474df6224a5da, not stripped

And to prove it really was working, disable i386 support and try again:

1
2
3
4
5
$ sudo service binfmt-support stop
* Disabling additional executable binary formats binfmt-support [ OK ]

$ ./helloworld
-bash: ./helloworld: cannot execute binary file: Exec format error

windows terminal

  1. Window开发】设置windows terminal

  2. Windows Terminal 美化教程

  3. 主题预览 https://ohmyposh.dev/docs/themes

备份

1
2
3
4
5
6
7
8
9
10
wsl --shutdown
wsl --export Ubuntu-18.04 D:\Study\wsl\backup\ubuntu1820221221.tar

wsl --import Ubuntu-18.04 D:\Study\wsl\ D:\Study\wsl\backup\ubuntu1820221221.tar

###
diskpart
select vdisk file="C:\Users\PC-GWJ\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx"
compact vdisk
detach vdisk

Failed to install linux distro with error 0x8007023e #5508

https://github.com/microsoft/WSL/issues/5508

https://github.com/microsoft/WSL/issues/5508#issuecomment-743733155

I was investigating why this failed and messed up with my note taking as I fixed it without having to do all of this, but this does seem to work.

Try this. It’s more in-depth …

  • Make sure Virtualisation is enabled in BIOS.
  • Make sure the Windows Image and the OS are healthy. Follow these instructions for Windows 10: https://docs.microsoft.com/en-us/troubleshoot/windows-server/deployment/fix-windows-update-errors
  • Make a note/backup of ALL Hyper-V settings!
  • Remove all Virtual machine features. This being: Hyper-V, containers, Virtual Machine Platform, Windows Hypervisor Platform, Windows Sandbox and Windows Subsystem for Linux.
  • Reboot the computer!
  • Follow the manual WSL installation instructions provided by Microsoft as shown here: http://aka.ms/wslinstall
  • Reboot the computer!
  • Make sure that your Linux Distro converts to WSL2 if you want your Distro to operate under a full Linux Kernel.
  • There is no harm in invoking Windows Update at this point. I recommend you do.
  • Install the Windows Hypervisor Platform. (The likes of VMware and Virtual Box need it.)
  • No harm in rebooting the computer at this point.
  • No harm in invoking Windows Update at this point.
  • If you use it, install the Windows Sandbox feature and test it.
  • Finally install Hyper-V and put back all your required settings.

At this point everything, for me, started working.

I have noticed that on the completion of installing any of the virtualisation features, when invoking Windows Update then KB4592438 gets reapplied. If you wish, and maybe recommended, invoke Windows Update when you install a Windows Feature. That’s what I did. Installed a feature, rebooted and invoked Window Update. (I think the only one where I didn’t invoke Windows Update is when I installed the Windows Hypervisor Platform.)

作者

GWJ

发布于

2022-12-20

更新于

2023-12-22

许可协议

评论