[WSL2/Win10] Limit Resource Consumption of WSLv2

Windows Subsystem for Linux 2 has a big problem called "Vmmem" - it is the process WSL uses to allocate RAM for usage within its user context. That itself is all fine, however, there are many known issues that show that even after closing your e.g. Debian session, this memory is not returned to the system. This is also valid for usage of Docker for Windows within the WSLv2 context (meaning using Docker in the "new-style" install, not with the old HyperV machine).

The easy way to mitigate the pain (meaning RAM-being eaten up faster than a Chrome Engine spinning up ;)) is to just shutdown the WSL engine after usage, which you can do by opening up a Powershell or CMD with admin rights and enter

wsl --shutdown

This will shutdown WSL until you start it manually again. (Thanks smigel!)

A better way to work with this is to create a .wslconfig in your Windows User directory. Here is a short example.

[wsl2]
 memory=2GB # Limits VM memory in WSL 2 to 2 GB
 processors=2 # Makes the WSL 2 VM use two virtual processors
 swap=0 # Do not use a swap file

Anyway, the issue is still open and not yet fixed, even though a fix was promised on 17.06.2019. Worse, Microsoft locked the issue in July 2020 for "spam". Talking about bad customer experience...

[WSL2/Win10] Install WSLv2

1.) Open an administrative Powershell and activate the WSL feature

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

2.) Activate the Virtual Machine Platform, afterwards reboot your computer

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

3.) Download and install the WSLv2 Linux Kernel package from https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi - another Reboot is needed afterwards

4.) Set WSLv2 as default WSL - open an administrative Powershell and enter

wsl --set-default-version 2

5.) Install one or multiple operating systems in WSLv2 - you can find them here. You can also install these "offline", meaning without the need to use Windows Store. For the offline install, download the needed package from this link, then install it by doubleclicking on the Icon - or via Powershell with

Add-AppxPackage .\app_name.appx

You can then start and use it :).

Its also not a bad idea to install Windows Terminal.

[Win10] Download Enterprise ISO with Media Creation tool / check ISO for available editions

Download latest Enterprise ISO

To download the latest Windows 10 Enterprise ISO:

.\MediaCreationTool20H2.exe /Eula Accept /Retail /MediaArch x64 /MediaLangCode en-US /MediaEdition Enterprise
  • You will need to enter the license key / serial to be able to download the Enterprise style ISO (you will not be able to chose any type, this is done by the /MediaEdition switch, as well as the language etc..)

Check ISO for version information and available editions

  • mount ISO in Windows 10 (here mounted as D:)
  • execute this
dism /Get-WimInfo /WimFile:D:\sources\install.esd
  • you will get infos about what is in the ISO (in old versions, the install.esd was known as install.wim, then you need to launch that tool with install.wim instead of install.esd at the end)
Deployment Image Servicing and Management tool
Version: 10.0.18362.1316

Details for image : D:\sources\install.esd

Index : 1
Name : Windows 10 Education
Description : Windows 10 Education
Size : 15.736.130.486 bytes

Index : 2
Name : Windows 10 Education N
Description : Windows 10 Education N
Size : 14.956.748.370 bytes

Index : 3
Name : Windows 10 Enterprise
Description : Windows 10 Enterprise
Size : 15.736.284.481 bytes

Index : 4
Name : Windows 10 Enterprise N
Description : Windows 10 Enterprise N
Size : 14.956.654.647 bytes

Index : 5
Name : Windows 10 Pro
Description : Windows 10 Pro
Size : 15.734.489.825 bytes

Index : 6
Name : Windows 10 Pro N
Description : Windows 10 Pro N
Size : 14.959.031.814 bytes

The operation completed successfully.
  • If you start the tool a second time with a specific index, you can find out more about that "shard", e.g.to find out more about the Enterprise version, launch the tool with
dism /Get-WimInfo /WimFile:D:\sources\install.esd /index:3
  • you will get these infos
Deployment Image Servicing and Management tool
Version: 10.0.18362.1316

Details for image : D:\sources\install.esd

Index : 3
Name : Windows 10 Enterprise
Description : Windows 10 Enterprise
Size : 15.736.284.481 bytes
WIM Bootable : No
Architecture : x64
Hal : acpiapic
Version : 10.0.19041
ServicePack Build : 631
ServicePack Level : 0
Edition : Enterprise
Installation : Client
ProductType : WinNT
ProductSuite : Terminal Server
System Root : WINDOWS
Directories : 24132
Files : 96485
Created : 19/11/2020 - 04:20:58
Modified : 10/02/2021 - 14:13:25
Languages :
        en-US (Default)

The operation completed successfully.

[Win10] Offline installation of FoD like OpenSSH Server without VLSC ISO

Windows 10 comes with a lot of additional options to install, which are not part of the "local" installationmedia.
That means things like e.g. the OpenSSH Server component. These options are called "Features on Demand" (FoD) and do need either an active internet connection to download and install, or a specializied FoD ISO which you only can download from the Microsoft Volume Licensing Service Center (VLSC). If you need to install these options for your Windows 10 Professional or similar offline - without having a valid subscription, you cannot get the needed offline files that way.

However, there is another option by using a second computer with the same Major Release (e.g. Windows 10 1909 / 2004 / or 20H2 aka 2009) and possible latest patches installed to grab those files from the internet and then use these for offline installation.

Example, we want to get the OpenSSH Server package for Windows 10 20H2 / 2009 and install it offline afterwards.

  1. Get a Windows 10 20H2 computer connected to the internet, patched to latest version
  2. Download Everything ( https://www.voidtools.com/ - the portable version will do ) - this is a tool to scan for changes in the windows filesystem
  3. Start it, if it asks for admininistrative rights, say yes
  4. Enter "C:\Windows\" in the search path, so that only changes in the Windows path will be shown in Everything, so that you don't drown in information

5. Open an admininistrative Powershell - you can know search for the latest version of the FoD package you want to install, like
Get-WindowsCapability -Online |? Name -like 'OpenSSH.Server*'

This will get us info like:

Name  : OpenSSH.Server~~~~0.0.1.0
State : NotPresent
  1. Now, this is were it gets tricky. You need to start the download of the package, track it traversing through the filesystem with Everything and copy it before it gets deleted again by Windows after installation. You might need to repeat the process several times, but here it is in a full write up

6a. Enter
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
which will start the download & installation

6b. Watch the Everything window. At some point a cab file with OpenSSH will pop-up within the C:\Windows\SoftwareDistribution\Download folder. Select that file. And press CTRL+C to copy it.

6c. Move the focus of the mouse to your Desktop or other folder, at some point the file path of the selected file will change to C:\Windows\CbsTemp because it was done with downloading and will start to be installed. Your selection and copy will still be valid and now target this new file. Hence you need to hit CTRL+V to paste/copy it to your desktop fast, because it will disappear within seconds.

6d. If everything went ok, you will have the cab file with some MB size (in case of OpenSSH-Server-Package~31bf3856ad364e35~amd64~~.cab its just ~1.22 MB) and thats what we need. If you were to slow, you can try again by uninstalling the OpenSSH Server via powershell using
Remove-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
then start at 6a and repeat until it works

6e. One word of caution - the filename will stay the same for all Windows 10 Release Versions. So it would be wise to put it into a folder with the version name - so that you don't mix up differnt versions. Also don't change the filename of the cab file, otherwise it cannot be used for installation anymore.

  1. Move the file to your target / isolated system.
  2. Powershell with Admin Rights to the correct folder with e.g. OpenSSH-Server-Package~31bf3856ad364e35~amd64~~.cab in it, then execute the installation command:
    Add-WindowsCapability -Online -Name "OpenSSH.Server~~~~0.0.1.0" -Source "." -LimitAccess

It is going to be installed. But as additional stuff…

// Set Services to Autostart
Set-Service sshd -StartupType Automatic
Set-Service ssh-agent -StartupType Automatic
// Check if Firewall Rule is activated
Get-NetFirewallRule -Name *ssh*
// There should be a firewall rule named "OpenSSH-Server-In-TCP", which should be enabled
// If the firewall does not exist, create one
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
// Configuration of OpenSSH Server
// You can set Powershell as default shell
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force
// Start Service 
Start-Service sshd
Start-Service ssh-agent

[WSL2/Win10] virt-manager for kvm on Windows

kvm Virtualisiation is great, however, useable tools to create and manage said VMs are rare. The best tool for the job, virt-manager is only available for Linux machines. But what if you want to manage said VMs also via Windows 10? WSL2 to the rescue: Just install WSL2 as shown by the excellent Microsoft Guide, install i.e. a Debian/GNU Linux instance and then launch into it.

You should update the instance to the latest version first:

sudo apt update && sudo apt upgrade -y && sudo apt dist-upgrade -y

then you can install virt-manager

sudo apt install -y virt-manager ssh-askpass

the last thing you would need is to install an X server on your windows machine, i.e. Xming or MobaXterm (which contains Xming) and launch it. Then you need to setup the X forwarding in your WSL2 instance, by entering

export DISPLAY="grep nameserver /etc/resolv.conf | sed 's/nameserver //':0"
export DISPLAY="sed -n 's/nameserver //p' /etc/resolv.conf:0"
export DISPLAY=$(ip route|awk '/^default/{print $3}'):0.0

after that, you can launch virt-manager by entering

virt-manager

and configure it to connect to your KVM instance via SSH.

[Win10] Random ports blocked while using Docker / WSL / HyperV

I have been using Windows Subsystem for Linux (WSL) and Docker on my Laptop since a long time. And during last Docker Con, WSL 2 was released to which I switched instantly - which I did not regret.

(Note: Upgrading to WSL 2 and the native Docker for WSL 2 version will cost you your containers and Docker images, there is even a Thanos meme coming around - so I have to give this fair warning ;))

However: Said Laptop started acting strange as suddenly local MariaDB instances or Apache2 did cease to work and even some nodeJS projects on port 9000. All these ports were not taken directly by any application, but somehow it did not work anymore. It turns out that a faulty HyperV update led to the hypervisor reserving too many ports across the board.

Luckily there is a solution to correct this issue as shown here by Christopher Pietrzykowski.

To make it easy and fast: Open up a powershell or cmd prompt as admin user and enter

netsh int ipv4 show dynamicport tcp
netsh int ipv6 show dynamicport tcp

If it comes up with startport 1025 and a huge number of reserved ports, you are experiencing the same problem. Please enter these commands to realign the startport to 49152 for both IPv4 and IPv6

netsh int ipv4 set dynamic tcp start=49152 num=16384
netsh int ipv6 set dynamic tcp start=49152 num=16384

after a reboot, everything should be fixed again 🙂

Windows 10 Change Wifi / WLAN Network Priority

1.) Open cmd.exe
2.) netsh wlan show profiles
The wifi profile names will be shown in the current priority order under the section "User profiles". Its important to note, that the section starts with "Profiles on interface INTERFACE", where INTERFACE is the interfacename. If you got multiple wifi interfaces, that section will reoccur with its own "User profiles" and priority list.
3.) To change the priority of one network, enter:
netsh wlan set profileorder name="WIFIPROFILE" interface="I" priority=NUMBER
You need to give the correct wifi name as WIFIPROFILE, the interface which uses said profile as I and the NUMBER for the priority, with 1 being the highest, i.e. should be used prefered. You can also define priorities lesser than 1.

Source

How to delete Hyper-V Virtual Networkcards in Windows 10

As a "Edge" Version user of Docker for Windows, I tend to reinstall Docker from time to time, just to get a clean base. However, Hyper-V digs quite deep into the system and creates its own virtual network interfaces, which you just can't delete from the Hyper-V Manager.

Best option is to use (READ ALL BEFORE COPY AND PASTE, PLEASE!):

netcfg -d

in a Administrator CMD window.

This does only delete virtual network devices - HOWEVER, it does delete devices like i.e. your OpenVPN interface AS WELL - AND does DELETE ALL SETTINGS of other network interfaces, i.e. your IP configuration or all your saved WIFI credentials.

So use with care!

Taken from: https://serverfault.com/questions/584316/how-to-delete-hyper-v-virtual-switch-extensions-adapters-from-device-manager

Upgrade WSL (Windows Subsystem for Linux) on Windows 10

I had installed WSL (Windows Subsystem for Linux) a long time ago to gain access to Ubuntu 14.04 LTS directly from my Windows 10 Desktop. However, as time passes, Software grows old. Upgrading the Ubuntu Subsystem via apt-get update / do-release-upgrade should work, but that could have some nasty sideeffects, considering that the 14.04 LTS WSL release had been a beta test - so, a reinstall should be better.

Luckily, TechRepublic got this covered. Just open a CMD and run:

lxrun /uninstall /full /y

to uninstall the current WSL version.

Afterwards, try

lxrun /install

to reinstall it. With this "reinstall", Ubuntu 16.04 LTS will be installed.

Nonetheless, I recommend a nice

sudo apt-get update
sudo apt-get dist-upgrade

afterwards in your BASH session to get the WSL to the latest version ;).

 

Solve Windows 10 does not automount USB Drives anymore

My Windows 10 machine started behaving weirdly and did not mount any USB Drives / Harddrives and such anymore. I had to open the Disk Management Tool and mount them manually, which is quite inconvenient. So I was looking up the error and came across the nearly perfect solution at http://woshub.com/windows-doesnt-assign-letters-to-external-and-usb-flash-drives/ . However, I did change it a bit so that the VDS is changed to automatically start on boot via the CLI :).

Oh, and yes, you need to do this from an admin cmd 😉

First thing to check if something is wrong, is if your VDS (Virtual Disk Service) works. If not, set it to start on boot and start the service:

sc query vds
sc config vds start= auto
sc start vds
sc query vds

If automounting still not works, it could be deactivated. Start diskpart, watch the status of automount and if it is disabled, activate it.

diskpart
DISKPART> automount
DISKPART> automount enable
DISKPART> exit

Now everything should be working fine again 🙂