🐧 How to install Linux on Windows with WSL

The Windows Subsystem for Linux (WSL) lets developers install a Linux distribution (such as Ubuntu, OpenSUSE, Kali, Debian, Arch Linux, etc) and use Linux applications, utilities, and Bash command-line tools directly on Windows, unmodified, without the overhead of a traditional virtual machine or dualboot setup.


Install WSL Command

Open PowerShell or Windows Command, run

wsl --install

or

wsl --list --online # For a valid list of distribution names
wsl --install -d <Distribution Name> # <Distribution Name> from the list got by upper command

Add a User for WSL (Optional)

adduser <username>
usermod -aG sudo <username> # Add user to sudoers

Install Visual Studio Code

Download

Use Windows Proxy

Ubuntu version at least to be 22.04

In your %UserProfile% directory (use cd ~ to access your home directory, which is typically your user profile, C:\Users\<UserName>), create .wslconfig file, paste the following configurations into it:

[wsl2]
networkingMode=mirrored

After configuring this, you need to run wsl --shutdown to stop WSL first, and then relaunch terminal.

If you are using Clash Verge for proxy, TUN mode is recommended. FYI, the network hotspot sharing only works in GVisor mode.

Install zsh

sudo apt install zsh
# Make zsh the default shell.
# NOTE: Run below command WITHOUT sudo!
# If you use `sudo` it will change the shell not for your working user but for root
chsh -s $(which zsh)
  1. Install oh-my-zsh
  2. Change theme

Install Homebrew

Homebrew on Linux

Useful Commands

explorer.exe .       # Open current folder in windows
wsl.exe hostname -I  # get the ip address

Configure Port Forwarding to Access WSL2 from the Local Network

To allow devices on your local network to access services running in WSL2, follow these steps:

Run the following commands in PowerShell (Admin).

  1. Enable port forwarding from windows to WSL2

    netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=<PORT> connectaddress=<WSL2_IP> connectport=<PORT>
    # for example port 3000
    netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=3000 connectaddress=localhost connectport=3000
  2. Allow the port in windows firewall

    New-NetFirewallRule -DisplayName "Allow_TCP_<PORT>" -Direction Inbound -LocalPort <PORT> -Protocol TCP -Action Allow
    # for example port 3000
    New-NetFirewallRule -DisplayName "Allow_TCP_3000" -Direction Inbound -LocalPort 3000 -Protocol TCP -Action Allow
  3. Verify forwarding

    # Check active port proxies:
    netsh interface portproxy show all
  4. Delete an unwanted forwarding rule

    netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=<PORT>

Additional Information

Terminal Configurations

  1. Size: 72 x 20
  2. Theme: Campbell
  3. Opacity: 95%