Try and Use WSL
TyeYeah Lv4

After a medium period of working & studying on Deepin Linux, I experienced the force of deepin technology community including well technology supports, and commercial softwares (cracked) provided, but there are still things I have to do on Windows only, like Office and my poor amd graphics card’s outdated driver, which cannot be installed on new Linux distributions.

By the way I want to start studying Windows OS, so initially planned to experience WSL, Hyper-v, Windows sandbox, Virtual Machine Platform, IIS and other Windows featured functions in order. It’s proved to be a hard long way.

Windows Subsystem for Linux

So why WSL?
Cygwin, Gow, MSYS2, Git bash and other UNIX-like terminals are suitable for users with Linux cmd habits, but on WSL, you can almost do whatever Linux developers can do. It obeys your Linux developing custom, allows you to run Linux softwares (ELF files), and boots up really fast and so on. But only supported on Win10.
Here is its Architecture

pic 1 Architecture

If you want to know more such as how to install, basic usages, and other stuff, please click Official documents

WSL

WSL is a collection of components that enables native Linux ELF64 binaries to run on Windows. It contains both user mode and kernel mode components. It is primarily comprised of:

  • User mode session manager service that handles the Linux instance life cycle
  • Pico provider drivers (lxss.sys, lxcore.sys) that emulate a Linux kernel by translating Linux syscalls
  • Pico processes that host the unmodified user mode Linux (e.g. /bin/bash)

It is the space between the user mode Linux binaries and the Windows kernel components where the magic happens. By placing unmodified Linux binaries in Pico processes we enable Linux system calls to be directed into the Windows kernel. The lxss.sys and lxcore.sys drivers translate the Linux system calls into NT APIs and emulate the Linux kernel.

pic 2 Architecture

Now we can see the subsystem does not contain a real Linux Kernel, but it can satisfied lots of users.

Pros:

  • fast bootup, much faster than VirtualMachine in VM and Vbox
  • almost perfect Linux Architecture simulation, and ELF support which means possible and convenient for running & debugging ELF files when using Windows
  • wsl’s linux file system can be directly browsed and accessed on Windows, though it’s not recommended, may be harmful. All files in rootfs directory :)
  • same network configuration as host Windows, use 127.0.0.1 to access files on wsl server

Cons:

  • no raw_socket using, which leads to many network-softwares (using socket) invalid
  • some functions like systemd are also invalid

Outlook:from official
What will happen to WSL 1? Will it be abandoned?
We currently have no plans to deprecate WSL 1. You can run WSL 1 and WSL 2 distros side by side, and can upgrade and downgrade any distro at any time. Adding WSL 2 as a new architecture presents a better platform for the WSL team to deliver features that make WSL an amazing way to run a Linux environment in Windows.

WSL2

WSL 2 is a new version of the architecture that powers the Windows Subsystem for Linux to run ELF64 Linux binaries on Windows. This new architecture changes how these Linux binaries interact with Windows and your computer’s hardware, but still provides the same user experience as in WSL 1 (the current widely available version). Individual Linux distros can be run either as a WSL 1 distro, or as a WSL 2 distro, can be upgraded or downgraded at any time, and you can run WSL 1 and WSL 2 distros side by side. WSL 2 uses an entirely new architecture that uses a real Linux kernel.

Announcing WSL 2

WSL 2 is now available in Windows Insiders

If you want to know more such as how to install, basic usages, and other stuff, please click Official documents

WSL2 requires Insider Preview. And this time, after reading all above, we know WSL2 is really based on Hyper-v.

Pros:

  • A more realistic Linux with systemd now and raw_socket allowed (maybe all this thanks to Hyper-v)
  • High levels of integration between Windows and Linux, extremely fast boot times, small resource footprint, and best of all will require no VM configuration or management

Cons:

  • Hyper-v means no other VMs except VirtualBox for 32 bits vm images as is said (test 64 bits vm images and NO)
  • NAT network mode cannot be changed now, which means cut the network setting part of this “faster Hyper-v”, and somehow it becomes bit of inconvenient
  • no direct browse or access to linux file system, because original roofs dir is replaced by the vm format file : ext4.vhdx. Still can use explorer.exe to open it

Outlook:
The networking applications part and hardware support will be improved. Let’s look forward to it.
But WSL seems to be mor useful than WSL2

Usages

The whole docs are on Windows Subsystem for Linux Documentation.

First visit Windows Subsystem for Linux Installation Guide for Windows 10 to know how to install

Refer to Command Reference for Windows Subsystem for Linux for WSL commands.

Here let me review. Check the WSL version assigned to each of the Linux distributions installed

1
> wsl --list --verbose # wsl -l -v

Set WSL 2/1 as default, so the next distro will be version 2/1 defaultly

1
> wsl --set-default-version 2/1

To set a distribution to be backed by either version of WSL

1
2
> wsl --set-version <distribution name> <versionNumber>
> wsl --set-version ubuntu 2

Enter wsl/bash to use default distro, enter debian/ubuntu/… to use other distros

1
2
3
> wsl
> bash
> debian

Learn more by showing help pages

1
2
> wsl --help # wsl -h
> wslconfig # wslconfig

Hyper-V

It you are a multi-platform dev, or a normal user of vm, instead of an advanced windows developer, of a virtualization architect, you don’t need Hyper-v at all. Because without VBoxGuestAdditions or VMwareTools, VM in Hyper-v is like ppt playing.

Actually No.1 rival/competitor of Hyper-v is VMware vSphere, and apparently in our using level, we can’t experience full potential of vitualizition tech, but in the future, maybe we will.

So my answer is NO, and be careful to try it cz after installing and uninstalling, VMs sometimes still disabled.

Uninstall Solutions:

  1. When canceled the check mark, sometimes VMs not work yet

  2. Then open cmd as Administrator and input bcdedit /set hypervisorlaunchtype off and then check VMs

  3. The ultimate solution is this script in cmd

1
2
3
4
5
6
7
8
9
mountvol X: /s
copy %WINDIR%\System32\SecConfig.efi X:\EFI\Microsoft\Boot\SecConfig.efi /Y
bcdedit /create {0cb3b571-2f2e-4343-a879-d86a476d7215} /d "DebugTool" /application osloader
bcdedit /set {0cb3b571-2f2e-4343-a879-d86a476d7215} path "\EFI\Microsoft\Boot\SecConfig.efi"
bcdedit /set {bootmgr} bootsequence {0cb3b571-2f2e-4343-a879-d86a476d7215}
bcdedit /set {0cb3b571-2f2e-4343-a879-d86a476d7215} loadoptions DISABLE-LSA-ISO,DISABLE-VBS
bcdedit /set {0cb3b571-2f2e-4343-a879-d86a476d7215} device partition=X:
bcdedit /set hypervisorlaunchtype off
mountvol X: /d

After this you will find another EFI boot selection added. Actually both new and the old one is definitely ok to run VMs, so feel free to use EasyBCD to delete extra EFI boot selection.

Windows Sandbox

A similar hyper-v vm, but with slower bootup and an inactivated system (seriously???) and few apps in it.

So why not a hyper-v vm with snapshot? Why not vmware or vbox with snapshot?

Summary

The conclusion is : Try and even deeply use WSL 1 to develop but don’t use or even try WSL 2 / Hyper-v
But your choice always depends on your own needs.

Consult official documents and devblog is always the right way to know and learn.
For developers click here, and Windows API references here.

Update (Tips)

Now it’s 2021/9/6 and I have changed my mind already.

I’m getting used to WSL 2 due to convenience. It is a compact hyper-v vm and I can boot it up in several seconds. Though much slower (booting and file transfering betweene host and guest) than WSL 1 but more powerful, it’s a relatively real linux vm.

Here is just for some Tips.

Docker

Since Linux has docker in repo, we can install it directly in WSL 2 instead of installing Docker Desktop for windows.

But when using kali(debian) some error occurred. Install it by

1
2
$ sudo apt install docker.io
$ sudo apt install daemon # daemon is needed

While when testing

1
2
3
4
5
6
7
8
9
$ dockerd   # testing service
[INFO] ...
...
[WARN] ...
...
[INFO] ...

failed to start daemon: Error initializing network controller: error obtaining controller instance: unable to add return rule in DOCKER-ISOLATION-STAGE-1 chain: (iptables failed: iptables --wait -A DOCKER-ISOLATION-STAGE-1 -j RETURN: iptables v1.8.7 (nf_tables): RULE_APPEND failed (No such file or directory): rule in chain DOCKER-ISOLATION-STAGE-1
(exit status 4))

That is because debian based systems use nftables instead of iptables, that’s why the command failed.

To solve it, use update-alternatives to let debian use iptables rather than nftables.

1
2
3
$ sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
# for ipv6
$ sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy

Then we can start service

1
2
$ service docker start
$ service docker status

The ubuntu on WSL 2 doesn’t have this problem after testing, but lacks docker service installed.

kali-win-kex

As I met it so many times, it’s 2023/11/14 and I have to take a note for fixing problems with it…

WSL 2 supports WSLg, which provides X Window service, the kali-win-kex provides a good way to connect kali-linux desktop conveniently, essentially an encrypted VNC method.

To install and use it:

1
2
3
4
5
$ sudo apt install -y kali-win-kex  # optional: sudo apt install kali-linux-large
$ sudo apt-get install x11-xserver-utils dconf-editor dbus-x11 -y
$ cd ~
$ kex

When you get errors like:

1
2
3
4
5
6
7
8
Starting Win-KeX server (Win)
Win-KeX server (Win) is stopped

Error connecting to the Win-KeX server (Win)
Please try "/usr/bin/kex start" to start the service
If the server fails to start, please try "/usr/bin/kex kill" or restart your WSL 2 session and try again

Starting Win-KeX client (Win)

Try commands:

1
2
3
4
5
6
7
8
$ sudo apt remove -y kali-win-kex && sudo apt install -y kali-win-kex
# or
$ sudo mount -o remount,rw /tmp/.X11-unix
# or delete all .XX-lock files you can find (just do rm .X1-lock, rm .X2-lock ...)
$ rm /tmp/.X1-lock /tmp/.X2-lock
# or delete related socket files
$ rm -r /tmp/.X11-unix
# or ... google it
Powered by Hexo & Theme Keep
Total words 135.7k