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.

Install Docker inside WSL

If you don’t want to 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 *