Windows Subsystem for Linux (WSL) - eodeluga/dev-notes GitHub Wiki

Add "allow" rule to Windows firewall for WSL2 network

Open a Windows PowerShell prompt

New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow

Open external network access to WSL app

Proxying ports is useful when a process binds on one (maybe only the local) interface and you want to expose that endpoint on another network interface.

The portproxy server listens for messages sent to a specific port and IPv4 address. It maps a port and IPv4 address to send messages received after establishing a separate TCP connection.

Using netsh

The following command allows any IPv4 address to connect to the local host using port 5432

netsh interface portproxy add v4tov4 listenport=5432 listenaddress=0.0.0.0 connectport=5432 connectaddress=127.0.0.1

Install support for Linux GUI apps

Prerequisites

  • You will need to be on Windows 11 Build 22000 or later to access this feature.
  • Installed driver for vGPU

To run Linux GUI apps, you should first install the driver matching your system below. This will enable you to use a virtual GPU (vGPU) so you can benefit from hardware accelerated OpenGL rendering.

Intel GPU driver for WSL

AMD GPU driver for WSL

NVIDIA GPU driver for WSL

Fresh install - No prior WSL installation

You can now install everything you need to run Windows Subsystem for Linux (WSL) by entering this command in an administrator PowerShell or Windows Command Prompt and then restarting your machine.

wsl --install

Once your machine has finished rebooting, installation will continue and you will be asked to enter a username and password. This will be your Linux credential for the Ubuntu distribution.

You're now ready to begin using Linux GUI apps on WSL!

For more info check install WSL.

Existing WSL install

If you already have WSL installed on your machine, you can update to the latest version that includes Linux GUI support by running the update command from an elevated command prompt.

Select Start, type PowerShell, right-click Windows PowerShell, and then select Run as administrator.

Enter the WSL update command:

wsl --update

You will need to restart WSL for the update to take effect. You can restart WSL by running the shutdown command in PowerShell.

wsl --shutdown

Backing up a WSL instance

  • Get the installed distro

wsl --list

  • Backup the instance (make sure to specify filename as a .tar)

wsl --export Ubuntu-20.04 .\wsl_backup.tar

Note: You can also backup to a disk image which appears to be faster

wsl --export Ubuntu-20.04 --vhd .\ubuntu-20.04.vhdx

Restoring / importing a distro

  • Restoring a backup distro

wsl --import ubuntu-20.04 .\ubuntu-20.04-distro-folder .\wsl_backup.tar

If you used the --vhd disk image export option, sometimes it makes sense to import from and set the location of the distro to where the vhdx file is located, using the --import-in-place option.

  • Import a disk image in place

wsl --import-in-place Ubuntu-20.04 .\ubuntu-20.04.vhdx

Setting a default user after importing a distro

Even if from a previously backed up distro with a default user set, after import the default user will be root

The current Microsoft recommended way of setting the username in an instance is to create a /etc/wsl.conf in the instance with the following setting:

  • Edit the WSL config file

nano /etc/wsl.conf

[user]
default=username

Changing, of course, username to be your default username.

Exit your distro/instance, then issue a wsl --terminate <distroname> from PowerShell or CMD. When you restart, the default user should be set.

Fixing no Internet issue

This is normally because DNS gets messed up

  • Backup the /etc/resolv.conf file

    sudo mv /etc/resolv.conf /etc/resolv.conf.bak

  • Delete the existing file as it's a symlink and a real file is needed

    sudo rm -f /etc/resolv.conf

  • Create a new file

    sudo touch /etc/resolv.conf

  • Open the file and add the following:

    sudo nano /etc/resolv.conf

  • Get your DNS server IP address

    ip route list

    Note: Copy the IP address line that starts default via... **

  • Add the following lines and save the file:

    [network]
    generateResolvConf = false
    nameserver <IP address WSL, your router IP address or 8.8.8.8>
    
  • Set the file to read only otherwise WSL will remove your changes at boot

    sudo chattr +i /etc/resolv.conf

External guides

⚠️ **GitHub.com Fallback** ⚠️