Getting Started with WSL 2 Part 2
Tags: linux, ubuntu, wsl2, windows
Published: 2020-07-12 18:59:28

If you have read my first post and install WSL2 successfully and you  are like me having a laptop with only 4 Gigs of RAM or using a very slow machine you will start immediately to notice that there is a process goes with that name vmmem on Task Manager that eats a lot of RAM this process is a very small virtual machine made by windows to run Linux and use the Linux Kernel it consumes less RAM than a regular Virtual Machyine but still may be a problem for some.

I have read some issue on Github that some users working with docker consume up to 30GB of RAM which is very ridiculous.

![vmmem RAM consumption](https://camo.githubusercontent.com/72c3d663ddd6f2b314898e80176323c76ecaf767/68747470733a2f2f692e696d6775722e636f6d2f725470453872532e706e67)

for low-end PC it doesn't mean it will break your PC but this process will happily consumes all available RAM leaving the host OS (windows) paralyzed luckily the team at Microsoft already provide a fix for this problem.

## Table of content
* Getting familiar with `wsl` command on PowerShell.
* Setting up a config file for WSL.
* Gift!.
*Conclusion.

*PS : if you want to just solve the RAM issue jump to setting a config file for WSL*.

## Getting familiar with `wsl` command on PowerShell.

To check how many distros you have installed or see what distros are currently running the Microsoft Team have provide a very useful utility that can provide with helpful information and can do a bunch of actions all the commands will be typed in PowerShell.

For the start i recommend you to check `wsl -h` command and see the full documentation of this tool and fear not it's not very long.

### Basic Commands
* `wsl` will start the default Linux distro for you.
* `wsl -l` will list all available distros.
* `wsl -l -v` will list all distros along with their state and version. *NOTE* version 2 is the one that comes with Linux Kernel.
* `wsl distro-name --set-version 2` will change the disto to version 2 or use 1 for older version.
* `wsl distro-name --set-default` will make the distro a default one.
* `wsl --shutdown` will terminal all distro and shutdown the virtual machine *vmmem process in task manager*.

## Setting up a config file for WSL.
It is possible to set a global setting for WSL2 in file that should be exist in user folder ex: `C:/Users/Adam`, this file allows us to define the max RAM WSL2 should consume.

the file name is .wslconfig and comes in this form
```
[wsl2]
kernel=              # An absolute Windows path to a custom Linux kernel.
memory=              # How much memory to assign to the WSL2 VM.
processors=        # How many processors to assign to the WSL2 VM.
swap=                # How much swap space to add to the WSL2 VM. 0 for no swap file.
swapFile=            # An absolute Windows path to the swap vhd.
localhostForwarding= # Boolean specifying if ports bound to wildcard or localhost in the WSL2 VM should be connectable from the host via localhost:port (default true).

#  entries must be absolute Windows paths with escaped backslashes, for example C:\\Users\\Adam\\kernel
#  entries must be size followed by unit, for example 8GB or 512MB
```

1. Open notepad.
2. copy and paste this code.
3. Save the file with the name .wslconfig.
4. Go to Powershell and type `wsl --shutdown` to close the vmmem and start it with `wsl`.

```
[wsl2]
memory=512MB
swap=4GB
localhostForwading=true
```
the next time you use WSL2 it won't consume more than what you have defined for it to consume.

## Gift!.

You may have experiencing an issue with hard drive if it full then you can easily move your WSL distro to another hard disk, by exporting and importing it back into a new folder as another WSL distro.

### The process.
* First open PowerShell.
* type `wsl -l -v` to list all distro names.
* export the distro wiht this command `wsl --export distro-name location-of-distro.tar` example `wsl --export kali-linux C:/temp/kali.tar`.
* Take a coffe break !.
* Then import the distro into another hard disk `wsl --import distro-new-name distro-new-destination tar-file-location` example `wsl --import kali-linux-2 F:/wsl C:/temp/kali.tar`.
* Take coffe break !.

after that open Powershell and type `wsl -l -v` you will find a new distro with new name *kali-linux-2* that resides in the new location, you will end up with two kali linux distro all you have to do is `wsl distro-name --unregister` ex `wsl kali-linux --unregister`.

In the end the new imported distro will only start with root not your previous user-account the fix for this problem is on of these three options:

* Launching using PowerShell command `wsl -d kali-linux-2 -u kaliuser`.
* Windows Terminal go to setting and add this snip code to your distro profile at the end `"commandline" : "wsl.exe -d kali-linux-2 -u kaliuser"`.
* Change the default user at start with Powershell using this code `<DistributionName> config --default-user <Username>` like `kali-linux --default-user omar`.