Syed Umar AnisLinuxTips and Tricks: Working with WSL (Windows Subsystem for Linux)
Syed Umar AnisLinuxTips and Tricks: Working with WSL (Windows Subsystem for Linux)
Linux

Tips and Tricks: Working with WSL (Windows Subsystem for Linux)

Browsing WSL files from windows

Open the following path in Windows Explorer.

\\wsl$\

Open a WSL directory in Explorer

Open the current WSL directory in Windows Explorer

explorer.exe .

Mounting additional drives

C: Drive is mounted automatically by WSL. To mount additional drives, e.g D:, run the following commands:

sudo mkdir /mnt/d
sudo mount -t drvfs D: /mnt/d

Using sudo

Using sudo doesn’t work when logging into WSL using PowerShell. We have to start a new WSL session with admin rights:

wsl --user root

Note: If you launch a Ubuntu terminal, sudo does work there.

Docker inside WSL

Tip: Run commands like docker compose up -d from inside of Linux CLI instead of Windows to avoid strange errors with mounting file paths.

For instance, if you are using Docker Desktop, start the Docker engine from the Docker Desktop app but execute docker compose from inside the WSL Linux terminal.

SSH into Linux

Check if SSH service is running inside the guest Linux

systemctl status sshd

Install / Reinstall SSH service

sudo apt remove openssh-server
sudo apt install openssh-server

Restart the SSH service

sudo service ssh restart

Configure SSH

Open SSH file

sudo vi /etc/ssh/sshd_config

Uncomment the following lines

#Port 22
#PasswordAuthentication yes

Enable remote access into WSL from LAN

Find the WSL Linux IP address

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

Set up the port forwarding on Windows. Let’s assume we want to connect on the port 9080 and linux IP returned by the above command is 172.21.144.225

netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=9080 connectaddress=172.21.144.225 connectport=9080

Open the port in the Windows firewall by creating an Inbound Rule in Windows Defender Firewall with Advanced Security.

Troubleshooting

  • If this doesn’t work, try the mirrored networking mode explained below.

Turn on Mirrored Networking mode

Mirrored networking mode alleviates the pain of working with two network interfaces: Host Windows and Guest Linux. It automatically mirrors the host interface to the guest. To turn it on:

1- Create a file called .wslconfig in the user directory (C:\Users\<username>\) with the following contents:

[wsl2]
networkingMode=mirrored

This can also be set in the new WSL settings application.

2- Remove all entries created using netstat interface portproxy add command. For instance,

netsh interface portproxy delete v4tov4 listenport=9080 listenaddress=0.0.0.0

3- Allow traffic from private to WSL by executing the PowerShell command in elevated mode.

New-NetFirewallHyperVRule `
-DisplayName 'Allow All Inbound Traffic to WSL in Private Network' `
-Name 'WSL Private Inbound Rule' `
-Profiles Private `
-Direction Inbound `
-Action Allow `
-VMCreatorId ((Get-NetFirewallHyperVVMCreator).VMCreatorId) `
-Enabled True

If you need to later remove this Firewall rule, run the following:

Remove-NetFirewallHyperVRule -Name 'WSL Private Inbound Rule'

Install Docker inside WSL

If you don’t want to use Docker Desktop for Windows (maybe due to the licensing), you can manually install the docker community edition inside WSL Linux.

# refresh and install packages
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common libssl-dev libffi-dev git wget nano

# add user group
sudo groupadd docker
sudo usermod -aG docker ${USER}

# add docker key and repo
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update

# install docker and docker-compose
sudo apt-get install -y docker-ce containerd.io docker-compose

# install docker-compose (if the previous command failed to install)
sudo curl -sSL https://github.com/docker/compose/releases/download/`curl -s https://github.com/docker/compose/tags | grep "compose/releases/tag" | sed -r 's|.*([0-9]+\.[0-9]+\.[0-9]+).*|\1|p' | head -n 1`/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose && sudo chmod +x /usr/local/bin/docker-compose
Hi, I’m Umar

Leave a Reply

Your email address will not be published. Required fields are marked *