Skip to content

Instantly share code, notes, and snippets.

@vflopes
Last active March 4, 2026 12:05
Show Gist options
  • Select an option

  • Save vflopes/50fd0d09da995904fefd3e7b3bb4bfb6 to your computer and use it in GitHub Desktop.

Select an option

Save vflopes/50fd0d09da995904fefd3e7b3bb4bfb6 to your computer and use it in GitHub Desktop.
Setup WSL2 distro

Setup WSL2 distro (step-by-step)

1. Open a PowerShell terminal and check WSL status

wsl --status

If needed, set default version to 2:

wsl --set-default-version 2

2. Create the distro directory

mkdir "C:\WSL\my-linux"

3. Download a rootfs

  1. For Ubuntu you can find available base images here
  2. For Debian download the nocloud version here

⚠️ Debian image comes in .raw format, use a tool like 7zip to extract the rootfs from the compressed and create the appropriate tar file.

4. Import the distro

wsl --import my-linux "C:\WSL\my-linux" "C:\WSL\ubuntu-base-25.10-base-amd64.tar.gz" --version 2

5. Start your instance

wsl -d my-linux

Optional steps

Change the default user (as root or sudo)

export USERNAME=myuser

useradd ${USERNAME}

mkdir /home/${USERNAME}
cp -a /etc/skel/. /home/${USERNAME}/
chown -R ${USERNAME}:${USERNAME} /home/${USERNAME}/
chmod 755 /home/${USERNAME}/

apt update && apt upgrade -y
apt install sudo -y

usermod -aG sudo ${USERNAME}
echo -e "[user]\ndefault=${USERNAME}" | tee /etc/wsl.conf

passwd ${USERNAME}

chsh -s /bin/bash ${USERNAME}

Change the hostname (as root or sudo)

apt install nano -y

# Set the hostname in the hosts file
nano /etc/hosts
# Set the hostname without restarting
hostname myhostname

Edit the /etc/wsl.conf

[network]
hostname=myhostname

WSL2 Multi-Distro Systemd Network Fix

When running multiple WSL2 distributions with systemd=true and networkingMode=mirrored, the network often fails (Network is unreachable) because each instance's systemd-networkd and systemd-resolved compete for the shared Windows network stack.

1. Host Configuration (.wslconfig)

Ensure your Windows host configuration is optimized in %USERPROFILE%\.wslconfig:

[wsl2]
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true

2. Linux Instance Configuration (/etc/wsl.conf)

Set up each distro to allow WSL to manage the DNS while enabling systemd:

[boot]
systemd=true

[network]
generateResolvConf=true

3. Fix Network Conflicts (Inside each Distro)

Run these commands to prevent systemd from hijacking the mirrored interface and breaking the routing table:

# 1. Prevent systemd-networkd from managing interfaces
sudo systemctl disable --now systemd-networkd
sudo systemctl disable --now systemd-networkd.socket
sudo systemctl mask systemd-networkd

# 2. Prevent systemd-resolved from conflicting with WSL DNS Tunneling
sudo systemctl disable --now systemd-resolved
sudo systemctl mask systemd-resolved

# 3. Clean up resolv.conf to use WSL's auto-generated version
sudo rm /etc/resolv.conf
# Restart WSL after this (wsl --shutdown)

4. Troubleshooting

ip route
# If 'default' route is missing, ensure no other service is managing eth0

ps 1
# Should output /sbin/init to ensure systemd is running
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment