Skip to content

WSL2#

Mount Ext4 Disks Into WSL2#

  • Open the PowerShell as Administrator
  • Run the command below to list all the disks.
1
GET-CimInstance -query "SELECT * from Win32_DiskDrive"
  • Then we can see the result as below.
DeviceID           Caption              Partitions Size          Model
--------           -------              ---------- ----          -----
\\.\PHYSICALDRIVE0 WDC WD40EFPX-68C6CN0 0          4000784417280 WDC WD40EFPX-68C6CN0
\\.\PHYSICALDRIVE3 KINGSTON SNV2S500G   1          500105249280  KINGSTON SNV2S500G
\\.\PHYSICALDRIVE1 WDC WD40EFPX-68C6CN0 0          4000784417280 WDC WD40EFPX-68C6CN0
\\.\PHYSICALDRIVE2 KINGSTON SNV2S500G   3          500105249280  KINGSTON SNV2S500G
  • Then use the DeviceID with the command below to mount the Disk into the WSL2
1
wsl --mount \\.\PHYSICALDRIVE0
  • The open the WSL and run the command below to check the Disk is mounted successfully or not.
1
lsblk
  • Then we should see the result.
1
2
3
4
5
6
7
8
9
NAME
    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda   8:0    0 388.4M  1 disk
sdb   8:16   0   186M  1 disk
sdc   8:32   0     8G  0 disk [SWAP]
sdd   8:48   0     1T  0 disk /mnt/wslg/distro
                              /
sde   8:64   0   3.6T  0 disk
sdf   8:80   0   3.6T  0 disk /mnt/wsl/PHYSICALDRIVE0

WSL Config Files#

  • There are 2 types of WSL2 configuration files. The WSL2 global config file which is located in the Windows host system and the WSL2 local config file which is contained in the WSL2 machine.
  • The WSL2 global config file will have the name as .wslconfig and it is located at C:\Users\<your-user>. By default it is not created automatically by the Windows system. So if you want to configure something, you need to create the .wslconfig file manually following the path above.
  • The global WSL2 config file will allow you to configures global settings that affect all distributions (all WSL2 machines), such as memory limits, CPU count, swap size, etc.
  • For example:
1
2
3
4
5
[wsl2]
memory=4GB
processors=2
swap=2GB
localhostForwarding=true
  • After configuring the .wslconfig then you should stop the WSL2 machines and wait for 8 seconds to make sure all instances have been stopped completely (8 seconds rule). Then start your WSL2 machines again then you can see all distributions has been applied with your configurations.
  • The WSL2 local config file will have the name as wsl.conf and it is located at /etc/wsl.conf. By default this is created automatically inside the WSL2 machine and it will be read everytime at WSL2 startup. This WSL2 local config file is used for controlling per-distro behavior such as mount options, user login defauts or windows drives are mounted.
  • For example:
1
2
3
[network]
hostname=duc-server
generateHosts=false
  • After configuring the wsl.conf then you should stop the WSL2 machine and wait for 8 seconds to make sure the instance have been stopped completely (8 seconds rule). Then start your WSL2 machine again then you can see your specific distribution has been applied with your configurations.

Change WSL Hostname#

  • Why do we need to change the WSL Hostname?

    • By default the hostname of the WSL machine will be the same with the hostname of the windows host machine. So if you use VPN such as tailscale for your windows host machine and WSL also then you may get issues with DNS which can make you get errors when you use browsers on Windows host machine.
  • For WSL machine you can't use normal way to set the hostname such as:

sudo hostnamectl set-hostname <your-host-name>
  • Because your hostname will be reset to default after you restart your WSL machine. So you need following this way to set your hostname permanently.
  • Firstly, let's open the wsl.conf file for editing.
1
sudo gedit /etc/wsl.conf
  • Then add these lines below to the end of the file.
1
2
3
[network]
hostname=duc-server
generateHosts=false
  • Next let's open the /etc/hosts file.
1
sudo gedit /etc/hosts
  • Then replace texts which contains your old hostname to your new hostname and save. For example:
  • The old domain is look like below.
1
2
127.0.0.1       localhost
127.0.1.1       test-server.localdomain  test-server
  • Then the new domain will be like this.
1
2
127.0.0.1       localhost
127.0.1.1       new-server.localdomain  new-server
  • Finally restart your WSL machine and then you will see your WSL hostname has changed