WSL
WSL, or Windows Subsystem for Linux, is a feature of Windows that allows you to run a Linux environment on your Windows machine, without the need for a separate virtual machine or dual booting. WSL is designed to provide a seamless and productive experience for developers who want to use both Windows and Linux at the same time[1]
Enable WSL
- Open PowerShell as Admin: Press
Win + Xand select "Terminal (Admin)" or "Windows PowerShell (Admin)". - Install WSL: Type
wsl --installand press Enter. This command enables required features, installs the latest kernel, and defaults to Ubuntu. - Restart: Your computer will likely need to restart to complete the installation.
- Optional : Create User: After reboot, a terminal window will open asking for a Linux username and password (these are separate from your Windows login).
Basic Usage
- Launch: Open the Start Menu and search for your installed distro (e.g., "Ubuntu") or type
wslin Command Prompt/PowerShell. - Update Software: In your Linux terminal, run
sudo apt update && sudo apt upgrade(for Ubuntu/Debian). - Install Software: Use
sudo apt install <package_name>(e.g.,sudo apt install git). - Access Windows Files: Your Windows drives are mounted under
/mnt/(e.g.,cd /mnt/c/Users/YourUser).
WSL commands
#WSL check status
>wsl -l -v
# Start WSL machine
>wsl -d <machine name>
# Stop a WSL machine
>wsl -t <machine name>
# Stop all WSL machine
>wsl --shutdown
Enable windows ping response
# as Admin Powershell
Set-NetFirewallRule -Name CoreNet-Diag-ICMP4-EchoRequest-In -enabled True
WSL Networking
Networking in WSL can be configured in various ways to enable communication between Windows and Linux applications.
Default Networking Mode: NAT
By default, WSL uses a NAT (Network Address Translation) based architecture. This means that the Linux distribution running under WSL has its own IP address, separate from the Windows host. To access a Linux networking app from Windows, you can use localhost. Conversely, to access a Windows networking app from Linux, you need to use the IP address of the Windows host.
Identifying IP Addresses
From Windows to Linux: To find the IP address of a Linux distribution running via WSL2, use the command:
wsl -d <DistributionName> hostname -I
From Linux to Windows: To find the IP address of the Windows host from a Linux distribution, use the command:
ip route show | grep -i default | awk '{ print $3}'
Mirrored Mode Networking
On machines running Windows 11 22H2 and higher, you can enable mirrored mode networking by setting networkingMode=mirrored under [wsl2] in the .wslconfig file at C:\Users\<username> folder. This mode mirrors the network interfaces from Windows into Linux, allowing for improved compatibility and new features.
Benefits of Mirrored Mode
- IPv6 Support: Enables IPv6 connectivity.
- Localhost Access: Allows connecting to Windows servers from within Linux using localhost (127.0.0.1).
- VPN Compatibility: Improved networking compatibility for VPNs.
- Multicast Support: Enables multicast networking.
- LAN Access: Allows connecting to WSL directly from your local area network (LAN).
Accessing Applications
From Windows to Linux
To access a Linux networking app from Windows, you can use localhost as the destination address. For example, if you have a Node.js server running on Linux, you can access it from a Windows browser using http://localhost:3000.
From Linux to Windows
To access a Windows networking app from Linux, you need to use the IP address of the Windows host. For example, to connect to a Node.js server running on Windows, you can use the following command in Linux:
curl http://<WindowsHostIP>:3000
Advanced Configuration
You can configure advanced networking settings using the wsl.conf and .wslconfig files.
- wsl.conf file is used for per-distribution settings,
- .wslconfig file is used for global settings across all WSL distributions.
Example .wslconfig File : This configuration enables mirrored mode networking, DNS tunneling, and automatic proxy settings
[wsl2]
networkingMode=mirrored
dnsTunneling=true
autoProxy=true
By understanding and configuring these networking options, you can optimize the communication between your Windows and Linux applications running under WSL.
WSL machine network configuration
$ cat /etc/wsl.conf
$ cat /etc/wsl-distribution.conf
Remote Connection to WSL
To login into WSL from remote,
- Enable OpenSSH package on Windows host[2] . (we also can use GUI windows through quickly launch the GUI with
optionalfeaturesorappwiz.cpl)
#check available OpenSSH service
PS C:> Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
Name : OpenSSH.Client~~~~0.0.1.0
State : NotPresent
Name : OpenSSH.Server~~~~0.0.1.0
State : NotPresent
# Install the OpenSSH Client
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
# Install the OpenSSH Server
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
# Reboot system and do following commands on powershell
# Start the sshd service
Start-Service sshd
# OPTIONAL but recommended:
Set-Service -Name sshd -StartupType 'Automatic'
# Confirm the Firewall rule is configured. It should be created automatically by setup. Run the following to verify
if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue)) {
Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..."
New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
} else {
Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists."
}
- Next, set up SSH for each WSL instance. Here, I use a different port number for each instance.
- todo
$ sudo apt install openssh-server